Lab: PiggyBank, a Class with instance variables

Colloration: Work on this with one person in Lab

You are to implement class PiggyBank. Implement always means that in addition to writing code that appears to work, you should also write tests to "prove" your code works. A PiggyBank object encapsulates the contents of a piggy bank with messages associated with real world actions. For our purposes, the bank should know how many coins are in it and the cash value of those coins (only pennies, nickels and dimes are allowed to save time). Banks can only hold a finite limit of coins. This amount should be 50 for any and all piggy bank objects.

The first task is to write a constructor and decide what instance variables you will need (feel free to add them later, if you find the need.) PiggyBank objects need no arguments in their constructors, just initialize your instance variables. Here is a summary of all methods needed with comments specifying the details of the expected behavior:

// Construct an empty PiggyBank that can store 50 pennies, nickels,

// and/or dimes (no quarter, half, or dollar coins to save time)

public PiggyBank()

// A getCapacity() message should return the maximum number

// of coins that can be stored in this PiggyBank object.

publicint getCoinCapacity()

// A getTotalNumberOfCoins() message should return the number of

// coins currently in the bank. The coins can be of any type.

publicint getTotalNumberOfCoins()

// A getTotalCashInBank() message should return the total amount of

// cash in the bank. Pennies are $0.01, nickels are $0.05, and dimes

// are $0.10 (no quarter, half, or dollar coins to save time).

publicdouble getTotalCashInBank()

// An addPennies message tries to add the given number of pennies to

// the bank. It will add as many pennies as can fit and return the

// amount added. For example, if the bank is empty and we add 50

// pennies, which is always the capacity a bank can fit, this method

// will return 50. If the bank has 20 coins in it and we try to add

// 50 coins, the method will return 30, as that is the number that

// actually went in. If the bank is full and we try to add 1 penny,

// this method will return 0, since no pennies were added.

publicint addPennies(int penniesEntered)

// An addNickels message is identical to the addPennies, but adds nickels instead.

// Remember the return value indicates how many nickels were actually added.

publicint addNickels(int numberOfNickels)

// An addDimes message is identical to the addPennies, but adds dimes instead.

// Remember the return value indicates how many dimes were actually added.

publicint addDimes(int numberOfDimes)

// A drainTheBank message takes all of the money out of the bank.

// The return value must be how much money was drained from the bank.

publicdouble drainTheBank()

// This method gives the user the satisfaction of "hearing" the money

// in their bank. If the bank has at least one coin in it, return

// "*jingle jingle*" will be returned. If the bank is empty, the

// String "*cricket cricket*" will be returned.

public String shake()

One Test Method to get you started and help explain behavior of methods

importstatic org.junit.Assert.*;

import org.junit.Test;

publicclass PiggyBankTest {

@Test

publicvoid testFailedAddPenniesWhenCapacityNotReached() {

PiggyBank oinky = new PiggyBank();

assertEquals("Adding one penny to an empty bank should return 1",

1, oinky.addPennies(1));

assertEquals(1, oinky.getTotalNumberOfCoins());

assertEquals(0.01, oinky.getTotalCashInBank(), 0.001);

assertEquals(1, oinky.addPennies(1));

assertEquals(2, oinky.getTotalNumberOfCoins());

assertEquals(0.02, oinky.getTotalCashInBank(), 0.001);

assertEquals(50, oinky.getCoinCapacity());

}

}

Submit your project to WebCat

You will be graded on a scale of 0.0 through 20.0 automatically by WebCat.[1]

___/ +20 Correctness and code coverage

[1]Rick has posted how to get the WebCat submission plugin for Eclipse on you home machine, along with an alternative if you have trouble with the plugin