Computer Science 15-100 (Sections T & U), Spring 2008
Homework 9b
Due: Fri 4-Apr-2008 at
10am (online submission) and at recitation
(physical copy)
(no late submissions accepted <--
really!)
Start from this file: Hw9b.java
Follow the directions in the header of the file to complete the assignment. For your convenience, here are the contents of the file:
// Hw9b.java
// Do not modify the Hw9b main class. Make this code work
// by adding the appropriate classes with the appropriate methods as
// described by the test methods called by the Hw9b.main method.
// Note that you do not have to add any code to the test cases, though
// you do have to solve them with general-purpose solutions (and not just
// hard-code the example test cases!).
// Hint: to solve this incrementally, you may wish to comment out
// parts of the test code so the parts you have implemented will compile
// and can be tested as you go.
public class Hw9b {
public static void main(String[] args) throws Exception {
testTelevisionClass();
testCokeMachineClass();
}
private static void verify(boolean b) {
if (!b) throw new RuntimeException("Failed check!");
}
//////////////////////////////////
// testTelevisionClass
//////////////////////////////////
private static void testTelevisionClass() {
System.out.println("Running Television class tests...");
// default constructor:
Television tv1 = new Television();
verify(tv1.getStation() == 4); // default station is channel 4
verify(tv1.getVolume() == 3); // default volume is 3 (min = 0, max = 10)
verify(tv1.isMuted() == false); // default is not muted
// getStation + setStation:
tv1.setStation(23); // legal stations in range [1,999]
verify(tv1.getStation() == 23); // we just set it!
tv1.setStation(-1); // -1 is <=0, illegal station, has no effect
verify(tv1.getStation() == 23); // -1 was illegal, had no effect
tv1.setStation(999);
verify(tv1.getStation() == 999);
tv1.setStation(1000); // too large, has no effect!
verify(tv1.getStation() == 999);
// getVolume and setVolume:
tv1.setVolume(0); // min legal value
verify(tv1.getVolume() == 0);
tv1.setVolume(-1); // too small, no effect!
verify(tv1.getVolume() == 0);
tv1.setVolume(11); // too large, no effect
verify(tv1.getVolume() == 0);
// isMuted and setMuted
tv1.setMuted(true);
verify(tv1.isMuted() == true);
tv1.setMuted(false);
verify(tv1.isMuted() == false);
// toString
verify(tv1.toString().equals("tv<station=999,volume=0,muted=false>"));
// second instance, using a different constructor
Television tv2 = new Television(5,6,true); // station, volume, isMuted
verify(tv2.getStation() == 5);
verify(tv2.getVolume() == 6);
verify(tv2.isMuted() == true);
verify(tv2.toString().equals("tv<station=5,volume=6,muted=true>"));
// independence of two instances
verify(tv1.getStation() == 999);
verify(tv2.getStation() == 5);
tv1.setStation(11);
tv2.setStation(22);
verify(tv1.getStation() == 11);
verify(tv2.getStation() == 22);
System.out.println("Passed all tests!!!");
}
//////////////////////////////////
// testCokeMachineClass
//////////////////////////////////
private static void testCokeMachineClass() {
System.out.println("Running CokeMachine class tests...");
// default constructor
CokeMachine cm1 = new CokeMachine();
verify(cm1.getBottleCount() == 100); // default starts with 100 bottles of coke
verify(cm1.isEmpty() == false);
verify(cm1.getBottleCost() == 125); // $1.25 (125 cents) is the default cost for a coke
verify(cm1.getPaidValue() == 0); // default starts with no coins inserted
// toString method
verify(cm1.toString().equals("cokeMachine<bottles=100,cost=$1.25,paidValue=$0.00>"));
// insert a dollar
int change;
change = cm1.insert(100); // we paid $1.00, it costs $1.25, so change == -1
// to indicate that not only is there on change, but
// we still owe money
verify(change == -1);
verify(cm1.stillOwe() == 25);
verify(cm1.toString().equals("cokeMachine<bottles=100,cost=$1.25,paidValue=$1.00>"));
// insert a dime more
change = cm1.insert(10);
verify(change == -1);
verify(cm1.stillOwe() == 15);
verify(cm1.toString().equals("cokeMachine<bottles=100,cost=$1.25,paidValue=$1.10>"));
// and insert a quarter more. Here, we finally pay enough, so we get a bottle
// and some change!
change = cm1.insert(25);
verify(change == 10);
verify(cm1.stillOwe() == 125); // this is for the NEXT bottle
verify(cm1.toString().equals("cokeMachine<bottles=99,cost=$1.25,paidValue=$0.00>"));
verify(cm1.getBottleCount() == 99);
// second instance, with a different constructor
CokeMachine cm2 = new CokeMachine(2,50); // 2 bottles, $0.50 each (and no initial paid value)
verify(cm2.toString().equals("cokeMachine<bottles=2,cost=$0.50,paidValue=$0.00>"));
// buy a couple bottles
change = cm2.insert(25);
verify(change == -1);
verify(cm2.stillOwe() == 25);
change = cm2.insert(25);
verify(change == 0); // bought with exact change
verify(cm2.isEmpty() == false);
verify(cm2.getBottleCount() == 1);
change = cm2.insert(100); // overpaid by $0.50
verify(change == 50);
verify(cm2.isEmpty() == true);
verify(cm2.getBottleCount() == 0);
verify(cm2.toString().equals("cokeMachine<bottles=0,cost=$0.50,paidValue=$0.00>"));
// cannot buy anything more -- the machine is empty.
// this is signified by returning -999 as the change
change = cm2.insert(50);
verify(change == -999);
verify(cm2.isEmpty() == true);
verify(cm2.getBottleCount() == 0);
verify(cm2.toString().equals("cokeMachine<bottles=0,cost=$0.50,paidValue=$0.00>"));
// addBottles method
cm2.addBottles(50);
verify(cm2.isEmpty() == false);
verify(cm2.getBottleCount() == 50);
change = cm2.insert(50);
verify(change == 0);
verify(cm2.toString().equals("cokeMachine<bottles=49,cost=$0.50,paidValue=$0.00>"));
// independence of two instances
verify(cm1.getBottleCount() == 99);
verify(cm2.getBottleCount() == 49);
System.out.println("Passed all tests!!!");
}
}
//////////////////////////////////
// Television class
//////////////////////////////////
class Television {
// ADD CODE HERE!!!
}
//////////////////////////////////
// CokeMachine class
//////////////////////////////////
class CokeMachine {
// ADD CODE HERE!!!
}
Carpe diem!