371,899 views
43 votes
43 votes
Finish writing the Oven class that represents an oven. This class has the variables, constructors and methods detailed below. You should write code implementing the methods and constructors so they behave as described.

Variables

private int maxTemp - the maximum temperature of the oven. This value should not be changed after the oven is constructed. If the temperature is greater than 500 or less than 0, it should be changed to 500.
private int currentTemp - the current temperature of the oven. Should not be greater than maxTemp or less than 0. If currentTemp is greater than maxTemp, it should be set to maxTemp. If currentTemp is less than 0, it should be set to 0.
Constructors

public Oven(int maxTemperature, int startTemperature) - constructs and an object of type Oven with the given maxTemperature and startTemperature.
Methods

public int getMaxTemp() - returns the maxTemp of the oven.
public int getCurrentTemp()- returns the currentTemp of the oven.
public void turnOff() - sets the currentTemp of the oven to 0 if the currentTemp of the oven is greater than 0.
public boolean isOn() - return true if currentTemp of the oven is greater than 0.
public void preheat(int temp) - sets currentTemp of the oven to temp. If temp is greater than maxTemp, then set currentTemp to maxTemp. If temp is less than or equal to 0, do nothing.
To test your code before submission, use the file runner_Oven.java. Run the main method in the runner_Oven class and verify that the program produces the expected output. A sample run is shown below, but you should also try a range of other inputs to test your code more thoroughly.

If the runner file does not compile then you should attempt to fix your Oven class before checking your code for a score. Start by checking that the signatures and return types of your Oven methods match the descriptions exactly. You should also not add a main method to your Oven class, since you may not be able to run your code, and it will not be scored correctly when you check it.

Sample run
Maximum oven temperature:
450
Starting temperature of the oven:
70
New oven with a maximum temperature of 450 and a starting temperature of 70 degrees.
To preheat the oven enter "p", to turn the oven off enter "o", to restart enter "r", to quit enter "q"
p
Enter the temperature to preheat the oven to:
350
Current temperature of the oven is now 350 degrees

New oven with a maximum temperature of 450 and a starting temperature of 350 degrees.
To preheat the oven enter "p", to turn the oven off enter "o", to restart enter "r", to quit enter "q"
o
Turning the oven off.

New oven with a maximum temperature of 450 and a starting temperature of 0 degrees.
To preheat the oven enter "p", to turn the oven off enter "o", to restart enter "r", to quit enter "q"
q
Hint - Start by writing the 3 accessor methods - getMaxTemp, getCurrentTemp and isOn. These methods just need to return the relevant member variables. Once this is done you will be able to compile and run the runner class to test your code. Then you can focus on writing and testing the more difficult remaining methods.

User Atticus
by
2.3k points

2 Answers

23 votes
23 votes

The `Oven` class has private variables `maxTemp` and `currentTemp`, a constructor that initializes these variables within specified constraints, and methods to get the maximum temperature, current temperature, turn off the oven, check if the oven is on, and preheat the oven to a specified temperature.

Here is the implementation of the `Oven` class with the described variables, constructors, and methods:

```java

public class Oven {

private int maxTemp;

private int currentTemp;

public Oven(int maxTemperature, int startTemperature) {

// Initialize maxTemp with a value between 0 and 500

this.maxTemp = (maxTemperature >= 0 && maxTemperature <= 500) ? maxTemperature : 500;

// Initialize currentTemp with a value between 0 and maxTemp

this.currentTemp = (startTemperature >= 0 && startTemperature <= this.maxTemp) ? startTemperature : 0;

}

public int getMaxTemp() {

return maxTemp;

}

public int getCurrentTemp() {

return currentTemp;

}

public void turnOff() {

if (currentTemp > 0) {

currentTemp = 0;

System.out.println("Turning the oven off.");

}

}

public boolean isOn() {

return currentTemp > 0;

}

public void preheat(int temp) {

if (temp > maxTemp) {

currentTemp = maxTemp;

} else if (temp > 0) {

currentTemp = temp;

}

// If temp is less than or equal to 0, do nothing.

}

}

```

User Phonixor
by
2.7k points
23 votes
23 votes

Answer:

public class Elevator{

private int maxFloor; // Represents max possible floor of elevator.

private int currentFloor; // Represents current floor of elevator.

private boolean inService; // // True if elevator in service, false if not.

// Creates an elevator which is in service. If max is greater than 1 sets

// the maximum floor to max, otherwise sets the maximum floor to 2. If start

// is between 1 and the maximum floor inclusive the elevator starts at this

// floor, otherwise it starts at 1.

public Elevator(int max, int start){Explanation:

Finish writing the Oven class that represents an oven. This class has the variables-example-1
User MoustafaAAtta
by
3.5k points