Answer to Question #287919 in Java | JSP | JSF for Angge

Question #287919

Construct a class to represent an air conditioning (AC) unit containing the following attributes

Input

The first input is an integer which will create an instance of an AC unit depending on the constructor used. This is specified below:


1 - construct an AC unit object via the default constructor (no

input needed)

2 - construct an AC unit object via the overloaded constructor

(a String for the brand followed by 1 (inverter type) or 0

(non-inverter type))


Then 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. The operators are specified below:


3 - calls power()

4 - calls thermostatUp()

5 - calls thermostatDown()

6 - calls temperatureUp()

7 - calls temperatureDown()

8 - calls getBrand()

9 - calls getType()

10 - calls getPower()

11 - calls getThermostat()

12 - calls getTemperature()

13 - calls display()

1
4
14
3
4
13
14







1
Expert's answer
2022-01-16T17:51:21-0500
import java.util.Scanner;

class Main {

    private static AC unit;

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        if (scanner.nextInt() == 1) {
            unit = new AC();
        } else {
            unit = new AC("BrandName", 1);
        }

        int m = scanner.nextInt();
        for (int i = 0; i < m; i++) {
            doAction(scanner.nextInt());
        }
    }

    private static void doAction(int input) {
        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(unit.getBrand());
                break;
            case 9:
                System.out.println(unit.getType());
                break;
            case 10:
                unit.getPower();
                break;
            case 11:
                unit.getThermostat();
                break;
            case 12:
                unit.getTemperature();
                break;
            case 13:
                unit.display();
                break;
        }
    }
}

class AC {

    private final String brand;
    private final int type;

    public AC() {
        this.brand = "Default";
        this.type = 0;
    }

    public AC(String brand, int type) {
        this.brand = brand;
        this.type = type;
    }

    public void power() {
        System.out.println("power() was invoked");
    }

    public void thermostatUp() {
        System.out.println("thermostatUp() was invoked");
    }

    public void thermostatDown() {
        System.out.println("thermostatDown() was invoked");
    }

    public void temperatureUp() {
        System.out.println("temperatureUp() was invoked");
    }

    public void temperatureDown() {
        System.out.println("temperatureDown() was invoked");
    }

    public String getBrand() {
        return this.brand;
    }

    public int getType() {
        return this.type;
    }

    public void getPower() {
        System.out.println("getPower() was invoked");
    }

    public void getThermostat() {
        System.out.println("getThermostat() was invoked");
    }

    public void getTemperature() {
        System.out.println("getTemperature() was invoked");
    }

    public void display() {
        System.out.println("display() was invoked");
    }
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog