Question

In: Computer Science

using java and use stack library java Bashemin Parking garage The Bashemin Parking Garage contains a...

using java and use stack library java

Bashemin Parking garage

The Bashemin Parking Garage contains a single lane that holds up to ten cars. There is only a single entrance/exit to the garage at one end of the lane. If a customer arrives to pick up a car that is not nearest the exit, all cars blocking its path. are moved out, the customer's car is driven out and the other cars are restored in the same order that they were in originally.

Write a program that processes a group of input lines. Each input line contains an "A" for arrival or a "D" for departure and a license plate number. Cars are assumed to arrive and depart in the order specified by the input. The program should print a message whenever a car arrives or departs. When a car arrives, the message should specify whether or not there is room for the car in the garage. If there is no room the car leaves without entering the garage. When a car departs, the message should include the number of times that the car was moved out to allow other cars to depart.

You will submit a program listing (properly commented), your sample data (at least 20 lines of text) and the output.

Solutions

Expert Solution

PROGRAM:

import java.util.*;
import java.lang.*;
import java.io.*;

class garage
{
   public static void main (String[] args)
   {
        Scanner sc = new Scanner(System.in);
      
        String s="";
        s = sc.nextLine();

        // STORE WHETHER A CAR IS ARRIVING OR DEPARTING WITH ITS PLATE NUMBER      
        String type;
        String plateNumber;

      
        //STACK TO SIMULATE THE GARAGE WHICH CAN STORE ONLY 10 CARS
        Stack<String> parkingGarage = new Stack<String>();


            //TEMPORARY STACK USED DURING REMOVAL OF ANY CAR
        Stack<String> temp = new Stack<String>();


        //STORES THE NUMBER OF TIMES A CAR WITH A PLATENUMBER P MOVES
        //OUT OF THE GARAGE FOR OTHER CARS
        HashMap<String,Integer> map = new HashMap<>();
      
          
        //UNTIL USER ENTERS THE TEXT END
            //A VALID SEQUENCE OF ARRIVALS AND DEPARTURES IS
            //EXPECTED FROM THE USER
        while(!s.equals("END")){
      

       //OBTAINING THE PLATE NUMBER AND TYPE(ARRIVAL/DEPARTURE) FROM THE USER
            String[] typeAndPlateNumber = s.split(" ",2);
            type = typeAndPlateNumber[0];
            plateNumber = typeAndPlateNumber[1];
         
       //IN CASE OF ARRIVAL
            if(type.equals("A")){

            //CAR ENTERS ONLY IF GARAGE HAS LESS THAN 10 CARS              
                if (parkingGarage.size()<10){
           System.out.println("There is room in the garage for the car.");
                    parkingGarage.push(plateNumber);
                    System.out.println("Car with plate number "+plateNumber+" has arrived.");
                    map.put(plateNumber,0);
                }else{
           System.out.println("There is no room in the garage for the car.");  
            }  

            }else{

            //REMOVE THE CARS UNTIL THE ACTUAL CAR TO BE REMOVED
            //IS REACHED
                while(!(parkingGarage.peek().equals(plateNumber))){
           int temp1 = map.get(parkingGarage.peek());
                    map.put(parkingGarage.peek(),temp1+1);
                    temp.push(parkingGarage.peek());
                    parkingGarage.pop();
                }

            //REMOVE THE CAR THAT IS TO DEPART
                System.out.println("\nCar with plate number "+plateNumber+" has departed and it moved out "+String.valueOf(map.get(plateNumber)+" times.\n"));
                parkingGarage.pop();

            //REENTER THE CARS IN THE ORIGINAL ORDER BACK INTO THE GARAGE          
                while(!temp.empty()){
                    parkingGarage.push(temp.peek());
                    temp.pop();
                }
            }
              
            s = sc.nextLine();
        }
      
   }
}

INPUT:

A C1

A C2

A C3

A C4

A C5

A C6
A C7
A C8
A C9

D C7

A C10

A C11

A C12

D C1

D C12

D C11

D C10

D C9

D C8

D C6

D C5

CONSOLE INTERACTION & OUTPUT:

A C1
There is room in the garage for the car.
Car with plate number C1 has arrived.
A C2
There is room in the garage for the car.
Car with plate number C2 has arrived.
A C3
There is room in the garage for the car.
Car with plate number C3 has arrived.
A C4
There is room in the garage for the car.
Car with plate number C4 has arrived.
A C5
There is room in the garage for the car.
Car with plate number C5 has arrived.
A C6
There is room in the garage for the car.
Car with plate number C6 has arrived.
A C7
There is room in the garage for the car.
Car with plate number C7 has arrived.
A C8
There is room in the garage for the car.
Car with plate number C8 has arrived.
A C9
There is room in the garage for the car.
Car with plate number C9 has arrived.
D C7

Car with plate number C7 has departed and it moved out 0 times.

A C10
There is room in the garage for the car.
Car with plate number C10 has arrived.
A C11
There is room in the garage for the car.
Car with plate number C11 has arrived.
A C12
There is no room in the garage for the car.
D C1
Car with plate number C1 has departed and it moved out 0 times.

