Question

Overview For this assignment, we will practice using stacks and queues. In this exercise, you will...

Overview

For this assignment, we will practice using stacks and queues. In this exercise, you will create two stacks and a queue to keep track of cars using a parking lot. We will use the nature of the stacks and queues to solve the problem.

Specifications

Rules

FIU has opened a new valet parking lot next to the PC building that requires a special decal to park. If the car does not have the correct parking decal, the driver should be told nicely that they do not have the correct permit to park in this lot.

The parking lot is a “last-in, first-out” stack. When parking a car, the program must first check to see if the car has the correct decal. If it does have the correct permit, the car may be added to the lot as long as there is enough room. To park a car, the car’s license tag is added to the parking lot stack. The parking lot can hold 20 cars at a time. If the parking lot is full, a waiting car can be added to a waiting queue. When a car leaves the lot, the next car in the queue will be able to park in the parking lot. When a car owner retrieves a vehicle that wasn’t the last one in, the cars blocking it must temporarily move to the street so that the requested vehicle can leave.

Write a program that models this behavior, using one stack for the driveway and one stack for the street. A queue will be used to store the cars waiting for a place to park of the lot is full

Use integers as license plate numbers. Positive numbers add a car, negative numbers remove a car, zero stops the simulation. If the stack representing the parking lot is full, the car waiting should be added to the queue. Print out the parking lot stack and the waiting queue after each operation is complete.

Expected Output:

The expected output should contain the list of cars currently parked in the parking lot and the list of cars currently waiting to park.

Java Requirements

The program must

  • Utilize a stack representing the parking lot.
  • Utilize a stack representing the street.
  • Utilize a queue to store the cars waiting to park.
  • Check the parking decal type and politely tell the driver if they do not have the correct decal.
  • Limit the number of cars parking in the lot to a maximum of 20.
  • Output that includes the cars parked in the lot and any cars waiting in the queue to park after each operation.
  • Have an easy to understand user interface that allows the user to park a car or retrieve a parked car.
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Here is the code in java please do upvote thankyou..!!

Also attached the screenshots of the output..

//code

package valetParking;
import java.util.*;

// class Valet Parking
public class ValetParking {
  
   // stacks and queue
   Stack<Integer> parkingLot = new Stack<Integer>();
   Stack<Integer> street = new Stack<Integer>();
   Queue<Integer> waiting = new LinkedList<Integer>();
  
   // function to add the car into the parking
   public void add(int x)
   {
       // if parking stack is full
       // then add it into the waiting park
       if(parkingLot.size() >= 20)
       {
           waiting.add(x);
       }
       // if parking stack is not full
       else
       {
           parkingLot.push(x);
       }
       // print everything
       System.out.print("Parking Lot: ");
       ValetParking.printStack(parkingLot);
       System.out.println("\nWaiting car: "+ waiting );
      
      
   }
  
   // function to print stack using recursion
   public static void printStack(Stack<Integer> s){
       if(s.empty()){
           return;
       }
       int x = s.pop();
       System.out.print(x + " ");
       printStack(s) ;
       s.push(x);
   }
  
   // remove car from parking
   public void remove(int x)
   {
       // if parking is not empty
       if(!parkingLot.empty())
       {
           // excess all element and search till same number
           // or to the end of stack
           // push all the elemnt of parking stack into street
           int n = parkingLot.pop();
           while(n != -x)
           {
               street.push(n);
               if(parkingLot.empty())
                   break;
               n = parkingLot.pop();
           }
          
           // push back element from street to parking
           while(!street.empty())
               parkingLot.push(street.pop());
          
           // check is there any space for waiting cars
           if(waiting.size()!=0 && parkingLot.size() <20)
               parkingLot.push(waiting.poll());
       }
      
       System.out.print("Parking Lot: ");
       ValetParking.printStack(parkingLot);
       System.out.println("\nWaiting car: "+ waiting );
      
   }
  
  
   // driver function
   public static void main(String[] args)
   {
       // object creation
       ValetParking vp = new ValetParking();

       Scanner sc = new Scanner(System.in);
      
       int x = sc.nextInt();
      
       while(x != 0)
       {
           if( x > 0)
               vp.add(x);
           else
               vp.remove(x);
          
           x = sc.nextInt();
       }
      
      
   }

}

--OUTPUT--

input 1 Parking Lot: 1 Waiting car: [] 2 Parking Lot: 2 1 Waiting car: [] 3 Parking Lot: 3 2 1 Waiting car: [] 4 Parking Lot:Parking Lot: 11 10 9 8 7 6 5 4 3 Waiting car: [] L2 Parking Lot: 12 11 10 9 8 7 6 5 4 3 2 1 Taiting car: [] 13 Parking Lot: 1Parking Lot: 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 Waiting car: [] 20 Parking Lot: 20 19 18 17 16 15 14 13 12 11 10

