• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Beginner Bank machine program

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 808
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, Rob -

Welcome to JavaRanch. Please use the CODE UBB tags to format your code to make it easier for us to read. The CODE tag will preserve indenting, and it's located at the bottom of the compose/edit screen.

Thanks!
 
Rob Fuhrman
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
will do - do you want me to re post?
 
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Rob,

1. You need to import the Savitchin class (I take it this a class that your instructor provided)
2. takePenny(int n) has a bug. What happens if n is greater than the number of pennies in the jar?
 
Rob Fuhrman
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
whoops hehe - a mistake in interpreting what I need to do for this assignment. As it turns out I do not have to have user input, but I do understand where my bugs are, thanks very much!

FYI the next task requires user input so I'll probably be posting soon lol

Thanks very much for your help
rob.
 
reply
    Bookmark Topic Watch Topic
  • New Topic