Results 1 to 2 of 2
- 07-16-2007, 05:48 PM #1
Member
- Join Date
- Jul 2007
- Posts
- 26
- Rep Power
- 0
how to properly express adding...
I want to be able to allow for an increase in cents, but obviously I don't want people to be able to have 15dollars and 190cents because that's just not how it is supposed to be. Can anyone give me some tips on how to properly express adding to dollars and subtracting to cents when my cents > 99?
Also eclipse is giving me an error when i try and add fixedCents in my toString method. How do i write it so that I get the fixedCents variable from the add method?
Java Code:public class USDollars { private int dollars; private int cents; private int increase; public USDollars(int dollars, int cents, int increase){ this.dollars = dollars; this.cents = cents; this.increase = increase; } public int add(){ fixedCents = cents + increase; if (fixedCents > 99){ dollars + 1; fixedCents-100; } } public String toString(){return dollars+"."+fixedCents;} }
- 08-07-2007, 05:08 AM #2
Member
- Join Date
- Jul 2007
- Posts
- 39
- Rep Power
- 0
You are getting an error about the fixedcents variable in your toString method because fixedcents no longer exists.
Fixedcents is declared in your add() method. Any variable that is declared in a method is freed/garbage collected when that method terminates. Hence why you have dollars, cents, and increase declared at the top of your file outside of any method. If you want fixedCents to exist for all your methods declare it with dollars, cents and increase.
I should mention that at second glance, I don't see where you've declared FixedCents at all... Thus, my above comment may or may not be correct.
Why don't you just change every instance of fixedCents to cents?
Example
Java Code:public int add(){ cents = cents + increase; if (cents > 99){ dollars + 1; cents-100; } } public String toString(){ return dollars+"."+cents; }
Similar Threads
-
Log4j not working properly....
By prakash_dev in forum Advanced JavaReplies: 0Last Post: 03-17-2008, 12:13 PM -
Adding taglibs in JSP
By Java Tip in forum Java TipReplies: 0Last Post: 01-13-2008, 11:43 PM -
Adding Tabs to JTabbedPane
By blackstone in forum AWT / SwingReplies: 2Last Post: 11-14-2007, 03:15 PM -
problem with viewing .java files when Visual J# 2005 Express Edition exist
By unhurt in forum New To JavaReplies: 3Last Post: 11-03-2007, 01:58 PM -
Adding a Restock Fee
By Nexcompac in forum New To JavaReplies: 2Last Post: 07-31-2007, 02:46 PM
Bookmarks