Hello hafedaddy-ga,
Your question referred you trouble you were having with being able to
return the total number of pennies and nickels.
The problem appears to be with the way you are maintain your three
instance variables (pennies, nickels, and total). I believe the total
variable actually represents the monetary value of the PennyJar. This
means that each time you add or remove a penny or nickel from the jar,
you also have to update the total value contained in the jar.
Also, in the final method, it's not entirely clear whether you intend
to simply count how many pennies and nickels there are in the PennyJar
or simply how much money is in the PennyJar.
I have edited your code below so that the total represents the amount
of money in the PennyJar, and have restructured the last method so
that it returns a count of the number of coins (pennies and nickels)
in the PennyJar. The second to last method simply returns the total
instance variable, which represents how much money is in the jar.
// Create instance data variables. They will be private instance variables
// They will be accessible only through methods in the PennyJar class
// They will be int
// Their names will be pennies and nickels
// =================================================================
public class PennyJar
{
private int pennies;
private int nickels;
private double total;
// =================================================================
// A constructor for the class.
// This constructor initializes the number of pennies in the jar to
the value of the parameter, p.
// NOTE: added a parameter to the constructor to initialize the pennies
// instance variable
public PennyJar (int p)
{
pennies=p;
nickels=0;
total=0.0;
}
// =================================================================
// This method adds p pennies to the total amount stored in the jar.
public void addPenny (int p)
{
if (p<=0){
System.out.println ("AddPenny Operation Failed!");
}
else{
pennies = pennies+p;
total = total + p*0.01;
}
}
// =================================================================
// This method removes p pennies from the total amount stored in the jar
public void takePenny (int p)
{
if (p<=0 || pennies <= 0){
System.out.println ("TakePenny Operation Failed!");
}
else{
pennies = pennies-p;
total = total - p*0.01;
}
}
// =================================================================
// This method adds n nickels to the total amount stored in the jar.
public void addNickel (int n)
{
if (n<=0){
System.out.println ("AddNickel Operation Failed!");
}
else{
nickels=nickels+n;
total = total + n*0.05;
}
}
// =================================================================
// This method removes n nickels from the total amount stored in the jar
public void takeNickel (int n)
{
if (n<=0 || nickels <= 0){
System.out.println ("TakeNickel Operation Failed!");
}
else{
nickels=nickels-n;
total = total - n*0.05;
}
}
// =================================================================
// This method returns the total number of pennies and nickels stored in the jar.
public double getTotalValue()
{
return total;
}
// =================================================================
// This method returns the total number of coins (pennies and nickels)
// currently stored in the penny jar.
public int getNumberOfCoins()
{
int coinTotal = pennies + nickels;
return coinTotal;
}
}
// End PennyJar
You should now be able to use these methods to add and remove coins
from PennyJar objects, as well as determine how many coins are in the
jar and the total value of the coins in the jar.
Hopefully that answers your question - if you have any questions or
have any trouble understanding the code or analysis above, post a
clarification and I will attempt to respond promptly.
Thank you for using Google Answers!
Cheers,
answerguru-ga |