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

Write a program that reads in lines from the input. For every pair of line, it c

ID: 656411 • Letter: W

Question

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 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 be 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 example above, the output will be

Explanation / Answer

package javaapplication15;
/**
*
* @author Sravan Yadav E
*/
import java.util.Scanner;


public class JavaApplication15
{
        static int nlamps;    
        static int j;
        static boolean status=false;
        String lampname=null;
        static String names[]=new String[10];
        public JavaApplication15(String s)
        {
            for(int i=0;i<nlamps;i++)
            {
            Scanner in=new Scanner(System.in);
            System.out.println("Enter Lamp name");                      
            s=in.nextLine();
            names[i]=s;
            }          
        }      
        public static void turnOn()
        {
            status=true;
            System.out.println(toStringPrint());
          
        }
        public static void turnOff()
        {
            status=false;
            System.out.println(toStringPrint());
        }
        public boolean isOn()
        {
            return true;
        }
        public static String toStringPrint()
        {
            String temp1;
            String temp2;
            if(status==true)
            {
                temp1=names[j]+" is"+" ON";
                return temp1;
            }
            else
            {
                temp2=names[j]+" is"+" OFF";
                return temp2;
            }
              
        }
      

    public static void main(String args[])
    {
      
        String temp3,temp4;      
        int turn=0;
        Scanner in=new Scanner(System.in);
        System.out.println("Enter number of lamps");                      
        temp3=in.nextLine();
        nlamps=Integer.parseInt(temp3);
        JavaApplication15 ob=new JavaApplication15(null);
        for(j=0;j<nlamps;j++)
        {
            System.out.println("Enter status");
            temp4=in.nextLine();
            turn=Integer.parseInt(temp4);
            if(turn==0)
            {
                turnOff();              
            }
              
            else
            {
                turnOn();
            }
              
              
        }
      
    }
}

Output:

Enter number of lamps
2
Enter Lamp name
Green
Enter Lamp name
Blue
Enter status
0
Green is OFF
Enter status
1
Blue is ON

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