I’m looking to improve the following java class in order to have something post for each Test Case but I keep running into more bugs than solutions.
I’m looking to improve the following java class in order to have something
post for each Test Case but I keep running into more bugs than solutions.
public class StandMixer {
// Define static class variable
// Make’s sure bowl is lifted up
private boolean bowlUp;
// Define class variable
// Mixer speed
private int speedSetting;
// Constructors
// Default constructor
public StandMixer() {
this.bowlUp = false;
this.speedSetting = 0;
}
// bowlPosition Method
public void bowlPosition(boolean up) {
// You can only change the position of the
// bowl when the mixer is off
if (this.speedSetting == 0) {
this.bowlUp = up;
} else {
System.out.println(“You can’t change the position of the bowl while the mixer is on.”);
}
}
// changeSpeed Method
public void changeSpeed(int speed) {
switch (speed) {
// If user wishes to turn off mixer
case 0:
this.speedSetting = speed;
System.out.println(“The mixer is currently off.”);
break;
// If user wishes to set mixer on low speed
case 1:
if (this.bowlUp) {
this.speedSetting = speed;
System.out.println(“You are mixing the ingredients on low speed.”);
} else {
System.out.println(“You cannot change the speed while the bowl is down.”);
}
break;
// If user wishes to set mixer on medium speed
case 2:
if (this.bowlUp) {
this.speedSetting = speed;
System.out.println(“You are mixing the ingredients on medium speed.”);
} else {
System.out.println(“You cannot change the speed while the bowl is down.”);
}
break;
// If user wishes to set mixer on high speed
case 3:
if (this.bowlUp) {
this.speedSetting = speed;
System.out.println(“You are mixing the ingredients on high speed.”);
} else {
System.out.println(“You cannot change the speed while the bowl is down.”);
}
break;
default:
System.out.println(“You have entered an invalid ” +
“speed setting for this mixer.”);
System.out.println(“Please select a speed 0-3.”);
}
}
The following is the Test code
public class TestStandMixer {
public static void main(String[] args) {
// Constructors
StandMixer myMixer_1 = new StandMixer();
StandMixer myMixer_2 = new StandMixer();
StandMixer myMixer_3 = new StandMixer();
StandMixer myMixer_4 = new StandMixer();
StandMixer myMixer_5 = new StandMixer();
StandMixer myMixer_6 = new StandMixer();
StandMixer myMixer_7 = new StandMixer();
// If you try to move the bowl up while the
// mixer is off, the bowl will move up
System.out.println(“Test Case 1:”);
myMixer_1.bowlPosition(true);
// If you try to put the bowl down while
// the mixer is off, the bowl will move down
System.out.println(“\nTest Case 2:”);
myMixer_2.bowlPosition(true);
myMixer_2.bowlPosition(false);
// If you try to put the bowl down while the
// mixer is on, you will get an error message
System.out.println(“\nTest Case 3:”);
myMixer_3.bowlPosition(true);
myMixer_3.changeSpeed(2);
myMixer_3.bowlPosition(false);
// If you try to turn the mixer on while the bowl
// is up, it will turn on
System.out.println(“\nTest Case 4:”);
myMixer_4.bowlPosition(true);
myMixer_4.changeSpeed(3);
// If you try to turn the mixer off while the bowl is
// up, it will turn the mixer off
System.out.println(“\nTest Case 5:”);
myMixer_5.bowlPosition(true);
myMixer_5.changeSpeed(1);
myMixer_5.changeSpeed(0);
// If you try to turn the mixer on while the bowl is
// down, you get an error message
System.out.println(“\nTest Case 6:”);
myMixer_6.changeSpeed(2);
// If you tried to choose a setting that does not exist
// for the mixer, you get an error message
System.out.println(“\nTest Case 7:”);
myMixer_7.bowlPosition(true);
myMixer_7.changeSpeed(4);
}
}