In: Computer Science
Write a program in Java Using object Orientation Design to determine the status of Mini Van Sliding Doors. A logical circuit receives a different binary code to allow opening different doors. The doors can be opened by a dashboard switch, inside or outside handle. The inside handle will not open the door if the child safety lock is on or the master lock is on. The gear shift must be in the park to open the door.
** MUST USE constructors and methods. The methods should be instantiated by an object. Use simple main. The first bit Stream must be entered by users. The program needs to be interactive
Hints & Suggestions
park
door1
door2
dashboardSwitch
inHandle
outHandle
safteyLock
p | d1 | d2 | dw | inh | outh | sLock | desc |
---|---|---|---|---|---|---|---|
1 | 1 | 1 | 1 | 1 | 1 | 0 | Saftey Lock Off, door 1 & 2 open |
1 | 1 | 0 | 1 | 1 | 1 | 0 | |
1 | 0 | 1 | 1 | 1 | 1 | 0 | |
0 | 0 | 0 | 0 | 0 | 0 | 0 | Car not parked, no door works |
1 | 0 | 0 | 0 | 0 | 1 | 1 | Saftey lock on, door only opens from outside |
Please find the code below. Save the file as door.java and change the path of the text file and then run the file door.java .
import java.util.Scanner;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
class Mini_Van_Sliding_Doors
{
boolean child_safety_lock;
boolean master_lock;
String gear_shift;
Mini_Van_Sliding_Doors(boolean child_safety_lock,boolean master_lock, String gear_shift)
{
this.child_safety_lock=child_safety_lock; this.master_lock=master_lock; this.gear_shift=gear_shift;
}
boolean inside_handle()
{
if (child_safety_lock == true || master_lock == true )
{
return false;
}
else
{
return true;
}
}
boolean outside_handle()
{
return true;
}
boolean gear_shift_status()
{
if ( gear_shift =="park")
{
return true;
}
else
{
return false;
}
}
boolean converter (String code)
{
long val = Integer.parseInt(code,2);
if (val == 10)
{
// Validate if the binary code is decimal equivalent to 10 or not . You can change the number as well try
{
FileWriter myWriter = new FileWriter("C:\\Users\\escuhao\\Desktop\\doorcode.txt");
// Path where the code is recorded. myWriter.write("Door code : "+val);
myWriter.close();
System.out.println("Code is recorded for the door . ");
}
catch (IOException e)
{
System.out.println("Alert !!! An exception happens !!!"); e.printStackTrace();
}
return true;
}
else
{
return false;
}
}
void display(boolean dec_code)
{
boolean g_status = gear_shift_status();
boolean in_handle = inside_handle();
boolean out_handle = outside_handle();
if (g_status ==true && out_handle == true && dec_code == true)
{
System.out.println("Door is open or can be opened by outside handle");
if (in_handle == true)
{
System.out.println("Door is open or can be opened by inside handle");
}
}
else { System.out.println("Door is locked");
}
}
}
public class door
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in); System.out.println("Enter the binary code : ");
String code = sc.nextLine();
System.out.println("Status of Door 1: "); Mini_Van_Sliding_Doors door1 = new Mini_Van_Sliding_Doors(false,false,"park");
// child_safety_lock , master_lock are off boolean rn1=door1.converter(code); door1.display(rn1); System.out.println("Status of Door 2: "); Mini_Van_Sliding_Doors door2 = new Mini_Van_Sliding_Doors(false,true,"park");
// child_safety_lock is on but master_lock is off boolean rn2=door2.converter(code); door2.display(rn2);
System.out.println("Status of Door 3: "); Mini_Van_Sliding_Doors door3 = new Mini_Van_Sliding_Doors(true,true,"park");
// child_safety_lock , master_lock are on boolean rn3=door3.converter(code); door3.display(rn3); System.out.println("Status of Door 4: "); Mini_Van_Sliding_Doors door4 = new Mini_Van_Sliding_Doors(true,true,"nopark");
// gear_status is not "park" boolean rn4=door4.converter(code);
door4.display(rn4);
}
}
OUTPUT: