Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

I dont know if any of it is right...need some help and/or direction

public class PiggyBank
{
private double balance;
private double pennies;
private double nickels;
private double dimes;
private double quarters;

public PiggyBank(double pen, double nick, double dime, double quart)
{
balance = 0.0;
pennies = pen;
nickels = nick;
dimes = dime;
quarters = quart;
}

public void depositPennies(double amount)
{
pennies = amount;
balance += pennies;
}

public void depositNickels(double amount)
{
nickels = amount * 5;
balance += nickels;
}

public void depositDimes(double amount)
{
dimes = amount * 10;
balance += dimes;
}

public void depositQuarters(double amount)
{
quarters = amount * 10;
balance += quarters;
}

public double getPennies()
{
return pennies;
}

public double getNickels()
{
return nickels;
}

public double getDimes()
{
return dimes;
}

public double getQuarters()
{
return quarters;
}

public double getBalance()
{
return balance;
}
}


import java.util.*; // Needed for the Scanner class

// This program demonstrates the PiggyBank class

public class PiggyBankTest
{
public static void main(String[] args)
{
PiggyBank bank;
double balance,
pennies,
nickles,
dimes,
quarters;

Scanner keyboard = new Scanner(System.in);

System.out.print("What is your staring balance? ");
balance = keyboard.nextDouble();

bank = new PiggyBank(pennies, nickles, dimes,
quarters);

System.out.print("How many pennies do you have? ");
pennies = keyboard.nextDouble();
bank.deposit(pennies);

System.out.print("How many nickels do you have? ");
nickels = keyboard.nextDouble();
bank.deposit(nickles);

System.out.print("How many dimes do you have? ");
dimes = keyboard.nextDouble();
bank.deposit(dimes);

System.out.print("How many quarters do you have? ");
quarters = keyboard.nextDouble();
bank.deposit(quarters);
}
}

bank.deposit(pennies);
^
symbol: method deposit(double)
location: variable bank of type PiggyBank
PiggyBankTest.java:28: error: cannot find symbol
nickels = keyboard.nextDouble();
^
symbol: variable nickels
location: class PiggyBankTest
PiggyBankTest.java:29: error: cannot find symbol
bank.deposit(nickles);


Comments

  • adetola68adetola68 Member Posts: 7
    As a beginner, I still need to give you credit. In no time, you will become an experience
    programmer.

    I could deduce from your code that you are trying to implement an application for
    a bank. Let leave that and focus on why you getting error.

    You are calling a method you didn't define in the PiggyBank class, trust me the compiler won't let you do that. The method deposit() ins't define;
    [code]bank.deposit(pennies);
    bank.deposit(nickles);
    bank.deposit(dimes);
    bank.deposit(quarters); [/code]

    Instead, you are to call the methods that are define inside the class;
    [code]bank.depositPennies(pennies);
    bank.depositNickels(nickles);
    bank.depositDimes(dimes);
    bank.depositQuarters(quarters);[/code]

    One more thing; do not pass unintialized variable to a constructor/method;
    [code]bank = new PiggyBank(pennies, nickles, dimes, quarters);[/code]

    If unsure, try to pass a default value like I did
    [code]bank = new PiggyBank(0.0, 0.0, 0.0, 0.0);[/code]

    Apart from the aforementioned issue, I believe you're on the right track. Copy the modify PiggyBankTest class and replace it with yours

    [code]import java.util.Scanner;

    public class PiggyBankTest
    {
    public static void main(String[] args)
    {
    PiggyBank bank;
    double balance,
    pennies,
    nickles,
    dimes,
    quarters;

    Scanner keyboard = new Scanner(System.in);

    System.out.print("What is your staring balance? ");
    balance = keyboard.nextDouble();

    //bank = new PiggyBank(pennies, nickles, dimes, quarters);
    bank = new PiggyBank(0.0, 0.0, 0.0, 0.0);

    System.out.print("How many pennies do you have? ");
    pennies = keyboard.nextDouble();
    //bank.deposit(pennies);
    bank.depositPennies(pennies);

    System.out.print("How many nickels do you have? ");
    nickles = keyboard.nextDouble();
    //bank.deposit(nickles);
    bank.depositNickels(nickles);

    System.out.print("How many dimes do you have? ");
    dimes = keyboard.nextDouble();
    //bank.deposit(dimes);
    bank.depositDimes(dimes);

    System.out.print("How many quarters do you have? ");
    quarters = keyboard.nextDouble();
    //bank.deposit(quarters);
    bank.depositQuarters(quarters);


    }
    }

    [/code]

    Cheers
Sign In or Register to comment.