D C11

Car with plate number C11 has departed and it moved out 1 times.

D C10

Car with plate number C10 has departed and it moved out 1 times.

D C9

Car with plate number C9 has departed and it moved out 2 times.

D C8

Car with plate number C8 has departed and it moved out 2 times.

D C6

Car with plate number C6 has departed and it moved out 1 times.

D C5

Car with plate number C5 has departed and it moved out 1 times.

END


Related Solutions

use c++ Parking charge application: A parking garage charges a $20.00 minimum fee to park for...
use c++ Parking charge application: A parking garage charges a $20.00 minimum fee to park for up to 3 hours. The garage charges an additional $5.00 per hour for hour or part thereof in excess of 3 hours. The maximum charge for any given 24-hour period is $50.00. Assume that no car parks for longer than 24 hours at a time. Write a program that calculates and prints the parking charge for each of 3 customers who parked their cars...
Write a Java Program.A parking garage charges a $3.00 minimum fee to park for up to...
Write a Java Program.A parking garage charges a $3.00 minimum fee to park for up to three hours. The garage charges an additional $0.75 per hour for each hour or part thereof in excess of three hours. The maximum charge for any given 24-hour period is $12.00 per day. Write an application that calculates and displays the parking charges for all customers who parked in the garage yesterday. You should enter the hours parked for each customer. The program should...
Use CPP 1) Parking charge application: A parking garage charges a $20.00 minimum fee to park...
Use CPP 1) Parking charge application: A parking garage charges a $20.00 minimum fee to park for up to 3 hours. The garage charges an additional $5.00 per hour for hour or part thereof in excess of 3 hours. The maximum charge for any given 24-hour period is $50.00. Assume that no car parks for longer than 24 hours at a time. Write a program that calculates and prints the parking charge for each of 3 customers who parked their...
Java program Reverse polish notation: using stack - You can use the Stack included in java.util.Stack...
Java program Reverse polish notation: using stack - You can use the Stack included in java.util.Stack (or your own implementation) for this problem. Reverse Polish notation is a notation where every operator follows all of its operands. For example, an expression (1+2)*(5+4) in the conventional Polish notation can be represented as 1 2 + 5 4 + * in the Reverse Polish notation. One of advantages of the Reverse Polish notation is that it is parenthesis-free. Write a program which...
please in java ! Assume you have a stack of integers. The stack contains same number...
please in java ! Assume you have a stack of integers. The stack contains same number of positive and negative integers. You want to organize it such that negative and positive integers alternate (+-+-.., or -+-+,..). A. Write a Java code that uses no more than two additional Stacks to solve the problem. Note: You do not need to write the code for Stacks, you are using a Stack from the library with a name ourStack and has the following...
IN JAVA LANGUAGE Linked List-Based Stack Implementation Implement Stack using a Linked List Use the language...
IN JAVA LANGUAGE Linked List-Based Stack Implementation Implement Stack using a Linked List Use the language library LinkedList Stack methods will call the LinkedList methods You can use string as the object Instead of using an array, as the StackLab did, here you will use a Linked List from your language's library. Implement all the methods of Stack : push(), pop(), size(), printStackDown(), etc, using calls to the linked list methods that correspond to the actions need. In the array...
The parking superintendent is responsible for snow removal at his parking garage. The probabilities for the...
The parking superintendent is responsible for snow removal at his parking garage. The probabilities for the number of days per year requiring snow removal are shown in the chart below. These probabilities are independent from year to year. The superintendent can contract for snow removal at a cost of $500 per day. Alternatively, he can purchase a snow-removal machine for $40,000. It is expected to have a useful life of 10 years and no salvage value at that time. Annual...
The program (​ stack-ptr.c​ ) implements stack using a linked list, however, it contains a race...
The program (​ stack-ptr.c​ ) implements stack using a linked list, however, it contains a race condition and is not appropriate for a concurrent environment. Using Pthreads mutex locks, fix the race condition. For reference, see Section 7.3.1 of SGG book.(Section 7.3.1 is about mutex and semaphores it does explain how to implement I'm just having a hard time finding the race condition within the code) /* * Stack containing race conditions */ #include #include #include typedef int value_t; //...
A car is to be hoisted by elevator to the fourth floor of a parking garage,...
A car is to be hoisted by elevator to the fourth floor of a parking garage, which is 48 ft above the ground. Part A If the elevator can accelerate at 0.8 ft/s2, decelerate at 0.3 ft/s2, and reach a maximum speed of 8 ft/s, determine the shortest time to make the lift, starting from rest and ending at rest.
using java Define a class Library based on the following specifications: a. A library can have...
using java Define a class Library based on the following specifications: a. A library can have multiple books. Decide on the best possible data structure to store books. b. A library has a name. c. A library has an address. Define a 2-argument constructor for the Library class. Decide on the arguments needed for this constructor. e. Define a method that can add new books to the library. f. Define a method that allows a book to be borrowed (checked...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT