#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    7
    Rep Power
    0

    Return total method


    Hello: I am stuck on something I need a bit of guidance on. Here is what I am needing to do:

    Write a class encapsulating the concept of coins, assuming that coins have the following attributes: a number of quarters, a number of dimes, a number of nickels, a number of pennies. Include a constructor, the accessors and mutators, and methods toString and equals. Also code the following methods: one returning the total amount of money in dollar notation with two significant digits after the decimal point, and other returning the money in quarters ( for instance, 0.75 if there are three quarters ), in dimes, in nickels and in pennies. Write a client class to test all the methods in your class.

    I am stuck on how to structure my total return method. Here is what I have so far, thanks for the help.




    Code:
    import java.text.DecimalFormat;
    
    public class Coins
    {
        private int quarters;
        private int dimes;
        private int nickels;
        private int pennies;
        private int total;
        
        public Coins ()
        {
        }
        
        public Coins ( int q, int d, int n, int p, int t )
        {
           quarters = q;
           dimes = d;
           nickels = d;
           pennies = p;
           total = t;
        }
        
        public int getQuarters()
        {
            return quarters;
        }
        
        public int getDimes()
        {
            return dimes;
        }
        
        public int getNickels()
        {
            return nickels;
        }
        
        public int getPennies()
        {
            return pennies;
        }
        
        public int getTotal()
        {
            return total;
        }
        
    }
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2005
    Posts
    74
    Rep Power
    33
    I'm not sure you need the first constructor. If you wanted to create an instance with no coins, just pass 0 to all the parameters of the second constructor.

    I'm not American so forgive me if I get the values wrong, but assuming:

    Quarters: 25cent
    Dime: 10cent
    Nickel: 5cent
    Pennies: 1cent

    If I had 1 quarter, 1 dime, 2 nickels & 3 pennies I'd have (25+10+(2x5)+(3x1))
    Therefore I'd have 48cents.

    Now... put that into code form
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    7
    Rep Power
    0
    Ok, so in my constructor I need to designate the what the value of each coin is, which makes sense. But, then to calculate my total for the coins would it be something like:

    Code:
    public int getTotal( quarters + dimes + nickels + pennies)
        {
            return total;
    }
    I guess I'm a bit unsure also on how to structure the methods to return the money in quarters ( for instance, 0.75 if there are three quarters ), in dimes, in nickels and in pennies.
  6. #4
  7. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,724
    Rep Power
    348
    to calculate my total for the coins would it be something like:
    You need an equation something dua24uk posted that adds together the values of all the coins for the total.

    The equation would NOT be inside the ()s
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    3
    Rep Power
    0
    Originally Posted by Nightryno
    Ok, so in my constructor I need to designate the what the value of each coin is, which makes sense. But, then to calculate my total for the coins would it be something like:

    Code:
    public int getTotal( quarters + dimes + nickels + pennies)
        {
            return total;
    }
    I guess I'm a bit unsure also on how to structure the methods to return the money in quarters ( for instance, 0.75 if there are three quarters ), in dimes, in nickels and in pennies.
    Hi, I just signed up and I'm also new to programming, but I thought I could help here.

    You aren't declaring your method correctly. After your method name the values you declare in the parentheses are what you are going to be passing into your method. In your case the getTotal method does not need any information from the class to determine which value to return, so you can start your method off with....

    Code:
    public int getTotal(){
    //your method body
    return total;
    }
    You can determine the value of total by counting up the value of your individual coins.
    total = (25*quarters)+(10*dimes) and so on

IMN logo majestic logo threadwatch logo seochat tools logo