aspose file tools*
The moose likes Beginning Java and the fly likes Help with objectifying code (constructor method) for simple compound interest quesion Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Help with objectifying code (constructor method) for simple compound interest quesion" Watch "Help with objectifying code (constructor method) for simple compound interest quesion" New topic
Author

Help with objectifying code (constructor method) for simple compound interest quesion

java nubee
Greenhorn

Joined: Mar 09, 2006
Posts: 4
Howdy Partners.
First time here (so be gentle).
Newbee to Java (but getting there).
Have class assign to calculate compound interest.
1st bit of assign ok with so far. i.e. 3 command line args (amount,rate,time) are used to output a total (integer).

2nd bit of assign i'm stuck:

". . .rewrite the class as account..
using a constructor of the form:
public Account(int a, float r) { ... }
and a method of the form
public double getBalance(int time) { ... }
that returns the balance the queried Account object would have after the elapsed time/years. This returns a double and is supposed to leave the balance in the queried Account object unchanged.


I have started with this


Thats as far as i can get with my little brain.
I dont know where i sould put public void main(String.....
(or even if it needs one).

Any help is much appreciated.

:-)
Tom Sullivan
Ranch Hand

Joined: Dec 20, 2005
Posts: 72
In your constructor, change it to be:

this.amount = a;

Do the same for all values you pass in where you have declared the local vars.

You don't have to have a main in this class. You could do:



Now you can use another class to instantiate this one for testing.



Good luck.
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
I would start by seeing if the code you gave compiles. If not, where are the errors? Can you see how to fix them? Once you get that much to compile, then figure out where main() should go. You could put it in the Account class, if you wish. However, it is very common to have a separate class just with the main() method.

The final thing you need to figure out is what to do in the getBalance() method. Do you know how to calculate compound interested by hand? What is the formula for this? How do you translate that formula into Java? Also, computing some examples by hand will help you verify that your program is correct. I suggest you do these examples before you even write any more code.

Let me know what you figure out from here. And feel free to come back with more questions.

Layne


Java API Documentation
The Java Tutorial
Tom Sullivan
Ranch Hand

Joined: Dec 20, 2005
Posts: 72
One more thing. If you have to use a command line arg, you won't be able to use the example I gave as it sits. But, you can configure the system to take the command line args in either class by incorporating the main thread, taking in the args and then saying new InterestCalc(args[0], args[1], args[2]); in main. Of course this is after you parse the string to the type you want as you would already be doing if your first version works as you expect with a main.
[ March 09, 2006: Message edited by: Tom Sullivan ]
java nubee
Greenhorn

Joined: Mar 09, 2006
Posts: 4
Tom,
From the assignment, i get the impression that its only wanting 1 class and not 2 :-( . The teacher is going to test by doing 'java Account 100 100 1'
Also, he has given us a list of deliverables (ref below).

AccountApplication and AccountApplet form part of the 'teachers' code which shows up in a html/gui face, the 3 input variables and output.

So, i'm still stuck on how to go to the next bit of code.

Arggg ..... help

-C

-------------------------------------------------------------------
-rw-rw-r-- 1 comp285 comp285 474 Mar 3 16:37 AccountApplet.class
-rw------- 1 comp285 comp285 192 Jan 10 10:45 AccountApplet.html
-rw------- 1 comp285 comp285 399 Mar 3 16:39 AccountApplet.java
-rw-rw-r-- 1 comp285 comp285 521 Mar 3 16:37 AccountApplication.class
-rw-rw-r-- 1 comp285 comp285 372 Mar 3 15:54 AccountApplication.java
-rw-rw-r-- 1 comp285 comp285 1272 Mar 3 16:37 Account.class
-rw------- 1 comp285 comp285 2363 Jan 10 10:45 Account.java
-rw-rw-r-- 1 comp285 comp285 1679 Mar 3 16:37 AccountWidget$1.class
-rw-rw-r-- 1 comp285 comp285 1765 Mar 3 16:37 AccountWidget.class
-rw------- 1 comp285 comp285 2602 Mar 3 15:26 AccountWidget.java
-rw-rw-r-- 1 comp285 comp285 1493 Mar 3 16:37 AdvancedAccount.class
-rw------- 1 comp285 comp285 2793 Jan 10 10:45 AdvancedAccount.java
-rw-rw-r-- 1 comp285 comp285 515 Mar 3 16:37 CenteredFrame$1.class
-rw-rw-r-- 1 comp285 comp285 842 Mar 3 16:37 CenteredFrame.class
-rwxrwxrwx 1 comp285 comp285 38 Mar 3 14:29 CenteredFrame.java
-rw-rw-r-- 1 comp285 comp285 1115 Mar 3 16:37 Compound.class
-rw------- 1 comp285 comp285 1479 Mar 3 14:38 Compound.java
drwxrwxr-x 5 comp285 comp285 4096 Mar 3 16:37 doc
-rw-rw-r-- 1 comp285 comp285 378 Mar 3 16:37 Makefile
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Help with objectifying code (constructor method) for simple compound interest quesion