by CodeChum Admin
Construct a class to represent an air conditioning (AC) unit containing the following attributes:
More details here, Important!
https://pastebin.com/yx0z0Tfr
import java.util.Scanner;
class AC {
// brand - name of manufacturers like Samsung, LG, Carrier, etc.
private String brand;
private boolean type;
// power - represents the status of the AC unit whether it is turned on or not
private boolean power;
// thermostat - levels go from 1 to 10
private int thermostat;
// temperature - levels go from 16 to 30 (in .5 intervals)
private double temperature;
/**
* default constructor sets all the numerical values to the least value as
* described above sets power to off, and type to inverter sets brand to "AC
* Brand" prints "Default Constructor"
*/
public AC() {
System.out.println("Default Constructor");
this.brand = "AC Brand";
this.power = false;
this.type = true;
this.thermostat = 1;
this.temperature = 16;
}
/**
* overloaded constructor accepts brand and type as arguments everything else is
* set to the same values as in the default constructor prints "Overloaded
* Constructor"
*
* @param brand
* @param type
*/
public AC(String brand, boolean type) {
System.out.println("Overloaded Constructor");
this.brand = brand;
this.type = type;
}
/**
* power() - this is supposed to turn the AC on if it is off, and turn it off if
* it is on
*/
public void power() {
this.power = !this.power;
}
/**
* thermostatUp() - increases thermostat by a value 1 (note that the thermostat
* has an upper limit)
*/
public void thermostatUp() {
if (this.thermostat < 10) {
this.thermostat++;
}
}
/**
* thermostatDown() - decreases thermostat by a value 1 (note that the
* thermostat has a lower limit)
*/
public void thermostatDown() {
if (this.thermostat > 0) {
this.thermostat--;
}
}
/**
* temperatureUp() - increases temperature by a value 0.5 (note that the
* temperature has an upper limit)
*/
public void temperatureUp() {
if (this.temperature < 30.0) {
this.temperature += 0.5;
}
}
/**
* temperatureDown() - decreases temperature by a value 1 (note that the
* temperature has a lower limit)
*/
public void temperatureDown() {
if (this.temperature > 16.0) {
this.temperature -= 0.5;
}
}
public String getBrand() {
return this.brand;
}
public boolean getType() {
return this.type;
}
/**
* display() - displays all the values of the member data (one member data per
* line) of AC following this format: <member_data>: <member_data_value>
*/
public void display() {
System.out.println("Brand: " + this.brand);
System.out.println("Type: " + this.type);
System.out.println("Power status: " + this.power);
System.out.println("Thermostat: " + this.thermostat);
System.out.println("Temperature: " + this.temperature);
}
/**
* @return the power
*/
public boolean isPower() {
return power;
}
/**
* @return the thermostat
*/
public int getThermostat() {
return thermostat;
}
/**
* @return the temperature
*/
public double getTemperature() {
return temperature;
}
}
class App {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// The first input is an integer which will create an instance of an AC unit
// depending on the constructor used.
AC unit = null;
if (scanner.nextInt() == 1) {
unit = new AC();
} else {
unit = new AC("BrandName", true);
}
// a number m is encountered which represents the number of operations that have
// to be invoked. This is then followed by an integer representing what operator
// to execute.
unit.display();
int m = scanner.nextInt();
for (int i = 0; i < m; i++) {
int input = scanner.nextInt();
switch (input) {
case 3:
unit.power();
break;
case 4:
unit.thermostatUp();
break;
case 5:
unit.thermostatDown();
break;
case 6:
unit.temperatureUp();
break;
case 7:
unit.temperatureDown();
break;
case 8:
System.out.println("Brand: " + unit.getBrand());
break;
case 9:
System.out.println("Type: " + unit.getType());
break;
case 10:
System.out.println("Power status: " + unit.isPower());
break;
case 11:
System.out.println("Thermostat: " + unit.getThermostat());
break;
case 12:
System.out.println("Temperature: " + unit.getTemperature());
break;
case 13:
unit.display();
break;
}
}
}
}
Comments
Leave a comment