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

Urgent help needed related java ! Thanx Design a parking Iot Design a ParkingLot

ID: 3916043 • Letter: U

Question

Urgent help needed related java ! Thanx

Design a parking Iot Design a ParkingLot package that allocates specific parking spaces to cars ina smart way. Decide what classes you'll need, and design the API for each. Time permitting, select data structures to implement the API for each class. Try to deal with annoying cases (like disobedient humans) . Parking spaces can be either regular, compact, or handicapped-only. . When a new car arrives, the system should assign a specific space based on the type of car All cars are allowed to park in regular spots. Thus, compact cars can park in both compact spaces and regular spaces When a car leaves, the system should record that the space is free. Your package should be designed in a manner that allows different parking lots to have different numbers of spaces for each of the 3 types. . Parking spots should have a sense of closeness to the entrance. When parking a car, place it as close to the entrance as possible. Assume these distances are distinct.

Explanation / Answer

Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts. Thanks

EDIT: I’m getting troubles submitting the answer without losing the format. Showing character limit exceeded error. So I have to paste it as a plain text, which will cause the loss of code formatting and indentations. Sorry for the trouble. If you are using eclipse ,copy the code and press ctrl+shift+F to format the code

// Car.java

public class Car {

// attributes

private boolean isCompact;

private boolean isHandicapped;

private ParkingLot parkedLot;

/**

* constructor

*

* @param isCompact

* - denoting if the car is compact or not

* @param isHandicapped

* - denoting if the car is under handicapped category or not

*/

public Car(boolean isCompact, boolean isHandicapped) {

this.isCompact = isCompact;

this.isHandicapped = isHandicapped;

}

//required accessors

public boolean isCompact() {

return isCompact;

}

public boolean isHandicapped() {

return isHandicapped;

}

/**

* method to park the car in a lot

* @param lot - the parking lot to park

* @return - true if parked, false if no space

*/

public boolean findSpotAndPark(ParkingLot lot) {

//trying to park the car in lot

if (lot.findSpotAndPark(this)) {

//success

parkedLot = lot;

return true;

}

//failed

return false;

}

/**

* method to leave the spot (if exists)

*/

public void leaveSpot() {

if (parkedLot != null) {

//removing car from lot

parkedLot.removeCarFromSpot(this);

}

}

}

// ParkingLot.java

import java.util.Arrays;

public class ParkingLot {

/**

* different spots for each category

*/

private Spot[] handicapped;

private Spot[] regular;

private Spot[] compact;

public ParkingLot(int[] handicappedDistances, int[] compactDistances,

int[] regularDistances) {

/**

* creating spots for each category using arrays

*/

handicapped = new Spot[handicappedDistances.length];

for (int i = 0; i < handicappedDistances.length; i++) {

handicapped[i] = new Spot("handicapped", handicappedDistances[i]);

}

regular = new Spot[regularDistances.length];

for (int i = 0; i < regularDistances.length; i++) {

regular[i] = new Spot("regular", regularDistances[i]);

}

compact = new Spot[compactDistances.length];

for (int i = 0; i < compactDistances.length; i++) {

compact[i] = new Spot("compact", compactDistances[i]);

}

/**

* Sorting the spots by distance (This is why the Spot class is made to

* implement Comparable interface)

*/

Arrays.sort(handicapped);

Arrays.sort(compact);

Arrays.sort(regular);

}

/**

* method to try to park a car in the lot

*

* @param toPark

* @return true if successful, else false

*/

public boolean findSpotAndPark(Car toPark) {

//the order of loops is very important

if (toPark.isHandicapped()) {

// looping through handicapped spots

for (int i = 0; i < handicapped.length; i++) {

if (handicapped[i].parkedCar == null) {

handicapped[i].parkedCar = toPark;

// parked

return true;

}

}

}

if (toPark.isCompact()) {

// looping through compact spots

for (int i = 0; i < compact.length; i++) {

if (compact[i].parkedCar == null) {

compact[i].parkedCar = toPark;

// parked

return true;

}

}

}

// looping through regular spots

for (int i = 0; i < regular.length; i++) {

if (regular[i].parkedCar == null) {

regular[i].parkedCar = toPark;

// parked

return true;

}

}

return false; // no spaces left

}

/**

* method to remove a car from a spot

*/

public void removeCarFromSpot(Car toRemove) {

boolean removed = false;

//checking if the car is parked in handicapped section

for (int i = 0; i < handicapped.length; i++) {

if (handicapped[i].parkedCar == toRemove) {

handicapped[i].parkedCar = null;

removed = true;

break;

}

}

if (!removed) {

//checking if the car is parked in compact section

for (int i = 0; i < compact.length; i++) {

if (compact[i].parkedCar == toRemove) {

compact[i].parkedCar = null;

removed = true;

break;

}

}

}

if (!removed) {

//checking if the car is parked in regular section

for (int i = 0; i < regular.length; i++) {

if (regular[i].parkedCar == toRemove) {

regular[i].parkedCar = null;

removed = true;

break;

}

}

}

}

@Override

public String toString() {

//returning a String containing formatted parking lot structure

String data = " Handicapped section: ";

for (int i = 0; i < handicapped.length; i++) {

if (handicapped[i].parkedCar == null) {

data += "[vacant] ";

} else {

data += "[FILLED] ";

}

}

data += " Compact section: ";

for (int i = 0; i < compact.length; i++) {

if (compact[i].parkedCar == null) {

data += "[vacant] ";

} else {

data += "[FILLED] ";

}

}

data += " Regular section: ";

for (int i = 0; i < regular.length; i++) {

if (regular[i].parkedCar == null) {

data += "[vacant] ";

} else {

data += "[FILLED] ";

}

}

data += " ";

return data;

}

private class Spot implements Comparable<Spot> {

private boolean isHandicapped;

private boolean isCompact;

private int proximity;

private Car parkedCar;

private Spot(String type, int proximity) {

this.proximity = proximity;

if (type.equalsIgnoreCase("Handicapped")) {

// handicapped

isHandicapped = true;

isCompact = false;

} else if (type.equalsIgnoreCase("Compact")) {

// compact

isHandicapped = false;

isCompact = true;

} else {

// regular

isHandicapped = false;

isCompact = false;

}

parkedCar = null;

}

private boolean isHandicapped() {

return isHandicapped;

}

private boolean isCompact() {

return isCompact;

}

@Override

public int compareTo(Spot other) {

/**

* helps in sorting Spots by proximity

*/

if (this.proximity < other.proximity) {

return -1;

} else if (this.proximity > other.proximity) {

return 1;

}

return 0;

}

}

}

