Forums: » Register « |  Free Tools |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |

New Free Tools on Dev Shed!

#1
April 8th, 2013, 03:45 PM
 Nightryno
Registered User

Join Date: Mar 2013
Posts: 7
Time spent in forums: 1 h 10 m 6 sec
Reputation Power: 0

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()
{
}

}```

#2
April 8th, 2013, 04:20 PM
 doa24uk
Contributing User

Join Date: Aug 2005
Posts: 74
Time spent in forums: 17 h 30 m 43 sec
Reputation Power: 32
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

#3
April 9th, 2013, 04:20 PM
 Nightryno
Registered User

Join Date: Mar 2013
Posts: 7
Time spent in forums: 1 h 10 m 6 sec
Reputation 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)
{
}```

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.

#4
April 9th, 2013, 04:46 PM
 NormR
Contributing User

Join Date: Aug 2010
Location: SW Missouri
Posts: 3,614
Time spent in forums: 2 Weeks 3 Days 7 h 4 m 22 sec
Reputation Power: 347
Quote:
 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

#5
April 17th, 2013, 07:23 PM
 JonesJava
Registered User

Join Date: Apr 2013
Posts: 3
Time spent in forums: 49 m 24 sec
Reputation Power: 0
Quote:
 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(){
}```

You can determine the value of total by counting up the value of your individual coins.
total = (25*quarters)+(10*dimes) and so on

 Viewing: Dev Shed Forums > Programming Languages > Java Help > Homework - Return total method