| Author |
Beginner Bank machine program
|
Rob Fuhrman
Greenhorn
Joined: Nov 16, 2004
Posts: 3
|
|
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
|
 |
Jeff Bosch
Ranch Hand
Joined: Jul 30, 2003
Posts: 804
|
|
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!
|
Give a man a fish, he'll eat for one day. <br />Teach a man to fish, he'll drink all your beer.<br /> <br />Cheers,<br /> <br />Jeff (SCJP 1.4, SCJD in progress, if you can call that progress...)
|
 |
Rob Fuhrman
Greenhorn
Joined: Nov 16, 2004
Posts: 3
|
|
|
will do - do you want me to re post?
|
 |
Junilu Lacar
Bartender
Joined: Feb 26, 2001
Posts: 4115
|
|
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?
|
Junilu - [How to Ask Questions] [How to Answer Questions] [MiH]
|
 |
Rob Fuhrman
Greenhorn
Joined: Nov 16, 2004
Posts: 3
|
|
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.
|
 |
 |
|
|
subject: Beginner Bank machine program
|
|
|