// Test.java

public class Test {

public static void main(String[] args) {

/**

* creating 4 cars

*/

Car car1=new Car(true, true);

Car car2=new Car(false, true);

Car car3=new Car(true, false);

Car car4=new Car(false, false);

/**

* Defining some distances to be used for each type of lots

*/

int handicappedDistances[]={100,150};

int compactDistances[]={100,110,170,40};

int regularDistances[]={80,110,100,40,50,150,250,350,200};

/**

* Creating a parking lot

*/

ParkingLot lot=new ParkingLot(handicappedDistances, compactDistances, regularDistances);

/**

* trying to park each car

*/

if(car1.findSpotAndPark(lot)){

System.out.println("Car1 parking successful");

}else{

System.out.println("Car1 parking failed");

}

/**

* displaying the parking lot, using toString() method

*/

System.out.println(lot);

if(car2.findSpotAndPark(lot)){

System.out.println("Car2 parking successful");

}else{

System.out.println("Car2 parking failed");

}

System.out.println(lot);

if(car3.findSpotAndPark(lot)){

System.out.println("Car3 parking successful");

}else{

System.out.println("Car3 parking failed");

}

System.out.println(lot);

if(car4.findSpotAndPark(lot)){

System.out.println("Car4 parking successful");

}else{

System.out.println("Car4 parking failed");

}

System.out.println(lot);

/**

* Removing each car from the lot

*/

car1.leaveSpot();

System.out.println("Car 1 removed");

System.out.println(lot);

car2.leaveSpot();

System.out.println("Car 2 removed");

System.out.println(lot);

car3.leaveSpot();

System.out.println("Car 3 removed");

System.out.println(lot);

car4.leaveSpot();

System.out.println("Car 4 removed");

System.out.println(lot);

}

}

/*OUTPUT*/

Car1 parking successful

Handicapped section:

[FILLED] [vacant]

Compact section:

[vacant] [vacant] [vacant] [vacant]

Regular section:

[vacant] [vacant] [vacant] [vacant] [vacant] [vacant] [vacant] [vacant] [vacant]

Car2 parking successful

Handicapped section:

[FILLED] [FILLED]

Compact section:

[vacant] [vacant] [vacant] [vacant]

Regular section:

[vacant] [vacant] [vacant] [vacant] [vacant] [vacant] [vacant] [vacant] [vacant]

Car3 parking successful

Handicapped section:

[FILLED] [FILLED]

Compact section:

[FILLED] [vacant] [vacant] [vacant]

Regular section:

[vacant] [vacant] [vacant] [vacant] [vacant] [vacant] [vacant] [vacant] [vacant]

Car4 parking successful

Handicapped section:

[FILLED] [FILLED]

Compact section:

[FILLED] [vacant] [vacant] [vacant]

Regular section:

[FILLED] [vacant] [vacant] [vacant] [vacant] [vacant] [vacant] [vacant] [vacant]

Car 1 removed

Handicapped section:

[vacant] [FILLED]

Compact section:

[FILLED] [vacant] [vacant] [vacant]

Regular section:

[FILLED] [vacant] [vacant] [vacant] [vacant] [vacant] [vacant] [vacant] [vacant]

Car 2 removed

Handicapped section:

[vacant] [vacant]

Compact section:

[FILLED] [vacant] [vacant] [vacant]

Regular section:

[FILLED] [vacant] [vacant] [vacant] [vacant] [vacant] [vacant] [vacant] [vacant]

Car 3 removed

Handicapped section:

[vacant] [vacant]

Compact section:

[vacant] [vacant] [vacant] [vacant]

Regular section:

[FILLED] [vacant] [vacant] [vacant] [vacant] [vacant] [vacant] [vacant] [vacant]

Car 4 removed

Handicapped section:

[vacant] [vacant]

Compact section:

[vacant] [vacant] [vacant] [vacant]

Regular section:

[vacant] [vacant] [vacant] [vacant] [vacant] [vacant] [vacant] [vacant] [vacant]

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