New to Programming, need help with HW

jec1521

Weaksauce
Joined
Sep 2, 2005
Messages
106
Hey, I'm taking an AP Comp Sci class in High school and we're learning Java, and this is my first try at programming. We have an assignment to do... here are the instructions:

Create your own PiggyBank class. This class will be used to create PiggyBank objects; therefore, everyone can have their own piggy bank! When your personal bank is created (instantiated), it may be stuffed with a certain number of pennies, nickels, dimes, and quarters. You will be able to access the number of each type of coin deposited into the bank as well as the bank’s total dollar value. You will also need to create methods that will allow the piggy bank to receive coins and update the appropriate values.

Now here is what I have. I think it's right, but if not let me know. I keep on getting errors when trying to compile.
Code:
public class PiggyBank {
    
    private int pennies;
    private int nickels;
    private int dimes;        
    private int quarters;
            
    /** Creates a new instance of PiggyBank */
    public PiggyBank(int findPennies, int findNickels, int findDimes, int findQuarters) {
        pennies = findPennies;
        nickels = findNickels;
        dimes = findDimes;
        quarters = findQuarters;  
        
    }
    public int getPennies()
    {
        return pennies;
    }
    public int getNickels()
    {
        return nickels;
    }
    public int getDimes()
    {
        return dimes;
    }
    public int getQuarters()
    {
        return quarters;
    }
    public void setPennies(int newPennies)
    {
        pennies = newPennies;
    }
    public void setNickels(int newNickels)
    {
        nickels = newNickels;
    }
    public void setDimes(int newDimes)
    {
        dimes = newDimes;
    }
    public void setQuarters(int newQuarters)
    {
        quarters = newQuarters;
    }
    public static void main (Stringargs[])
    {
    PiggyBank myPiggyBank = new PiggyBank(20, 10, 5, 12);
    System.out.println("There are" +myPiggyBank.getPennies() +"pennies.");
    System.out.println("There are" +myPiggyBank.getNickels() +"nickels.");
    System.out.println("There are" +myPiggyBank.getDimes() +"dimes.");
    System.out.println("There are" +myPiggyBank.getQuarters() +"quarters.");
    myPiggyBank.setPennies(30);
    myPiggyBank.setNickels(20);
    myPiggyBank.setDimes(15);
    myPiggyBank.setQuarters(22);
    System.out.println("Now there are" +myPiggyBank.getPennies() +"pennies.");
    System.out.println("Now there are" +myPiggyBank.getNickels() +"nickels.");
    System.out.println("Now there are" +myPiggyBank.getDimes() +"dimes.");
    System.out.println("Now there are" +myPiggyBank.getQuarters() +"quarters.");
    }

    
    
}

The errors that are coming up are "identifier expected" on line 61

And it also says it needs a ")" on line 77
 
Nevermind guys, I found my mistake. I forgot a space in between String and args. Thanks anyway.
 
If you haven't yet read the sticky at the top: Programming Students. READ THIS! yet please do. When you first start programming you will have to learn how lots of small errors get handled and things will confuse you. I solved these problems by trial and error and taking the compiler errors and doing a search on the Internet to learn about the problem. In my experience this method was much faster then posting the problem on this forum would have been. The first time I did a problem like the one you posted it took me a few hours to complete. I have run into single problems that took me longer than that to solve and I still did not feel the need to ask on a forum because I would have solved the problem before getting a response most likely anyways. The java online documentation will be your best friend, I have used that for almost every java problem I have ever run into.
 
Back
Top