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
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;
import java.util.Stack;

public class Main {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        Stack<Integer> parkingLot  = new Stack<>();
        Stack<Integer> street  = new Stack<>();
        Queue<Integer> waitingCars  = new LinkedList<>();

        while (true) {
            System.out.println("\nEnter the positive license number to park the car");
            System.out.println("Enter the negative license number to remove the car");
            System.out.println("Enter zero to stop simulation");
            int licensePlatenumber;
            try{
                licensePlatenumber = Integer.parseInt(sc.nextLine());
            } catch (Exception e){
                System.out.println("You do not have the permit to park here\n");
                continue;
            }
            if(licensePlatenumber == 0){
                break; // User entered zero so stop the simulation.
            }
            else if(licensePlatenumber > 0){ // positive number means user wants to park the car in the lot.
                if(parkingLot.size() < 20){
                    parkingLot.push(licensePlatenumber);
                }
                else{
                    waitingCars.add(licensePlatenumber);
                }
                System.out.println("Parking lot after this operation has following  cars" + parkingLot);
                System.out.println("following cars are waiting in queue" + waitingCars);
            }
            else{ // negative number means user wants to remove the car from the lot.
                licensePlatenumber = -1 * licensePlatenumber;
                if(parkingLot.search(licensePlatenumber) == -1){
                    System.out.println("This car is not present in the parking lot\n"); // When user enters a number which is not present in the parking lot.
                }
                else{
                    while(parkingLot.peek()!= licensePlatenumber){
                        street.push(parkingLot.peek());
                        parkingLot.pop();
                    }
                    parkingLot.pop();
                    while(!street.empty()){
                        parkingLot.push(street.peek());
                        street.pop();
                    }
                    if(waitingCars.size() > 0){
                        parkingLot.push(waitingCars.remove());
                    }
                }
                System.out.println("Parking lot after this operation has following  cars" + parkingLot);
                System.out.println("following cars are waiting in queue" + waitingCars);
            }
        }
    }
}

Main x Enter the positive license number to park the car Enter the negative license number to remove the car Enter zero to st

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