// Example 4 of OOP - data
hiding
public class oop4
{
public static void main(String[] args)
{
PiggyBank P = new PiggyBank(42,8,14,7); //
pennies, nickles, dimes, quarters
System.out.println("Piggy Bank's value is $" +
P.GetValue());
P.AddPennies(4);
System.out.println("You now have " + P.GetPennies()
+ " pennies");
}
}
class PiggyBank
{
private int Pennies;
int Nickles, Dimes, Quarters;
PiggyBank (int P, int N, int D, int Q)
{
Pennies = P; Nickles = N; Dimes = D;
Quarters = Q;
}
void AddPennies (int P)
{
Pennies = Pennies + P;
}
int GetPennies ()
{
return Pennies;
}
double GetValue()
{
return Pennies * 0.01 + Nickles * 0.05 + Dimes *
0.10 + Quarters * 0.25;
}
}