Freezer class (Freezer.java and FreezerTester.java) Design a class named Freezer
ID: 3711389 • Letter: F
Question
Freezer class (Freezer.java and FreezerTester.java) Design a class named Freezer to represent a freezer to store frozen food. This class contains: Three constants named SLOW, MEDIUM, and FAST with the value 1 ,2, and 3 to denote the freezing speed. A private int data field named temperature in Fahrenheit (between 0 and 32) that specifies the temperature of the freezing temperature (the default is 0). A private boolean data field named on that specifies whether the freezer is on(the default is false). A private double data field named size (in cubit feet) that specifies the size of the freezer (default is 3.0). A string data field named color that specifies the color of the freezer (the default is black). The accessor and mutator methods for all four data fields. A no-arg constsructor that crates a default freezer. A method name toString() that returns a string description for the freezer. If the freezer is on, the method returns the freezer’s freezing speed, color, and size in one combined string. If the freezer is not on, the method returns the freezer color and size along with the “The freezer is currently off” in one combined string. Write a test program that creates two Freezer objects. Assign maximum speed, size 28.5 cu.ft, color red, and turn it on to the first object. Assign medium speed, size 19.5 cu.ft., color StainlessSteel, and turn it off to the second object. Display the object by invoking their toString method.
Explanation / Answer
Freezer.Java
===========
public class Freezer {
public static final int SLOW = 1;
public static final int MEDIUM = 2;
public static final int FAST = 3;
private int speed;
private int temp = 0;
private boolean freezerOn = false;
private double size = 3.0;
private String color = "black";
/**
* @return the speed
*/
public String getSpeed() {
return (speed==FAST?"FAST":(speed==MEDIUM?"MEDIUM":"SLOW"));
}
/**
* @param speed the speed to set
*/
public void setSpeed(int speed) {
this.speed = speed;
}
/**
* @return the temp
*/
public int getTemp() {
return temp;
}
/**
* @param temp the temp to set
*/
public void setTemp(int temp) {
this.temp = temp;
}
/**
* @return the freezerOn
*/
public boolean isFreezerOn() {
return freezerOn;
}
/**
* @param freezerOn the freezerOn to set
*/
public void setFreezerOn(boolean freezerOn) {
this.freezerOn = freezerOn;
}
/**
* @return the size
*/
public double getSize() {
return size;
}
/**
* @param size the size to set
*/
public void setSize(double size) {
this.size = size;
}
/**
* @return the color
*/
public String getColor() {
return color;
}
/**
* @param color the color to set
*/
public void setColor(String color) {
this.color = color;
}
/* (non-Javadoc)
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
String freezerStr = "";
if(this.isFreezerOn()){
freezerStr="Freezing Speed = "+this.getSpeed()+", Color = "+color+", Size="+this.getSize()+" cu.ft.";
}else{
freezerStr="Color = "+this.getColor()+", Size="+this.getSize()+" cu.ft.. The freezer is currently off";;
}
return freezerStr;
}
}
FreezerTester.java
===============
package apr192018;
public class FreezerTester {
public static void main(String[] args) {
Freezer freezer1 = new Freezer();
Freezer freezer2 = new Freezer();
freezer1.setSpeed(Freezer.FAST);
freezer1.setSize(28.5);
freezer1.setColor("red");
freezer1.setFreezerOn(true);
freezer2.setSpeed(Freezer.MEDIUM);
freezer2.setSize(19.5);
freezer2.setColor("Stainless Steel");
freezer2.setFreezerOn(false);
System.out.println("The first freezer:"+freezer1.toString());
System.out.println("The second freezer:"+freezer2.toString());
}
}
OUTPUT
=======
The first freezer:Freezing Speed = FAST, Color = red, Size=28.5 cu.ft.
The second freezer:Color = Stainless Steel, Size=19.5 cu.ft.. The freezer is currently off
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.