Google Answers Logo
View Question
 
Q: java: simpple piggy bank program ( Answered,   0 Comments )
Question  
Subject: java: simpple piggy bank program
Category: Computers > Programming
Asked by: hafedaddy-ga
List Price: $50.00
Posted: 02 Nov 2006 14:44 PST
Expires: 02 Dec 2006 14:44 PST
Question ID: 779545
I need to create a simple piggy-bank program. The program must be able
to create a penny-jar piggy bank with "p" pennies and ?n?  nickels in
it (0 or any positive integer). From then on one can only add or
subtract pennies or nickels to/from it.
This penny-jar piggy-bank program will allow a user to add or subtract
pennies or nickels, and tell the user the total amount of pennies and
nickels in the jar.
There is an error when it comes to return the total number of pennies and nickels.
How to fix it?

// 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 int total;
// =================================================================
// A constructor for the class.
// This constructor initializes the number of pennies in the jar to
the value of the parameter, p.
// What will we need to do to achieve this?

public PennyJar ()
{
	pennies=0;
	nickels=0;
	total=0;
}
// =================================================================
// This method adds p pennies to the total amount stored in the jar.
public void addPenny (int addPenny)
{
	if (addPenny<=0)
	System.out.println ("AddPenny Operation Failed!");
	else
	pennies=pennies+addPenny;
}
// =================================================================
// This method removes p pennies from the total amount stored in the jar
public void takePenny (int takePenny)
{
	if (takePenny<=0)
	System.out.println ("TakePenny Operation Failed!");
	else
	pennies=pennies+takePenny;
}
// =================================================================
// This method adds n nickels to the total amount stored in the jar.
public void addNickel (int addNickel)
{
	if (addNickel<=0)
	System.out.println ("AddNickel Operation Failed!");
	else
	nickels=nickels+addNickel;
}
// =================================================================
// This method removes n nickels from the total amount stored in the jar
public void takeNickel (int takeNickel)
{
	if (takeNickel<=0)
	System.out.println ("TakeNickel Operation Failed!");
	else
	nickels=nickels+takeNickel;
}
// =================================================================
// This method returns the total number of pennies and nickels stored in the jar.
public int total (int total)
{
	if (total<=0)
	System.out.println ("Total Operation Failed!");
	else
	total=pennies+nickels;
}
	return total;
// =================================================================
}
// End PennyJar
Answer  
Subject: Re: java: simpple piggy bank program
Answered By: answerguru-ga on 03 Nov 2006 01:06 PST
 
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
Comments  
There are no comments at this time.

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy