Unit 5 demo - PiggyBank
//
// PiggyBank.java
//
public class PiggyBank
{
public PiggyBank()
{
nickels = 0;
dimes = 0;
quarters = 0;
}
// (normal) member functions
public int total()
{
return nickels*5 + dimes*10 + quarters*25;
}
public void addNickel()
{
nickels += 1;
}
public void addDime()
{
dimes += 1;
}
public void addQuarter()
{
quarters += 1;
}
// static function ("class function")
public static int valueOfDime()
{
return 10;
}
// static variable ("class variable")
public static float PI = 3.14f;
private int nickels;
private int dimes;
private int quarters;
}
//
// PiggyBankTest.java
//
public class PiggyBankTest
{
public static void main(String[] args)
{
PiggyBank piggyBank = new PiggyBank();
piggyBank.addNickel();
piggyBank.addNickel();
piggyBank.addNickel();
piggyBank.addDime();
piggyBank.addDime();
piggyBank.addQuarter();
piggyBank.addQuarter();
System.out.println("total: " + piggyBank.total());
System.out.println("value of dime: " +
PiggyBank.valueOfDime());
System.out.println("pi: " + Math.PI);
System.out.println("piggybank pi: " + PiggyBank.PI);
}
}