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

MAIN: Data File: There will be one data file. You must test your program with th

ID: 3599285 • Letter: M

Question

MAIN:

Data File: There will be one data file. You must test your program with this file. The file is: bashemin.i n (you should create this file and use it in your program). Its text is: aVAX785 aPDPII aIBM360 aMTHCSC dPDP11 aMAC512 dVAX785 aP1314 aCSB-1 dCSB-1 dIBM360 aPDP11 a257ADT dMACS12 Your program might start out like this - import java.io. class BasheminParkinglot public static void main(Stringl) args) throws Exception Stack parkinglot- new Stack): Stack alley-new Stack(: File f= new File(.bashemin.in"); FilelnputStream finstream new FileinputStream(fl: InputStreamReader finreader new InputstreamReaderffinstream) BufferedReader finput- new BufferedReader(finreader) String line = finput-readLine(); String plate System.out.printin System.out.printin("The Bashemin Status": System.out.printin while(linel-null) fline.charAt(o) 'a') plate-line.substring(1); System.out.printin/"car"+plate+ "arrived and parked": parkinglot.Push(plate

Explanation / Answer

Given below is the new and modified code. I had to modify the Stack class to know if it is empty or not. Make sure to place the input file bashemin.in directly under the project folder and NOT INSIDE src folder , if using eclipse. Otherwise you would get file not found exception. Please don't forget to rate the answer if it helped. Thank you.

To indent code in eclipse, select the code by pressing Ctrl+A and then press Ctrl+i

Stack.java


public class Stack { //linked list version of stack, String data
// uses the Node class
private Node headNode; // refers to dummy head node of the linked list-"bookend"
private Node endNode; // refers to dummy end node of the linked list-"bookend"
//Constructor – establishes an empty stack
public Stack() {
// sets up two linked dummy "bookend" nodes
endNode=new Node("a",null);
headNode=new Node("a",endNode);
}
//Methods - Push, Pop and Peek
public void Push(String item) {
if(headNode.getLink()==endNode) { // if the list is empty
Node n = new Node();
n.setDatum(item);
n.setLink(endNode);
headNode.setLink(n);
} else {
Node n = new Node();
n.setDatum(item);
n.setLink(headNode.getLink());
headNode.setLink(n);
}
}
public void Pop() {
headNode.setLink(headNode.getLink().getLink());
}
public String Peek() {
return headNode.getLink().getDatum();
}
  
public boolean Empty()
{
return headNode.getLink() == endNode;
}
  
  
  
// Method Display
public void Display(){
//System.out.println("The data in the linked list are:");
Node l = headNode.getLink();
while (l != endNode) {
System.out.println( l.getDatum());
l = l.getLink();
}
}
}

BasheminParkingLot.java


import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
public class BasheminParkingLot {
public static void main(String[] args) throws Exception{
Stack parkinglot = new Stack();
Stack alley = new Stack();
File f = new File("bashemin.in");
FileInputStream finstream = new FileInputStream(f);
InputStreamReader finreader = new InputStreamReader(finstream);
BufferedReader finput = new BufferedReader(finreader);
String plate ="";
System.out.println();
System.out.println("The Bashemin Status");
System.out.println();
String line = finput.readLine();
int parkedCars = 0;
int PARKING_CAPACITY = 5; //only 5 cars allowed to be parked
while(line != null)
{
if(line.charAt(0) == 'a')
{
plate = line.substring(1);
System.out.print("Car " + plate + " arrived . " );
if(parkedCars < PARKING_CAPACITY)
{
System.out.print("It is parked. ");
parkedCars++;
parkinglot.Push(plate);
}
else
{
System.out.print("Parking lot full. Can't park. ");
}
}
else if(line.charAt(0) == 'd')
{
plate = line.substring(1);
int moved = 0;
while(!parkinglot.Empty() && !parkinglot.Peek().equals(plate)) //as long as we did not find the car to be removed from lot
{
//move them to alley
alley.Push(parkinglot.Peek());
parkinglot.Pop();
moved++;
}
if(parkinglot.Empty()) //did not find the car with the plate given
System.out.println("No car " + plate + " in the parking lot to depart");
else
{
System.out.println("Car " + plate + " departed. " + moved + " cars were moved to adjacent alley.");
parkinglot.Pop(); //car departs
parkedCars--;
//get back all cars from alley
while(!alley.Empty())
{
parkinglot.Push(alley.Peek());
alley.Pop();
}
}
}
else if(line.equals("p"))
{
System.out.println("The current parking lot status is ");
parkinglot.Display();
}
line = finput.readLine();
}
System.out.println("Day is over!");
//print cars towed away at end of day...
while(!parkinglot.Empty())
{
System.out.println("Car " + parkinglot.Peek() + " towed away");
parkinglot.Pop();
}
}
}

input file: bashemin.in

aVAX785
aPDP11
aIBM360
aMTHCSC
dPDP11
p
aMAC512
dVAX785
aPI314
aCSB-1
dCSB-1
dIBM360
p
aPDP11
a257ADT
dMAC512

output


The Bashemin Status

Car VAX785 arrived . It is parked.
Car PDP11 arrived . It is parked.
Car IBM360 arrived . It is parked.
Car MTHCSC arrived . It is parked.
Car PDP11 departed. 2 cars were moved to adjacent alley.
The current parking lot status is
MTHCSC
IBM360
VAX785
Car MAC512 arrived . It is parked.
Car VAX785 departed. 3 cars were moved to adjacent alley.
Car PI314 arrived . It is parked.
Car CSB-1 arrived . It is parked.
Car CSB-1 departed. 0 cars were moved to adjacent alley.
Car IBM360 departed. 3 cars were moved to adjacent alley.
The current parking lot status is
PI314
MAC512
MTHCSC
Car PDP11 arrived . It is parked.
Car 257ADT arrived . It is parked.
Car MAC512 departed. 3 cars were moved to adjacent alley.
Day is over!
Car 257ADT towed away
Car PDP11 towed away
Car PI314 towed away
Car MTHCSC towed away