Add a comment
Know the answer?
Add Answer to:
Overview For this assignment, we will practice using stacks and queues. In this exercise, you will...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Overview For this assignment, we will practice using stacks and queues. In this exercise, you will...

    Overview For this assignment, we will practice using stacks and queues. In this exercise, you will create two stacks and a queue to keep track of cars using a parking lot. We will use the nature of the stacks and queues to solve the problem. Specifications Rules FIU has opened a new valet parking lot next to the PC building that requires a special decal to park. If the car does not have the correct parking decal, the driver should...

  • Use the description from Parking Ticket Simulator from Chapter 14 as a basis, where you need...

    Use the description from Parking Ticket Simulator from Chapter 14 as a basis, where you need to create a Car class and Police Officer class, to create a new simulation. Write a simulation program (refer to the Bank Teller example in the PPT) that simulates cars entering a parking lot, paying for parking, and leaving the parking lot. The officer will randomly appear to survey the cars in the lot to ensure that no cars are parked beyond their time...

  • Module 4 follow the materials available at Topic - Stacks and Queues. You should have a...

    Module 4 follow the materials available at Topic - Stacks and Queues. You should have a good understanding of Lists at  Topic - Basic Data structures as a Queue and a Stack are simply implementation of a List with specific properties. Assignment - Implement a Stack computer in Javascript (you will turn in a link to your program in JSFiddle). This is a simple computer that keeps a stack, when a number is entered it goes onto the top of the...

  • The CSC326 parking garage contains 2 lanes, each capable of holding up to 10 cars. There...

    The CSC326 parking garage contains 2 lanes, each capable of holding up to 10 cars. There is only a single entrace/exit to the garage at one end of the lanes. If a customer arrives to pick up a car which is not nearest the exit, all cars blocking the car's path are moved into the other lane. If more cars still must be moved out of the way, they go into the street. When the customer's car is driven out,...

  • The purpose of this problem is to gain familiarity with stacks and queues. You have three...

    The purpose of this problem is to gain familiarity with stacks and queues. You have three jugs that can hold c1, c2, and c3 liters of water, respectively. Initially, jug 1 is full and the other two jugs are empty. You can repeat the following procedure any number of times: Choose two of the jugs and pour the contents of one into the other until either the first is empty or the second is full. Your goal is to end...

  • This is a c++ program. Use the description from Parking Ticket Simulator (listed below) as a basis, where you need to...

    This is a c++ program. Use the description from Parking Ticket Simulator (listed below) as a basis, where you need to create a Car class and Police Officer class, to create a new simulation. Write a simulation program (refer to the Bank Teller example listed below) that simulates cars entering a parking lot, paying for parking, and leaving the parking lot. The officer will randomly appear to survey the cars in the lot to ensure that no cars are parked...

  • using java: For this exercise, you will design a set of classes that work together to simulate a parking officer checkin...

    using java: For this exercise, you will design a set of classes that work together to simulate a parking officer checking a parked car issuing a parking ticket if there is a parking violation. Here are the classes that need to collaborate: • The ParkedCar class: This class should simulate a parked car. The car has a make, model, color, license number. •The ParkingMeter class: This class should simulate a parking meter. The class has three parameters: − A 5-digit...

  • Lab 3 – Array-Based Stack and Queue Overview In this assignment, you will be implementing your...

    Lab 3 – Array-Based Stack and Queue Overview In this assignment, you will be implementing your own Array-Based Stack (ABS) and Array-Based Queue (ABQ). A stack is a linear data structure which follows the Last-In, First-Out (LIFO) property. LIFO means that the data most recently added is the first data to be removed. (Imagine a stack of books, or a stack of papers on a desk—the first one to be removed is the last one placed on top.) A queue...

  • Write a C++ code based this question n this assignment you will create three additional functions...

    Write a C++ code based this question n this assignment you will create three additional functions for each one of the data structures created in class. You will be provided with a header file which you will modify and a demo program for each data structure. If your functions are implemented correctly the demo programs should compile and execute correctly. Part 0 (Comments) 1. Over the course of the semester we have discussed comments for each data structure. Please add...

  • Solve it for java Question Remember: You will need to read this assignment many times to...

    Solve it for java Question Remember: You will need to read this assignment many times to understand all the details of the you need to write. program Goal: The purp0se of this assignment is to write a Java program that models an elevator, where the elevator itself is a stack of people on the elevator and people wait in queues on each floor to get on the elevator. Scenario: A hospital in a block of old buildings has a nearly-antique...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT