Lab7 Question 2
Java Code for SetTime use case of watch:
public class Watch {
private WatchState state = new MeasureTime();
public void pressButtonLAndR(){ state.pressButtonLAndR(); }
public void After2Minutes(){ state.After2Minutes();}
public void After20Years(){ state.After20Years(); }
public void pressButtonL(){ state.pressButtonL();}
public void pressButtonR(){state.pressButtonR();}
private abstract class WatchState
{
public WatchState() {}
public void pressButtonLAndR()
{
System.out.println ( "Error: Cannot change mode.");
}
public void pressButtonL()
{
System.out.println( "Error: Cannot change mode.");
}
public void pressButtonR()
{
System.out.println( "Error: Cannot change mode.");
}
public void After2Minutes()
{
System.out.println ("Error: Cannot change mode.");
}
public void After20Years()
{
System.out.println ("Error: Cannot change mode.");
}
}
public class MeasureTime extends WatchState
{
public void pressButtonLAndR() { state = new SetTime(); }
public void After20Years() { state = new DeadBattery(); }
public void pressButtonL() { state = new MeasureTime(); }
public void pressButtonR() { state = new MeasureTime(); }
}
}// class Watch
///////////////////////////////////////////////////////////////////////////////////////////////////////////////
public class SetTime extends WatchState
{
public void pressButtonLAndR()
{
state = new MeasureTime();
System.out.println ( "beep!");
}
public void pressButtonL() { state = new SetTime();}
public void pressButtonR() { state = new SetTime();}
public void After2Minutes() { state = new MeasureTime(); }
public void After20Years() { state = new DeadBattery(); }
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////
public class DeadBattery extends WatchState
{
DeadBattery()
{
System.out.println ( "The battery is dead!");
}
}
Equivalence Testing:
class Watch
pressButtonLAndR(){}
Equivalence Class |
Value input |
Value output |
Press both button |
Press both L and R |
State = new SetTime(); |
After2Minutes(){}
Equivalence Class |
Value input |
Value output |
Wait 2 minutes |
nothing |
Measure after wait (state.After2Minutes()) |
Abstract class WatchState
After20Years(){}
Equivalence Class |
Value input |
Value output |
Wait 20 years |
nothing |
Measure after wait (state = new DeadBattery()) |
class MeasureTime extends WatchState
pressButtonLAndR(){}
Equivalence Class |
Value input |
Value output |
Press both button |
Press both L and R |
Set Time State (state = new SetTime) |
class SetTime extends WatchState
After20Years(){}
Equivalence Class |
Value input |
Value output |
Wait |
Nothing |
Measure after wait |
class DeadBattery extends WatchState
DeadBattery(){}
Equivalence Class |
Value input |
Value output |
Wait |
Nothing |
String of text |
Boundary Testing:
class Watch
pressButtonLAndR(){}
Equivalence Class |
Value input |
Value output |
Did not Press both buttons |
Either R or L |
Always stay at MeasureTime state, can never go to SetTime state. |
DeadBattery(){}
Equivalence Class |
Value input |
Value output |
Battery went dead |
nothing |
nothing |
Path Testing:
Test Case |
Path |
{button not press} |
{nothing} |
{button L pressed} |
{Measure Time} |
{button R pressed} |
{Measure Time} |
{button both pressed} |
{SetTime } |
{Do nothing, wait 10 year} |
{DeadBattery} |