Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Code in java 1 - Design an interactive simulation for a hamburger stand: * There are 1 thru N serving lines (10 maximum) * Customers

Code in java

1 - Design an interactive simulation for a hamburger stand:

* There are 1 thru N serving lines (10 maximum)

* Customers arrive at the rate of 0 to M customers per minute and goes to the shortest queue

* Customers wait until they are serviced (take X minutes median time)

* Simulate to T hours

2 - Read from command line the values N and M and and X and T - Prompt if missing and after the 1st pass

3 - Run the simulation using the Java random number generator to generate customers and their order processing time

* Use Normal / Gaussian distribution for service time, centered on X

4 - Keep track of:

* Average queue length

* Average customer wait time to be served

* Maximum queue length

* Maximum customer wait time

* How often the queue is empty

* How often a customer is served AFTER a customer that came after him

5 - At the end of each simulation, display statistic for the run

6 - Loop (step 2-6) simulating until user asks to stop

Hints:

- Create a small ADT called Customer - Each instance should keep trach of: arrival time, departure time, ...

- Create your own queue ADT - Do not use built-in

- Use either time-driven or event-driven simulation

- Run the simulation with 3 times with different input such:

* One simulation shows a store with sometimes empty queue (Off-peak time)

* One simulation shows a arrival rate about the same as departure rate (Desired maximum efficiency)

* One simulation shows a gradual lengthling of the queue (Peak time)

This is what I have so far:

Simulation.java

import java.util.Scanner; public class Simulation{ private static int n; private static int m; private static int x; private static int t; private static int hour = 8; private static int minute = 0; private static int day = 1; private static Gen random = new Gen(); private static final boolean DEBUG = true; public static void main(String args[]){ //We declare our first keyboard. Scanner keyboard = new Scanner(System.in); $("Starting program"); boolean timeToExit = false; while(!timeToExit){ try{ System.out.println("Getting input...."); n = Integer.parseInt(args[0]); m = Integer.parseInt(args[1]); x = Integer.parseInt(args[2]); t = Integer.parseInt(args[3]); } catch(Exception e){ System.out.println("Failed input.... try again input 4 values delimitted by spaces"); String tempInput = keyboard.nextLine(); args = tempInput.split(" "); continue; } $("Making " + n + " lines"); ServingLine lines = new ServingLine(n, 8); $("Starting sim loop..."); Customer temp; for(int count = 0; count < t * 60; count++){ tick(); for(int rate = 0; rate < m; rate++){ temp = new Customer(hour, minute); $(temp + " Has joined the line"); lines.joinLine(temp); } if( lines != null || lines.nOfLines != 0) lines.ellapse(hour, minute); if( minute % 10 == 0 ) $("Time = " + hour + ":" + minute); lines.serve(); } timeToExit = true; } } private static void $(String in){ if(DEBUG) try{ System.out.printf("$ %s ", in); } catch (Exception e ){} } private static void tick(){ if(hour == 21 ){ day++; hour = 8; } else if ( minute == 60 ){ hour++; minute = 0; } else minute++; } }

Gen.java

import java.util.Random; import java.util.concurrent.ThreadLocalRandom; public class Gen{ private String [] names = {"Adelaida", "Adelaide", "Adele", "Adelia", "Adelina"}; private Random rand; public Gen(){rand = new Random(); } //Gaussian Random gen public double gGen(int input){ return Math.abs(rand.nextGaussian() * input);} //double gen public double dGen(int input){return Math.abs(rand.nextDouble() * input); } public int rGen(int min, int max){ return ThreadLocalRandom.current().nextInt(min, max + 1);} //checks if they are fat public boolean scale(){ float chance = rand.nextFloat(); if (chance <= 0.357f) return true; return false; } //name gen public String nGen(){ int index = rand.nextInt((3833) + 1); return names[index]; } }

ServingLine.java

import java.util.Arrays;

public class ServingLine { public int nOfLines; Line[] lines; public ServingLine(int n, int max) { n = ( n < 1 ) ? 1 : n; n = ( n > 10 ) ? 10 : n; lines = new Line[n]; nOfLines = n; for(int count = 0; count < n; count++){ lines[count] = new Line(max); } } public boolean joinLine(Customer newCustomer){ Arrays.sort(lines); if( lines[0].getLength() == lines[0].max ) return false; else try { lines[0].getInLine(newCustomer); } catch (Exception e){ } return true; } public void serve(){ for(int count = 0; count < nOfLines; count++){ lines[count].serve(); } } public void ellapse(int hour, int minute){ for(int count = 0; count < nOfLines; count++){ lines[count].ellapse(hour, minute); } } }

Customer.java

public class Customer{ private String name; private int age; private boolean isFat; private int[] arrival; private int[] leave; private int wait = -1; private Gen customerRand = new Gen(); public Customer(int hour, int minute){ this.name = customerRand.nGen(); this.age = (int) customerRand.gGen(17); this.isFat = customerRand.scale(); arrival = leave = new int[]{hour, minute}; } public String toString(){ return "My name is " + name + " and I am " + age + " years old, " + (( isFat == true ) ? "sadly I am overweight " : "I can happilly say I am not fat. " ) ; } public void leave(int hour, int minute){ if(wait == 0) leave = new int[]{hour, minute}; } public int[][] getTime(){ int result[][] = {arrival, leave}; return result; } public void getServed(){ if(wait == -1) wait = customerRand.rGen(2, 14); } public boolean ellapse(){ if(wait == 0){ return false; } else { wait--; return true; } } }

Line.java

public class Line { private static int fights = 0; private static int cuts = 0; private Customer[] customers; public int max; private int backOfLine = 0; private Gen random = new Gen(); private boolean hasBeenServed = false; public Line(int length){ customers = new Customer[length]; this.max = length; } public void getInLine(Customer newCustomer) throws Exception{ if( backOfLine == 0 ){ customers[0] = newCustomer; backOfLine++; } else { if( backOfLine == 5 ){ throw new Exception("Hey buddy, there is no room in this line, get lost." ); } else { customers[backOfLine] = newCustomer; backOfLine++; if( random.gGen(1) % 2 == 0) { cuts++; if( random.gGen(1) % 2 == 0 ){ fights++; } } } } } public Customer getOutOfLine(){ if(backOfLine == 5 ){ int temp = backOfLine; backOfLine--; return customers[temp]; } int temp = backOfLine; backOfLine--; hasBeenServed = false; return customers[temp]; } public int getLength(){ return backOfLine; } public void ellapse(int hour, int minute){ if(customers[0].ellapse()){ customers[0].leave(hour, minute); getOutOfLine(); } } public void serve(){ if( !hasBeenServed ) { customers[0].getServed(); hasBeenServed = true; } } }

My problem is that whenever i do enter the 4 values with the space in between i get an Array index out of bounds exception error:

java.lang.ArrayIndexOutOfBoundsException: 907 at Gen.nGen(Gen.java:25) at Customer.(Customer.java:10) at Simulation.main(Simulation.java:41) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) at java.lang.reflect.Method.invoke(Unknown Source) at edu.rice.cs.drjava.model.compiler.JavacCompiler.runCommand(JavacCompiler.java:267)

Please help me perfect my code

Thank you

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Marketing The Ultimate Marketing Tool

Authors: Edward L. Nash

1st Edition

0070460639, 978-0070460638

Students also viewed these Databases questions