A microwave control panel has four buttons: one for increasing the time by 30 se
ID: 3870347 • Letter: A
Question
A microwave control panel has four buttons: one for increasing the time by 30 seconds, one for switching between power levels 1 and 2, a reset button, and a start button. Implement a class that simulates the microwave, with a method for each button. The method for the start button should print a message “Cooking for ... seconds at level ...”. Include instance variables for time and power level. The skeleton for the Microwave class is provided. Complete the methods.
its gotta have these methods, thanks
public class Microwave {
public Microwave()
{
//your code here
}
public void increaseTime()
{
//your code here
}
public void switchPower()
{
//your code here
}
public void reset()
{
//your code here
}
public void start()
{
//your code here
}
Explanation / Answer
// Microwave.java
public class Microwave {
// Add a field representing the time
int time;
// Add a field representing the power level
int level;
/**
* Creates a microwave with 0 seconds on the timer and at low power.
*/
public Microwave()
{
// microwave initialization
level = 1;
time = 0;
}
/**
* Increases the time on the timer by 30 seconds.
*/
public void increaseTime()
{
// increase time by 30 secs
time += 30;
}
/**
* Switches the power level from low to high, or vice versa.
*/
public void switchPower()
{
// switch levels
if(level == 2)
level = 1;
if(level == 1)
level = 2;
}
/**
* Resets the microwave to its initial state.
*/
public void reset()
{
// to reset set time to 0 and level to 1
level = 1;
time = 0;
}
/**
* Starts the microwave.
*/
public void start()
{
System.out.println("Cooking for " + time + " seconds at level " + level);
}
}
// MicrowaveTest.java
import static org.junit.Assert.*;
import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import org.junit.Before;
import org.junit.Test;
public class MicrowaveTest
{
private Microwave microwave;
private final ByteArrayOutputStream outStream = new ByteArrayOutputStream();
@Before
public void setUp()
{
microwave = new Microwave();
}
@Test
public void testStart()
{
microwave.start();
System.out.println(outStream.toString().trim());
assertEquals("Cooking for 0 seconds at level 1", outStream.toString().trim());
}
@Test
public void testIncreaseTime1()
{
microwave.increaseTime();
microwave.start();
assertEquals("Cooking for 30 seconds at level 1", outStream.toString().trim());
}
@Test
public void testIncreaseTime2()
{
microwave.increaseTime();
microwave.increaseTime();
microwave.increaseTime();
microwave.start();
assertEquals("Cooking for 90 seconds at level 1", outStream.toString().trim());
}
@Test
public void testSwitchPower1()
{
microwave.increaseTime();
microwave.increaseTime();
microwave.switchPower();
microwave.start();
assertEquals("Cooking for 60 seconds at level 2", outStream.toString().trim());
}
@Test
public void testSwitchPower2()
{
microwave.switchPower();
microwave.increaseTime();
microwave.increaseTime();
microwave.increaseTime();
microwave.increaseTime();
microwave.switchPower();
microwave.start();
assertEquals("Cooking for 120 seconds at level 1", outStream.toString().trim());
}
@Test
public void testReset()
{
microwave.switchPower();
microwave.increaseTime();
microwave.increaseTime();
microwave.increaseTime();
microwave.increaseTime();
microwave.switchPower();
microwave.reset();
microwave.start();
assertEquals("Cooking for 0 seconds at level 1", outStream.toString().trim());
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.