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

CS 370- Fall 2017 Lab 6-Simulating Ferries with Stacks and Queues Objective: One

ID: 3596444 • Letter: C

Question

CS 370- Fall 2017 Lab 6-Simulating Ferries with Stacks and Queues Objective: One of the most interesting and useful things we can do with computers is to construct a simulation of some system, run the simulation program, a the simulation to provide us with additional insight into the behavior of the system. We have spent some time in class discussing three ADTs: lists, stacks and queues. These can be used to simulate different kinds of systems. nd use the results of running Background Prior to the completion of the Skye Bridge in 1995, you could reach the Isle of Skye in the Scottish Highlands only from two points on the mainland: Kyle of Lochalsh and Mallaig. Each town ran a car ferry service -a boat that takes cars (and people) to Skye and back. A ferry between the villages of Kyle of Lochalsh on the mainland and Kyleakin on the island's east coast operated from around the year 1600 until the bridge was completed. The ferry from Mallaig to Armadale still runs regularly today Each Mallaig ferry has a single automobile entrance. The cars drive on and go as far back in the boat as they can; it takes about a minute to load a car onto the ferry. The ferry takes about 20 minutes to make the crossing. When the ferry reaches Skye, the cars leave the boat by the same entrance, so the car at the far end of the boat is the last one off. When it is a car's turn, it takes about a minute for a car to drive off the ferry. (Is the Mallaig ferry more like a list, a stack, or a queue?) Each ferry from Kyle of Lochalsh has a ramp at both ends. The cars drive onto the boat using the ramp at one end; each car drives as far down the boat as possible; takes about a minute to load a car onto the ferry. The ferry takes about 20 minutes to make the crossing. When the ferry reaches Skye, it docks so that the ramp at the other end connects with the dock and the cars drive straight off. When it is a car's turn, i takes about a minute for a car to drive off the ferry. (Is the Kyle of Lochalsh ferry more like a list, a stack, or a queue?) What we want to know is is, which ferry is more efficient

Explanation / Answer

// This is the code which contains the three classes

package chegg;

import java.util.Arrays;

import java.util.Calendar;

class Stack {

private int maxSize;

private long[] stackArray;

private int top;

public Stack(int s) {

maxSize = s;

stackArray = new long[maxSize];

top = -1;

}

public void push(long j) {

stackArray[++top] = j;

}

public long pop() {

return stackArray[top--];

}

public long peek() {

return stackArray[top];

}

public boolean isEmpty() {

return (top == -1);

}

public boolean isFull() {

return (top == maxSize - 1);

}

}

class Queue {

private int capacity;

int queueArr[];

int front = 0;

int rear = -1;

int currentSize = 0;

public Queue(int queueSize){

this.capacity = queueSize;

queueArr = new int[this.capacity];

}

public void enqueue(int item) {

if (isQueueFull()) {

//System.out.println("Overflow ! Unable to add element: "+item);

} else {

rear++;

if(rear == capacity-1){

rear = 0;

}

queueArr[rear] = item;

currentSize++;

//System.out.println("Element " + item+ " is pushed to Queue !");

}

}

public void dequeue() {

if (isQueueEmpty()) {

//System.out.println("Underflow ! Unable to remove element from Queue");

} else {

front++;

if(front == capacity-1){

//System.out.println("Pop operation done ! removed: "+queueArr[front-1]);

front = 0;

} else {

//System.out.println("Pop operation done ! removed: "+queueArr[front-1]);

}

currentSize--;

}

}

public boolean isQueueFull(){

boolean status = false;

if (currentSize == capacity){

status = true;

}

return status;

}

public boolean isQueueEmpty(){

boolean status = false;

if (currentSize == 0){

status = true;

}

return status;

}

}

public class ferry {

public static void main(String[] args) {

// TODO Auto-generated method stub

Stack carStack = new Stack(20);

Queue carQueue = new Queue(20);

long inTime[] = new long[20];

long outTime[] = new long[20];

long removeTime[] = new long[20];

long spentTime[] = new long[20];

long avg =0;

int i;

for (i=0;i<20;i++)

{

inTime[i]=Calendar.getInstance().getTimeInMillis();

carStack.push(i);

avg+= Calendar.getInstance().getTimeInMillis()-inTime[i];

}

System.out.println("Avarage time to push a car in the stack " + avg/20);

i=0;

avg=0;

long j;

for (i=0;i<20;i++)

{

removeTime[i]=Calendar.getInstance().getTimeInMillis();

j = carStack.pop();

outTime[(int)j]=Calendar.getInstance().getTimeInMillis();

avg+= Calendar.getInstance().getTimeInMillis()-removeTime[i];

}

System.out.println("Avarage time to remove a car from the stack " + avg/20);

avg=0;

for(i=0;i<20;i++)

{

spentTime[i]=outTime[i]-inTime[i];

avg+=spentTime[i];

}

Arrays.sort(spentTime);

System.out.println("Maxtime a car spent in the stack "+ spentTime[19]);

System.out.println("Mintime a car spent in the stack "+ spentTime[0]);

System.out.println("Avg time spent by a car in the stack " + avg/20);

avg=0;

for (i=0;i<20;i++)

{

inTime[i]=Calendar.getInstance().getTimeInMillis();

carQueue.enqueue(i);

avg+= Calendar.getInstance().getTimeInMillis()-inTime[i];

}

System.out.println("Avarage time to push a car in the queue " + avg/20);

i=0;

avg=0;

j=0;

for (i=0;i<20;i++)

{

removeTime[i]=Calendar.getInstance().getTimeInMillis();

carQueue.dequeue();

outTime[i]=Calendar.getInstance().getTimeInMillis();

avg+= Calendar.getInstance().getTimeInMillis()-removeTime[i];

}

System.out.println("Avarage time to remove a car from the queue " + avg/20);

avg=0;

for(i=0;i<20;i++)

{

spentTime[i]=outTime[i]-inTime[i];

avg+=spentTime[i];

}

Arrays.sort(spentTime);

System.out.println("Maxtime a car spent in the queue "+ spentTime[19]);

System.out.println("Mintime a car spent in the queue "+ spentTime[0]);

System.out.println("Avg time spent by a car in the queue " + avg/20);

}

}

//Sample output of the code which shows beautifully that stack is working with last in 1st out so max time a car spend is very high and min time a car spend is very low. In case of queue it is first in first out so not much diff betwwen max and min waiting time /// this is the sample output below

Avarage time to push a car in the stack 12
Avarage time to remove a car from the stack 0
Maxtime a car spent in the stack 252
Mintime a car spent in the stack 2
Avg time spent by a car in the stack 17
Avarage time to push a car in the queue 0
Avarage time to remove a car from the queue 0
Maxtime a car spent in the queue 4
Mintime a car spent in the queue 3
Avg time spent by a car in the queue 3