Results 1 to 2 of 2
  1. #1
    paul is offline Member
    Join Date
    Jul 2007
    Posts
    26
    Rep Power
    0

    Default 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;}
            
        }
    Thanks.

  2. #2
    coco is offline Member
    Join Date
    Jul 2007
    Posts
    39
    Rep Power
    0

    Default

    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;
    }
    Greetings.

Similar Threads

  1. Log4j not working properly....
    By prakash_dev in forum Advanced Java
    Replies: 0
    Last Post: 03-17-2008, 01:13 PM
  2. Adding taglibs in JSP
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-14-2008, 12:43 AM
  3. Adding Tabs to JTabbedPane
    By blackstone in forum AWT / Swing
    Replies: 2
    Last Post: 11-14-2007, 04:15 PM
  4. Replies: 3
    Last Post: 11-03-2007, 02:58 PM
  5. Adding a Restock Fee
    By Nexcompac in forum New To Java
    Replies: 2
    Last Post: 07-31-2007, 03:46 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •