Requirements You are required to write a program that simula

Requirements You are required to write a program that simulate a machine operations. The machine comprises of a group of components(switches) as in Diagram 1 below. The operating rules for the machine are: For the machine to work, MAIN SWITCH must be turned on first. If the user attempts to turn on any switch (A or B) before MAIN SWITCH is on, the program will alert the user to turn on the MAIN SWITCH. To turn on SWITCH B, SWITCH A must be turned on. If the user attempts to turn on SWITCH B before SWITCH A is on, the program will alert the user to turn on SWITCH A. Diagram 1. The machine architecture. The sample program output with a few scenarios is given in the following pages. MAIN BOARD SWITCH A SWITCH B MAIN SWITCH Scenario 1: When the machine is operated accordingly by following the operating rules (eg. Turn on the switch in sequence > Main > A > B >end) Machine Status: MAIN SWITCH=0 SWITCH A=0 SWITCH B=0 Machine Main Menu 1.MAIN SWITCH 2.SWITCH A 3.SWITCH B 4.END Enter your choice (1-3) to on the switch, 4 to STOP: 1 Machine Status: MAIN SWITCH=1 SWITCH A=0 SWITCH B=0 Machine Main Menu 1.MAIN SWITCH 2.SWITCH A 3.SWITCH B 4.END Enter your choice (1-3) to on the switch, 4 to STOP: 2 Machine Status: MAIN SWITCH=1 SWITCH A=1 SWITCH B=0 Machine Main Menu 1.MAIN SWITCH 2.SWITCH A 3.SWITCH B 4.END Enter your choice (1-3) to on the switch, 4 to STOP: 3 Machine Status: MAIN SWITCH=1 SWITCH A=1 SWITCH B=1 Machine Main Menu 1.MAIN SWITCH 2.SWITCH A 3.SWITCH B 4.END Enter your choice (1-3) to on the switch, 4 to STOP: 4 Machine Stopped! Press any key to continue . . . Scenario 2: When the user is trying to turn on SWITCH A or B without prior turn on the MAIN SWITCH Machine Status: MAIN SWITCH=0 SWITCH A=0 SWITCH B=0 Machine Main Menu 1.MAIN SWITCH 2.SWITCH A 3.SWITCH B 4.END Enter your choice (1-3) to on the switch, 4 to STOP: 2 ON MAIN SWITCH FIRST! Machine Status: MAIN SWITCH=0 SWITCH A=0 SWITCH B=0 Machine Main Menu 1.MAIN SWITCH 2.SWITCH A 3.SWITCH B 4.END Enter your choice (1-3) to on the switch, 4 to STOP: 3 ON MAIN SWITCH FIRST! ………………………………. ………………………………. ………………………………. The rest of the outputs Scenario 3: When the user has turned on MAIN SWITCH and is trying to turn on SWITCH B without prior turn on SWTCH A. Machine Status: MAIN SWITCH=0 SWITCH A=0 SWITCH B=0 Machine Main Menu 1.MAIN SWITCH 2.SWITCH A 3.SWITCH B 4.END Enter your choice (1-3) to on the switch, 4 to STOP: 1 Machine Status: MAIN SWITCH=1 SWITCH A=0 SWITCH B=0 Machine Main Menu 1.MAIN SWITCH 2.SWITCH A 3.SWITCH B 4.END Enter your choice (1-3) to on the switch, 4 to STOP: 3 ON SWITCH A FIRST! ………………………………. ………………………………. ……………………………….

Solution

import java.util.Scanner ;
public class Machine{
public static void main(String args[]){
   int m=0;
   int a=0;
   int b=0;
   System.out.println(\"Turn on the switch in sequence > Main > A > B >end.\ \" +
           \" Machine Status: MAIN SWITCH=0 SWITCH A=0 SWITCH B=0\ \" +
           \" Machine Main Menu 1.MAIN SWITCH 2.SWITCH A 3.SWITCH B 4.END\ \" +
           \"Enter your choice (1-3) to on the switch, 4 to STOP:\");
   try{
while (true)
{
   Scanner sc=new Scanner(System.in);
   int num=sc.nextInt();
if (num==1)
{if(m==0){m=1;a=0;b=0;System.out.println(\"Machine Status: MAIN SWITCH=\"+m+\" SWITCH A=\"+a+\" SWITCH B=\"+b);}
else if(m==1){System.out.println(\"Machine Status: MAIN SWITCH=\"+m+\" SWITCH A=\"+a+\" SWITCH B=\"+b);}
}  
else if (num==2)
{if(m==0){System.out.println(\"ON MAIN SWITCH FIRST!\");}
else if(m==1){m=1;a=1;b=0;System.out.println(\"Machine Status: MAIN SWITCH=\"+m+\" SWITCH A=\"+a+\" SWITCH B=\"+b);}
   System.out.println(\"a\");}
else if(num==3)
{if(m==0){System.out.println(\"ON MAIN SWITCH FIRST!\");}
else if(m==1){
   if(a==0)System.out.println(\"ON SWITCH A FIRST!\");
   else if(a==1)
           {
       m=1;a=1;b=1;System.out.println(\"Machine Status: MAIN SWITCH=\"+m+\" SWITCH A=\"+a+\" SWITCH B=\"+b);
           }
}}
else if(num==4){m=0;a=0;b=0;System.out.println(\"Machine Stopped! Press any key to continue . . .\");}
else{System.out.println(\"Turn on the switch in sequence > Main > A > B >end.\ \" +
       \" Machine Main Menu 1.MAIN SWITCH 2.SWITCH A 3.SWITCH B 4.END\ \ \" +
       \"Enter your choice (1-3) to on the switch, 4 to STOP:\");}
}}
   catch(Exception e){System.out.println(\"Invalid Input. Restart Machine\");}
}}

Requirements You are required to write a program that simulate a machine operations. The machine comprises of a group of components(switches) as in Diagram 1 be
Requirements You are required to write a program that simulate a machine operations. The machine comprises of a group of components(switches) as in Diagram 1 be

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site