Using the following Java Class.... public class MicroWave { // Represent possibl
ID: 3826816 • Letter: U
Question
Using the following Java Class....
public class MicroWave {
// Represent possible microwave heat selections
private final int LOW = 1;
private final int MEDIUM = 2;
private final int HIGH = 3;
// specifies the microwave heat selection(default is MEDIUM)
private int heatSelection;
// specifies whether the microwave is on
private boolean on;
private String color;
public MicroWave(int heatSelection, boolean on, String color) {
this.heatSelection = heatSelection;
this.on = on;
this.color = color;
}
public MicroWave() {
this.heatSelection = MEDIUM;
this.on = false;
this.color = "black";
}
public int getHeatSelection() {
return heatSelection;
}
public void setHeatSelection(int heatSelection) {
this.heatSelection = heatSelection;
}
public boolean isOn() {
return on;
}
public void setOn(boolean on) {
this.on = on;
}
public String getColor() {
return color;
}
@Override
public String toString() {
String result = "MicroWave color=" + color + " heat selection: ";
if(heatSelection == LOW)
{
result += "LOW";
}
else if(heatSelection == MEDIUM)
{
result += "MEDIUM";
}
else
{
result += "HIGH";
}
return result;
}
}
create a test class that does the following:
1. Creates four instances of a MicroWave:
(a) A blue microwave that is turned "on" and set to "MEDIUM" heat selection.
(b) A green microwave that is turned "off" and set to "LOW" heat selection.
(c) A blue microwave that is turned "on" and set to "HIGH" heat selection.
(d) A microwave created with all default values.
2. Performs an automated test against each Microwave. Test should be placed in a static method with the following signature in the test class.
public static void test(MicroWave[] microWaves)
(a) For simplicity, test method will just iterate over the array of MicroWaves and log a text nessage to a file named "test.log" for each microwave test. Message should consist of the toString() of the microwave along with phrases "pass" or "fail" indicating the result of the test. Microwave "fails" the test if its off.
Sample log file content:
BLUE MEDIUM PASS
GREEN LOW FAIL
Explanation / Answer
MicroWaveTest.java
import java.io.FileWriter;
import java.io.IOException;
public class MicroWaveTest {
public static void test(MicroWave[] microWaves) throws IOException
{
FileWriter fr = new FileWriter("test.log");
for(int i = 0; i < microWaves.length; i++)
{
if(! microWaves[i].isOn())
{
System.out.println(microWaves[i] + " FAIL");
}
else
{
System.out.println(microWaves[i] + " PASS");
}
}
}
public static void main(String[] args) throws IOException
{
MicroWave blue = new MicroWave(2, true, "BLUE");
MicroWave green = new MicroWave(1, false, "GREEN");
MicroWave blue2 = new MicroWave(3, true, "BLUE");
MicroWave def = new MicroWave();
MicroWave[] microwaves = {blue, green, blue2, def};
test(microwaves);
}
}
MicroWave.java
public class MicroWave {
// Represent possible microwave heat selections
private final int LOW = 1;
private final int MEDIUM = 2;
private final int HIGH = 3;
// specifies the microwave heat selection(default is MEDIUM)
private int heatSelection;
// specifies whether the microwave is on
private boolean on;
private String color;
public MicroWave(int heatSelection, boolean on, String color) {
this.heatSelection = heatSelection;
this.on = on;
this.color = color;
}
public MicroWave() {
this.heatSelection = MEDIUM;
this.on = false;
this.color = "BLACK";
}
public int getHeatSelection() {
return heatSelection;
}
public void setHeatSelection(int heatSelection) {
this.heatSelection = heatSelection;
}
public boolean isOn() {
return on;
}
public void setOn(boolean on) {
this.on = on;
}
public String getColor() {
return color;
}
@Override
public String toString() {
String result = color + " ";
if(heatSelection == LOW)
{
result += "LOW";
}
else if(heatSelection == MEDIUM)
{
result += "MEDIUM";
}
else
{
result += "HIGH";
}
return result;
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.