2 Replies Latest reply: Apr 9, 2013 3:46 AM by thomas.behr RSS

    return method problem

      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.

      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;
        • 1. Re: return method problem
          Looks like a simple caculation is needed. What exactly is your problem with that?

          • 2. Re: return method problem
            Your Coins class should not have a field total (and consequently, there should not be a constructor parameter for total nor a getter method for total). Instead of your current approach, there should be a method public String calcTotalDollars() that calculates the total value of - well - the coins in the Coins instance. Similarly, there should be methods calcTotalQuarters, calcTotalDimes, etc. that express the total dollar value as quarters, dimes, etc.

            As a first step, return "exact" value. Only when that works as it should, return values rounded to two fractional digits.