Quiz J5-1 | CS 2113 Software Engineering - Spring 2021

Quiz J5-1

Submit your answers via this Google Form

Question #1

Consider the House and HouseTask below:

import java.util.*;

class House {
    public boolean lightsOn = false;
    public int temperature = 65;

    public House(boolean lightsOn, int temperature) {
        this.lightsOn = lightsOn;
        this.temperature = temperature;
    }
}

class HouseTask extends Thread {
    private House house;
    private int task;

    public HouseTask(House house, int task) {
        this.house = house;
        this.task = task;
    }

    public void run() {
        switch (task) {
            case 1:
                house.lightsOn = !house.lightsOn;
                break;
            case 2:
                // takes awhile to heat the house
                try{
                    Thread.sleep(500);
                }catch(Exception e){
                    //...
                }
                house.temperature++;
                break;
            case 3:
                // AC works great though!
                try {
                    Thread.sleep(250);
                } catch (Exception e) {
                    // ...
                }
                house.temperature--;
                break;
        }
    }
}

What is the output of the program below?

    public static void main(final String args[]) {
        House myHouse = new House(false, 65);
        HouseTask dadsTask = new HouseTask(myHouse, 3);
        dadsTask.start();
        try {
            Thread.sleep(500);
        } catch (Exception e) {
            // ...
        }
        myHouse.temperature--;
        System.out.println("Temp: "+myHouse.temperature);

    }

Question #2

Using the same classes from question 1, what is the output of the program below:

    public static void main(final String args[]) {
        House myHouse = new House(false, 65);
        HouseTask dadsTask = new HouseTask(myHouse, 3);
        dadsTask.start();
        dadsTask.start();
        dadsTask.start();
        try {
            Thread.sleep(700);
            dadsTask.join();
        } catch (Exception e) {
            // ...
        }
        myHouse.temperature--;
        System.out.println("Temp: "+myHouse.temperature);

    }

Question #3

Using the same classes from question 1, what is the output of the program below:

    public static void main(final String args[]) {
        House myHouse = new House(false, 65);
        HoustTask tasks[] = new HouseTask[3];
        for (int i = 0; i < 3; i++){
            tasks[i] = new HouseTask(myHouse, 2);
            tasks[i].start();
        }
        try{
           task[0].join();
           task[1].join();
           task[2].join();
        }catch{Exception e}{ /*...*/}
        
        System.out.println("Temp: "+myHouse.temperature);

    }

Question #4

Using the same classes from question 1, what is the output of the program below:

    public static void main(final String args[]) {
        House myHouse = new House(false, 65);
        HouseTask lightTask = new HouseTask(myHouse, 1);
        HouseTask momsTask;
        
        lightTask.start();
        while(lightTask.isAlive());
        
        if (myHouse.lightsOn) {
            momsTask = new HouseTask(myHouse, 2);
        } else {
            momsTask = new HouseTask(myHouse, 3);
        }
        try {
            Thread.sleep(400);
        } catch (Exception e) {
            // ...
        }
        System.out.println("Temp: " + myHouse.temperature);
    }