Enjoy Upto 50% OFF on Assignment Solutions!
Sussex Car Park Java Project: Queue and Stack Data Structures Case Study By Native Assignment Help
Ph.D. Writers For Best Assistance
Plagiarism Free
No AI Generated Content
The problem of Sussex car park mainly focuses on increasing process smoothness and has been applied through better Java implementation. The building's front entry leads to the parking lot, and a road surrounds it that prohibits passing vehicles or turning around. Now, whenever a car approaches the structure, it parks as far away as it can and pulls into the next slot. All the vehicles that are obstructing a car's exit must leave the parking lot and line up on the road. Following the departure of the departing vehicle, the remaining vehicles enter the parking lot in the same sequence as they departed. A Java class d ‘SussexCarPark’ has been used to implement the simulation.
Connect with us ASAP and get the same Paper!
Order AI-FREE ContentFigure 1: Classes
There has been the creation of 7 classes within this, these are ‘CarPark’, ‘CircularArrayQueue’, ‘LinkedListStack’, ‘Queue’, ‘Stack’, ‘SussexCarPark’ (contains main class), and Tests. It is evident from the above figure after running tests, none of the classes has any errors (Higo et al. 2022). Thus, it is also evident that the creation of classes has been successful for the project.
import java.util.LinkedList;
import java.util.Queue;
public class CarPark{
private Queue<String> carQueue; // to store the car owners
public CarPark() {
carQueue = new LinkedList<>();
}
public int arrive(String carOwner) {
carQueue.add(carOwner); // add car owner to the queue
return carQueue.size(); // return the number of cars in the queue
}
public String leave(String carOwner) {
String result = "Alice"; // initialize result string
// check if the car owner is in the queue
if (!carQueue.contains(carOwner)) {
System.out.println(carOwner + " is not in the car park.");
return null;
}
// remove cars from the front of the queue until the car owner is reached
while (!carQueue.peek().equals(carOwner)) {
result += carQueue.poll() + "-"; // add car owner to the result string
}
carQueue.remove(); // remove the car owner from the queue
return result;
}
}
Class Carpark
It can be seen from the above mentioned that the ‘Queue’ interface which is applied as a method, provides a ‘LinkedList’ implementation of a ‘queue’. It should be mentioned that the ‘Queue’ interface determines methods like “add(), remove(), and peek()” in terms of adding, removing, or getting the first element of the queue respectively. Thus, it is evident from this that method has been applied effectively (Rani et al. 2021). The ‘add ()’ method is employed in the ‘arrive ()’ method to add a new automobile owner to the end of the queue. The queue's current size is returned via the ‘size ()’ method. The ‘contains ()’ method is used in the ‘leave ()’ method to determine whether the specified automobile owner is ahead of the line. The method prints a message and returns null if the owner of the car cannot be located.
import java.util.LinkedList;
// SussexCarPark which extends a CarPark class
public class SussexCarPark extends CarPark {
//private instance variable cars which is a LinkedList of Strings
private LinkedList<String> cars = new LinkedList<>();
//arrive method takes a String parameter carOwner
public int arrive(String carOwner) {
cars.addLast(carOwner);
return cars.size();
}
//leave method takes a String parameter carOwner
public String leave(String carOwner) {
int index = cars.indexOf(carOwner);
if (index == -1) {
return null;
}
String result = "";
for (int i = 0; i < index; i++) {
result += cars.get(i) + "-";
}
cars.remove(index);
return result;
}
public static void main(String[] args) {
SussexCarPark carPark = new SussexCarPark();
System.out.println(carPark.arrive("Alice")); // 1
System.out.println(carPark.arrive("Bob")); // 2
System.out.println(carPark.arrive("Charlie")); // 3
System.out.println(carPark.leave("Charlie")); // Alice-Bob-
System.out.println(carPark.leave("Alice")); // Bob-
System.out.println(carPark.leave("Eve")); // null
}
}
Class SussexCarPark
The class ‘SussexCarPark’, which extends the ‘CarPark’ class, is defined which can be seen from the above Java code snippets. ‘cars’, a LinkedList of Strings is also a ‘private instance variable’ (Higo et al. 2020). The arrival function uses the ‘addLast’ method to add the String parameter ‘carOwner’ to the end of the LinkedList. The LinkedList's new size is then returned. On the other hand, the ‘indexOf’ method is used by the leave method to locate the String parameter ‘carOwner’ in the LinkedList. It returns null if the ‘carOwner’ cannot be located (Ogala and Ojie, 2020). In case it's not, then it uses the remove function to remove the ‘carOwner’ from the linked list and produces a string that contains a list of all the cars parked before the one being removed, each car's separated by a dash. The ‘main function’ creates a ‘SussexCarPark’ instance, which uses the ‘arrive’ method to add three vehicles, and the ‘leave’ method to take away two of them from the parking lot. Finally, the values returned by the leave method are then printed. The below figure is suggesting on the successful completion of the main class and the resulting output.
Figure 2: Result output from the SussexCarPark
public class CircularArrayQueue implements Queue{
private String[] arrayQueue;
//How many items in the queue
private int size;
//Maximum items that could be stored
private int maxSize;
private int front;
private int rear;
/**
* Constructor initialises CircularArrayQueue fields
* @param maxSize
*/
public CircularArrayQueue(int maxSize) {
this.maxSize = maxSize;
size = 0;
arrayQueue = new String[maxSize];
front = 0;
rear = 0;
}
/**
* Adds an object to the back of the queue
*
* @param data
*/
@Override
public void enqueue(String data) {
if (size < maxSize) {
arrayQueue[rear] = data;
rear = (rear + 1) % maxSize;
size++;
} else {
System.out.println("Queue is full!");
}
}
/**
* Removes an object from the front of the queue
* @return
*/
@Override
public String dequeue() {
String item = null;
if (size != 0) {
item = arrayQueue[front];
front = (front + 1) % maxSize;
size--;
}
return item;
}
/**
* Returns the item at the front of the queue without removing it
* @return
*/
@Override
public String front() {
String item = null;
if (size != 0) {
item = arrayQueue[front];
}
return item;
}
/**
* Returns the number of items in the queue
* @return
*/
@Override
public int size() {
return size;
}
/**
* Returns true if the queue is empty
* @return
*/
@Override
public boolean isEmpty() {
return size == 0;
}
/**
* Prints the contents of the queue
*/
@Override
public void printAll() {
if (size > 0) {
int i = front;
do {
System.out.print(arrayQueue[i] + " ");
i = ++i % maxSize;
} while (i != rear);
System.out.println();
} else {
System.out.println("Queue empty");
}
}
}
Class ‘CircularArrayQueue’
The above mentioned is suggesting on the circular array class which effectively implements Queue. The above code essentially defines a “Java class” by the of “CircularArrayQueue” which implements the “Queue” interface. This is the generic implementation regarding the “queue” data structure utilizing an “array” with the circular buffer for storing the elements.
The class contains “private” instance variables “arrayQueue”, “maxSize”, “size”, “rear”, and “front” respectively. The “aeeayQueue” refers to the array which holds the concerned elements of the “queue”. The “size’ is the maximum quantity of the elements within the queue. The “maxSize” if the most quantity of the elements which can be stored in particular. The “front” is also the index regarding the very first element within the queue, whereas the rear pertains to the index of the very last element inside the queue.
The constructor d “class” initializes the size, arrayQueue, maxSize instance variables to the values specified. The sets front as well as reas to zero showcases the very beginning of the concerned queue. The method d “front” returns all of the elements at the very front of the “queue” without even removing them. Essentially it returns the null when the queue stays empty. The method d “size” returns the present quantity of the elements within the queue. Moreover, the method called “isEmpty” returns the Boolean values which indicates whether that queue stays empty or not in particular.
The “printAll” method essentially prints all of the elements pertaining to the “queue” inside the order in which they are added into the queue. When the underlying size of that “queue” is zero, then this method prints the message that represents the fact that the “queue” is empty.
The “circular array queue” data structure has been selected for showcasing the car park and the road. The sole reason is that it permits the effective insertion as well as deletion of the elements in a “FIFO” manner, which turns out to be an ideal to simulate the vehicles entering as well as leaving the car park. This circular nature enables the efficient usage of space along with preventing the wastage of the memory for that matter. Moreover, this has got a fixed size that limits the very number of all the vehicles which can easily be simulated in this regard. Furthermore, the resizing of the queues might turn out to be very expensive when the factors of memory and time are taken into account.
A primary advantage in utilizing the “Linked List” as a concrete “data structure” for the “Stack ADT” is its very dynamic nature. The “Linked Lists” can easily shrink or grow dynamically on the basis of the amount of the elements they contain, enabling the efficient usage of memory. Furthermore, the “Linked Lists” permits for the constant “time insertion” along with deletion of the elements form the very top of the “stack”. This in turn makes it extremely suitable for the applications which need frequent push as well as pop operations.
One limitations when it comes to the “Linked Lists” is that these need an extra amount of memory for storing the pointers towards that very next element, which makes it less “memory” efficient in comparison to the arrays. The accessing of the elements at a particular index within a ‘Linked Lists” requires iteration all throughout the lists from the very beginning. This in turn can be much slower than the direct access rendered by the arrays.
import java.util.LinkedList;
import java.util.ListIterator;
public class LinkedListStack <T> implements Stack<T>{
private LinkedList<T> stack;
private int size;
public LinkedListStack() {
stack = new LinkedList<>();
size = 0;
}
@Override
public T peek() {
return stack.peekFirst();
}
@Override
public T pop() {
T t = stack.removeFirst();
size--;
return t;
}
@Override
public void push(T t) {
stack.addFirst(t);
size++;
}
@Override
public boolean isEmpty() {
return size == 0;
}
@Override
public int size() {
return size;
}
@Override
public void printAll() {
// Setting the ListIterator
ListIterator listIter = stack.listIterator();
// Iterating through the created list from the position
while(listIter.hasNext()){
System.out.println(listIter.next());
}
}
}
Class ‘LinkedListStack.class’
This is explained all above in the class ‘’LinkedListStack’and it can be seen implementing Stack.
public interface Queue {
/**
* Adds an object to the back of the queue
* @param data
*/
public void enqueue(String data);
/**
* Removes an object from the front of the queue
* @return
*/
public String dequeue();
/**
* Returns the item at the front of the queue without removing it
* @return
*/
public String front();
/**
* Returns the number of items in the queue
* @return
*/
public int size();
/**
* Returns true if the queue is empty
* @return
*/
public boolean isEmpty();
/**
* Prints the contents of the queue
*/
public void printAll();
}
Class ‘Queue.class’
This is suggested in the above of the Queue class that uses public interface as ‘Queue’.
public interface Stack<T> {
public T peek();
public T pop();
public void push(T t);
public boolean isEmpty();
public int size();
public void printAll();
}
Class ‘Stack.class’
It can be viewed from the above mentioned that Class Stack has been implemented.
public class Tests {
private static final String[] owners = {"Anne", "Bob", "Chen", "Diego"};
/**
* Adds one owner to the car park
* @return true if arrive returns 1
*/
public static boolean testOne() {
SussexCarPark sussexCarPark = new SussexCarPark();
return sussexCarPark.arrive(owners[0]) == 1;
}
/**
* Adds and then removes one owner to the car park
* @return empty String as car park is empty
*/
public static boolean testTwo() {
SussexCarPark sussexCarPark = new SussexCarPark();
sussexCarPark.arrive(owners[0]);
return sussexCarPark.leave(owners[0]).isEmpty();
}
/**
* The first car in the car park leaves
* @return true if String is "Chen-Bob-"
*/
public static boolean testThree() {
SussexCarPark sussexCarPark = new SussexCarPark();
for (int i = 0; i < 3; i++) {
sussexCarPark.arrive(owners[i]);
}
return sussexCarPark.leave(owners[0]).equals(String.format("%s-%s-",owners[2], owners[1]));
}
/**
* Car park has three cars - tries to remove a car not in the car park
* @return true if result == null
*/
public static boolean testFour() {
SussexCarPark sussexCarPark = new SussexCarPark();
for (int i = 0; i < 3; i++) {
sussexCarPark.arrive(owners[i]);
}
return sussexCarPark.leave(owners[3]) == null;
}
/**
* Add 3 cars to the car park
* @return true if arrive returns 3
*/
public static boolean testFive() {
SussexCarPark sussexCarPark = new SussexCarPark();
int numCars = 0;
for (int i = 0; i < 3; i++) {
numCars = sussexCarPark.arrive(owners[i]);
}
return numCars == 3;
}
/**
* Add three cars to the car park, Last car leaves and then returns
* @return true if arrive returns 3
*/
public static boolean testSix() {
SussexCarPark sussexCarPark = new SussexCarPark();
for (int i = 0; i < 3; i++) {
sussexCarPark.arrive(owners[i]);
}
sussexCarPark.leave(owners[2]);
return sussexCarPark.arrive(owners[2]) == 3;
}
/**
* Adds three cars to the car park. Last car added leaves
* @return true if leave returns an empty String
*/
public static boolean testSeven() {
SussexCarPark sussexCarPark = new SussexCarPark();
for (int i = 0; i < 3; i++) {
sussexCarPark.arrive(owners[i]);
}
return sussexCarPark.leave(owners[2]).isEmpty();
}
/**
* Add three cars. The first car added to the car park leaves
* @return true if arrive returns 3
*/
public static boolean testEight() {
SussexCarPark sussexCarPark = new SussexCarPark();
for (int i = 0; i < 3; i++) {
sussexCarPark.arrive(owners[i]);
}
sussexCarPark.leave(owners[0]);
return sussexCarPark.arrive(owners[0]) == 3;
}
/**
* Add three cars to the car park
* Bob leaves
* @return true if leave returns "Chen-"
*/
public static boolean testNine() {
SussexCarPark sussexCarPark = new SussexCarPark();
for (int i = 0; i < 3; i++) {
sussexCarPark.arrive(owners[i]);
}
return sussexCarPark.leave(owners[1]).equals(String.format("%s-",owners[2]));
}
/**
* Adds three cars to the car park
* The first car added to the car park leaves
* The last car added to the car park leaves
* @return true if leave returns "Bob-"
*/
public static boolean testTen() {
SussexCarPark sussexCarPark = new SussexCarPark();
for (int i = 0; i < 3; i++) {
sussexCarPark.arrive(owners[i]);
}
sussexCarPark.leave(owners[0]);
return sussexCarPark.leave(owners[2]).equals(String.format("%s-",owners[1]));
}
/**
* Adds three cars to the car park
* The second car added to the car park leaves
* The second car returns to the car park
* @return true if arrive returns 3
*/
public static boolean testEleven() {
SussexCarPark sussexCarPark = new SussexCarPark();
for (int i = 0; i < 3; i++) {
sussexCarPark.arrive(owners[i]);
}
sussexCarPark.leave(owners[1]);
return sussexCarPark.arrive(owners[1]) == 3;
}
public static void main(String[] args) {
System.out.printf("Test one %s%n", testOne());
System.out.printf("Test two %s%n", testTwo());
System.out.printf("Test three %s%n", testThree());
System.out.printf("Test four %s%n", testFour());
System.out.printf("Test five %s%n", testFive());
System.out.printf("Test six %s%n", testSix());
System.out.printf("Test seven %s%n", testSeven());
System.out.printf("Test eight %s%n", testEight());
System.out.printf("Test nine %s%n", testNine());
System.out.printf("Test ten %s%n", testTen());
System.out.printf("Test eleven %s%n", testEleven());
}
}
Class ‘Tests.java’
Finally, the above is explaining that 11 tests have been completed. The result should be viewed as either true or false statement.
Figure 9: 11 tests run and obtained results
The above figure is suggesting that all the predetermined tests have been successfully passed, thereby complying with the methods. It is evident from the figure that all the tests have run successfully defining the effectiveness of the system.
Conclusion
It can be said from the entire project that the successful completion of each of the scopes has been done with the correct Java methodologies.
References
Higo, Y., Hayashi, S. and Kusumoto, S., 2020. On tracking Java methods with Git mechanisms. Journal of Systems and Software, 165, p.110571.
Higo, Y., Matsumoto, S., Kusumoto, S. and Yasuda, K., 2022, May. Constructing dataset of functionally equivalent Java methods using automated test generation techniques. In Proceedings of the 19th International Conference on Mining Software Repositories (pp. 682-686).
Ogala, J.O. and Ojie, D.V., 2020. Comparative analysis of c, c++, c# and java programming languages. GSJ, 8(5), pp.1899-1913.
Rani, P., Panichella, S., Leuenberger, M., Di Sorbo, A. and Nierstrasz, O., 2021. How to identify class comment types? A multi-language approach for class comment classification. Journal of Systems and Software, 181, p.111047.
Go Through the Best and FREE Case Studies Written by Our Academic Experts!
Native Assignment Help. (2024). Retrieved from:
https://www.nativeassignmenthelp.co.uk/sussex-car-park-java-project-queue-and-stack-data-structures-case-study-23309
Native Assignment Help, (2024),
https://www.nativeassignmenthelp.co.uk/sussex-car-park-java-project-queue-and-stack-data-structures-case-study-23309
Native Assignment Help (2024) [Online]. Retrieved from:
https://www.nativeassignmenthelp.co.uk/sussex-car-park-java-project-queue-and-stack-data-structures-case-study-23309
Native Assignment Help. (Native Assignment Help, 2024)
https://www.nativeassignmenthelp.co.uk/sussex-car-park-java-project-queue-and-stack-data-structures-case-study-23309
Corporate Governance: A Diageo Plc Looking for top-notch assignment services...View or download
The Three Certainties in Trust Law: An Analysis of Intention, Subject, and...View or download
Architectural Innovation and Economic Transformation Strategies Part...View or download
Exploring the Affective Domain in Math: Enhancing Attitudes The...View or download
Strategies for Mitigating Political Risks in Pakistan The UK’s top-notch...View or download
Unveiling H&M's Approach to Streamlined Operations and Supply Chain Are...View or download
Get your doubts & queries resolved anytime, anywhere.
Receive your order within the given deadline.
Get original assignments written from scratch.
Highly-qualified writers with unmatched writing skills.
We utilize cookies to customize your experience. By remaining on our website, you accept our use of cookies. View Detail
Hi! We're here to answer your questions! Send us message, and we'll reply via WhatsApp
Please enter a messagePleae enter your phone number and we'll contact you shortly via Whatsapp
We will contact with you as soon as possible on whatsapp.
Ph.D. Writers For Best Assistance
Plagiarism Free
No AI Generated Content
offer valid for limited time only*