Hi guys,
I am trying to write a piggy bank program to keep track of pennies. I need the user to enter in the number of pennies to add or subtract.
This is the main class (I think lol)
// PennyJar.java
// A class to build and operate a penny jar.
// Robbie Fuhrman Student # 0423845
// This is PennyJar.java for Task 3
public class PennyJar2
{
// create an instance data variable.
// It will be a private instance variable --
//accessible only through methods in the PennyJar class
// It will be an int
// Its name will be pennies
private int pennies;
int n;
// ================================
public PennyJar2 (int n)
{
// A constructor for the class. This constructor
// initializes the number of pennies in the jar to the
// value of the parameter, n.
// what will I need to do to "pennies" to achieve the state of "n"
// pennies in the jar?
pennies = n;
}
// ===================================
public void addPenny ()
{
// this method adds 1 penny to the total amount stored
// in the jar.
// What will we need to do to "pennies" to achieve this?
pennies=pennies + 1;
}
// =================================
public void addPenny (int n)
{
// this method adds 1 penny to the total amount stored
// in the jar.
// What will we need to do to "pennies" to achieve this?
pennies=pennies + n;
}
// =================================
public void takePenny ()
{
// this method removes a penny from the total amount
// stored in the jar.
// what will we need to do to "pennies" to achieve this?
// how will we handle the error message?
if (pennies > 0)
{
pennies=pennies-1;
}
else
{
System.out.println ("Sorry you do not have enough pennies in the jar");
}
}
// ========================
public void takePenny (int n)
{
// this method removes a penny from the total amount
// stored in the jar.
// what will we need to do to "pennies" to achieve this?
// how will we handle the error message?
if (pennies > 0)
{
pennies=pennies-n;
}
else
{
System.out.println ("Sorry you do not have enough pennies in the jar");
}
}
// ========================
public int total ()
{
// this method returns the total number of pennies
// currently stored in the jar.
// what will we need to return?
return pennies;
}
// ================================
} // End PennyJar
And this is the driver program that they want us to use.
// PennyJarTester2.java
// This code will
test my PennyJar object
public class PennyJarTester2
{
public static void main(
String[] args)
{
PennyJar p1 = new PennyJar(0);
System.out.println ("How many pennies would you like to add to the penny jar?");
p1.addPenny= Savitchin.readLineInt();
System.out.print("there are now " + p1.total());
System.out.println(" pennies in jar p1.");
System.out.println ("Now how many pennnies would you like to withdraw from the penny jar?");
p1.takePenny= Savitchin.readLineInt();
System.out.print("there are now " + p1.total());
System.out.println(" pennies in jar p1.");
} // end Main method
} // end PennyJarTester
I tried to get it to work but it didn't recognize or like my Savitchin.readLineInt command.
Hope you guys can help!
Thx in advance!!
rob