PiggyBank
//
// PiggyBank.java
//
public class PiggyBank
{
public PiggyBank() // note: constructor has no return type
{
total = 0;
}
public void addQuarter()
{
total += 25;
}
public void addDime()
{
total += 10;
}
public void addNickel()
{
total += 5;
}
public int getTotal()
{
return total;
}
private int total;
}
//
// PiggyBankTest.java
//
public class PiggyBankTest
{
public static void main(String[] args)
{
// create (instantiate, construct) a new PiggyBank object
PiggyBank bank = new PiggyBank();
bank.addQuarter();
bank.addQuarter();
bank.addDime();
bank.addDime();
bank.addNickel();
System.out.println("My total is:" + bank.getTotal());
bank.addNickel();
System.out.println("My total is:" + bank.getTotal());
}
}
Output:
My total is:75
My total is:80