In Java Parkable: This should be an interface with:a method called park() which:
ID: 3733797 • Letter: I
Question
In Java
Parkable:
This should be an interface with:a method called park() which:
Returns a boolean to note if the Parkable object has been parked correctly or not.
a method called unpark() which:
Returns a boolean to note if the Parkable object has been unparked correctly or not.
Auto:
Should implement the Parkable interface.
For park, only return true if the Auto has a permit.
For unpark, always return true (eventually we will add auto boots for cars that are illegally parked)
Bicycle:
Should implement the Parkable interface.
For park, set the Bicycle's attribute for locked to true, then return true;
For unpark, set the Bicycle's attribute for locked to false, then return true;
----------------------------------------------
Static stuff!
The ParkingSpace object will get a static variable for tracking number of parking spaces
Use this static variable to set the number of the parking space in both ParkingSpace constructors
Remove the number parameter from the the Full ParkingSpace Constructor
Explanation / Answer
import java.lang.*;
import java.io.*;
import java.util.Scanner;
interface Parkable
{
public boolean park();
public boolean unpark();
}
class Auto implements Parkable
{
boolean permit;
Scanner cin;
Auto()
{
permit=false;
}
Auto(int x)
{
String p;
cin=new Scanner(System.in);
System.out.println("Enter Auto's Permit[yes/no]");
p= cin.nextLine();
if(p.equals("yes"))
permit=true;
if(p.equals("no"))
System.out.println("not parkable");
if(permit && x==0)
System.out.println("No Parking Space");
}
public boolean park()
{
if (permit)
{
return true;
}
else
return false;
}
public boolean unpark()
{
if(!permit)
{ return true;
}
else
return false;
}
}
class Bicycle implements Parkable
{
boolean locked;
Scanner cin;
Bicycle()
{
locked=false;
}
Bicycle(int x)
{
String l;
cin=new Scanner(System.in);
System.out.println("Enter Cycle is lockable[yes/no] ");
l=cin.nextLine();
if(l.equals("yes"))
locked=true;
if(l.equals("no"))
System.out.println("not parkable");
if(locked && x==0)
System.out.println("No Parking Space");
}
public boolean park()
{
if (locked)
{
return true;
}
else
return false;
}
public boolean unpark()
{
if(!locked)
{
return true;
}
else
return false;
}
}
// Class parking space contains static stuff counts parking spaces for autos and bicycles
class ParkingSpace
{
static int Autospaces;
static int Bicyclespaces;
ParkingSpace()
{
Autospaces=3;
Bicyclespaces=3;
}
}
//Parking Project is main class
public class ParkingProject
{
public static void main(String args[]) throws Exception
{
Scanner cin=new Scanner(System.in);
int nextveh=0;
ParkingSpace obj=new ParkingSpace();
while(nextveh!=3)
{
System.out.println("1 Auto");
System.out.println("2 Bicycle");
System.out.println("3 to exit");
System.out.println("Enter your option");
nextveh=cin.nextInt();
if(nextveh==1)
{ Auto objA=new Auto(ParkingSpace.Autospaces);
if(objA.park())
ParkingSpace.Autospaces--;
}
else if(nextveh==2)
{
Bicycle objB=new Bicycle(ParkingSpace.Bicyclespaces);
if(objB.park())
ParkingSpace.Bicyclespaces--;
}
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.