Tendo City
Graphing Compound Interest in Java - Printable Version

+- Tendo City (https://www.tendocity.net)
+-- Forum: Tendo City: Metropolitan District (https://www.tendocity.net/forumdisplay.php?fid=4)
+--- Forum: Ramble City (https://www.tendocity.net/forumdisplay.php?fid=44)
+--- Thread: Graphing Compound Interest in Java (/showthread.php?tid=2791)



Graphing Compound Interest in Java - The Former DMiller - 21st April 2005

I'm a bit rusty using Java...rusty meaning I remember almost nothing about programming in it. It's been over 4 years since I did anything other than short little Javascripts, but they aren't the same as full-fledged Java. What I need to do is write a program that will ask the user for four inputs: the principle, the interest, the unit of time, and the amount of time. It will then compute the compound interest and create a graph based on the data obtained. Any hints as to what I will need to do would be greatly appreciated. I don't think I've ever created graphs in Java so I'll have to be trying that out cold.


Graphing Compound Interest in Java - OB1 - 21st April 2005

I will ask my friend.


Graphing Compound Interest in Java - The Former DMiller - 21st April 2005

It's kind of a funny situation. I wrote a lesson plan for my Math and Science methods class that had the students writing a compound interest program. The teacher asked if I could bring in an example of the program because she was interested, and I was stupid and said yes, but mostly because I wanted the A. Turns out I got a 98% on it anyway, but it would look bad if I didn't bring in the program. I wish I had put in the lesson plan that I wrote it in Perl, or some other programming language I wrote in recently.


Graphing Compound Interest in Java - Laser Link - 22nd April 2005

I've been playing with Swing over the last few months, and can't think of anything that lets you draw points and a graph... Let's see what Google says...

Okay, <a href="http://www.sci.usq.edu.au/staff/leighb/graph/">here</a> is a Graph Library that somebody made. It has all the source and some examples. The documentation doesn't look all that helpful, but I just glanced at it. I bet you could hack one of the examples pretty easily, though. It uses Applets.

That looks like the best I could find. I could do this easily in OpenGL, but I haven't really figured out how to use Java Swing to do anything outside of the built in classes. You could check out <a href="http://java.sun.com/docs/books/tutorial/uiswing/14painting/index.html">this tutorial</a> from Sun on Custom Painting. It might help.

Maybe Obi can get you some better help, since his friend has done game programming with Java. When do you need it done by? Do you want to do it on your own, cause I could help if you want.


Graphing Compound Interest in Java - The Former DMiller - 22nd April 2005

I need it done by Monday. The graphing is obviously the hitch point, but I'll check out that graph library and see if I can get it to work.


Graphing Compound Interest in Java - OB1 - 22nd April 2005

Ok I asked my friend for help and here's what he said:


Quote:this Java file will prompt the user for input from the command line, and then convert this input into an int and a double for use in computation


See attached file.


Graphing Compound Interest in Java - Laser Link - 23rd April 2005

Well getting input from the command line isn't the hard part, it's drawing the graph of the interest that is tough because Java doesn't come with any standard classes for graphing. I wonder why that is? You would think that would be a relatively common use, especially among scientists. That's about the only thing Matlab can do well, that and gigantic matrix operations. I guess that's why my EE friends like Matlab. :)

Here's another thought. If you can find a way of using bar graphs, you could use JLabels pretty easily. Just set them each to opaque and set Maximum, Preferred, and Minimum Size for each as well with the values from your computations (for some reason you have to usually set all three size attributes to get it to actually use that size, and even that isn't 100%. Dumb Swing.).


Graphing Compound Interest in Java - Laser Link - 23rd April 2005

You're probably way past this point already Derek, but if you haven't got around to it yet, this might save you some time.
The first <a href="http://www.sci.usq.edu.au/staff/leighb/graph/examples/example1.html">example</a> looks like it can do what you want. Assuming I am right in what you want. :) My understanding is you want to keep track of how much interest you pay over time or something like that, so this should work okay.

It looks like he uses that data array to store each point of the graph (I think it's a parabola):

for(i=j=0; i<np; i++,j+=2) {
data[j] = j-np;
data[j+1] = 60000 * Math.pow( ((double)data[j]/(np-2) ), 2);
}


Sets those DataSet objects (data1 and data2) with the curve info here:

data1 = graph.loadDataSet(data,np);

(np is just the max size of the array, I don't know why he didn't declare it <b>final</b>)

And finally sets the axis with the DataSets and the graph names and units.

xaxis = graph.createAxis(Axis.BOTTOM);
xaxis.attachDataSet(data1);
xaxis.attachDataSet(data2);
.
.
.

Really the only thing you need to even touch is the part where he loads the data array with the data points he wants to graph. You should be able to do it pretty easily. I can't say I've done a lot with Applets (hopefully you have), so I don't know if any of that needs to be changed or if it will automatically pull up a browser and run the applet there. Sorry.

[edit]I don't know why this board won't display all the code. It keeps cutting off the for loop.[/edit]


Graphing Compound Interest in Java - The Former DMiller - 23rd April 2005

That should help a lot. I've had two other projects to work on so I really haven't even scratched the surface of this program yet besides boning up on some basic Java stuff. Tomorrow I'll have the whole day to work on it, however. Thanks for helping out guys.


Graphing Compound Interest in Java - Laser Link - 25th April 2005

Hey OB1, I'm sorry if I sounded like that help wasn't useful. I realize it probably came across that way, and I didn't mean for that to happen. Especially since you had to ask your friend to help us.


Graphing Compound Interest in Java - OB1 - 25th April 2005

Haha, don't sweat it LL. I just asked him to help me with that and I didn't know how much Derek already knew about java so my friend just whipped that up real quick. We were at work, so there wasn't a whole lot of time for detailed explanations. :)