Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Java Objective Write a program that reads in lines from the input. For every pai

ID: 3825876 • Letter: J

Question

Java

Objective

Write a program that reads in lines from the input.

For every pair of line, it creates Lamp objects and operates on them as per instructions in the lines. Then, it prints out information on the status of each Lamp.

The important part of this assignment is to make use of the Lamp class. Details are given after an explanation of what the program is to do.

Input

The input will be one or more PAIRS of lines.

Each pair of lines will have on the first line names of lights and on the second line will have the states of the lines. The first line will have an integer followed by that many strings consisting of names of colors.

Example: 2 green gray The second line will have for each lamp a 0 or 1 . A 0 will signify that the lamp is to be turned OFF and a 1 will indicate that the particular lamp is to turned ON.

Example: 2 green gray followed by 1 1 will indicate that both the green and the gray lamps are to be turned on.

Output

Each pair of input lines will be followed by ONE line of output indicating the status of the lamps described in the input. In the above example, the output will be "green is ON gray is ON".

Sample Input

2 green gray

1 1

1 orange

0

Sample Output

green is ON gray is ON

orange is OFF

Now that you know what this program is to do, let us get into more details of what you need to do.

You will need to write code for TWO classes ( a Lamp class and a Main class).

Lamp class

1. The Lamp class should have TWO instance variables a boolean variable to indicate the status of the Lamp a String variable that will indicate the name of the Lamp

2. The Lamp class should have FIVE methods

a. a constructor public Lamp(String s) that will set the name of the Lamp to the String s.

b. public void turnOn() that will set the status of the Lamp to true.

c. public void turnOff() that will set the status of the Lamp to false.

d. public boolean isOn() that will return the value of the status

e. public String toString() that will return a String consisting of name + " is " + "ON" (if the Lamp's status is true) OR name + " is " + "OFF" (if the Lamp's status is false)

Main class

The Main class should have the main method (public static void main(String [] args) {})

The main method should keep reading lines

1. // process the FIRST line of the pair a. it should read in the number of lamps b. then it should read in the names of the lamps, create a new Lamp, and store the lamps in an array.

2. // process the second line of the pair a. Next, it should read in an int from the line and take a turnOn or turnOff action on the particular lamp (which is in the array). It should do this for all the lamps.

3. // print the output a. traverse the lamp array and print each lamp (use System.out.print(lamp + " "); )

4. remember to print a final println after printing the lamps.

Thank you!

Explanation / Answer

import java.io.BufferedReader;

import java.io.FileNotFoundException;

import java.io.IOException;

import java.io.InputStreamReader;

/**

*

* @author Sam

*/

public class LampDriver {

    public static void main(String[] args) throws FileNotFoundException, IOException {

       Lamp[] lamps;

       BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); //this will read from keyboard

       String line1, line2;

       line1 = br.readLine(); //read first line containing size and name

       line2 = br.readLine(); //read 2nd line consisting of states

       while (line1 != null){

           String tokens1[] = line1.split(" ");

           String tokens2[] = line2.split(" ");

           lamps = new Lamp[Integer.parseInt(tokens1[0].trim())];

           for (int i = 1; i<tokens1.length; i++){

               Lamp tmp = new Lamp(tokens1[i]); //create a lamp with given name

               if (tokens2[i-1].equals("1")) //then set state

                   tmp.turnOn();

               else //else part is useless, since the constructor sets the state to off in lamp class

                   tmp.turnOff();

               lamps[i-1] = tmp;;

           }

           for (Lamp lamp:lamps)

              System.out.println(lamp);

       

           line1 = br.readLine();

           line2 = br.readLine();

       }

   

    }

}


class Lamp {

    private String name;

    private boolean on;

    public Lamp(String name) {

       this.name = name;

       on = false;

    }

    public boolean isOn() {

       return on;

    }

    public void turnOn() {

       on = true;

    }

    public void turnOff() {

       on = false;

    }

    @Override

    public String toString() {

       return name + " is " + (on?"ON":"OFF") ;

    }

  

}



Hey champ! I tried my best to keep the code as simple as possible. I have also commented the code wherever required. If incase you are facing any problem with the code, please feel free to comment below. I shall be glad to help you.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote