Я работал над этим больше, но это не дает мне правильного вывода. Я не знаю, почему общее время ожидания всегда сводится к нулю.
The purpose of this assignment is to perform simulation of a building's elevator system that processes passenger requests. You will need to input parameters to the simulation representing the number of floors in the building, the number of elevators, the simulation length in time units, and the percent chance that a passenger will place a new elevator request during each time unit. The assignment requires to have all the classes provided in that particular output format.
Что я уже пробовал:
public class Request { private int sourceFloor; private int destinationFloor; private int timeEntered; private int max; private int arrival; public Request(int numFloor){ setSourceFloor((int) (Math.random()*numFloor+1)); setDestinationFloor((int) (Math.random()*numFloor+1)); this.max=max; } public int getSourceFloor() { return sourceFloor; } public void setSourceFloor(int sourceFloor) { this.sourceFloor = sourceFloor; } public int getDestinationFloor() { return destinationFloor; } public void setDestinationFloor(int destinationFloor) { this.destinationFloor = destinationFloor; } public int getTimeEntered() { return timeEntered; } public void setTimeEntered(int timeEntered) { this.timeEntered = timeEntered; } public int getMax() { return max; } public void setMax(int max) { this.max = max; } public int getArrival() { return arrival; } public void setArrival(int arrival) { this.arrival = arrival; } }
import java.util.LinkedList; public class RequestQueue extends LinkedList<Request>{ private LinkedList<Request> s = new LinkedList<>(); private int size; public RequestQueue(){ s=null; size=0; } public Request dequeue(){ size--; return this.removeFirst(); } public Request pop()throws IllegalArgumentException{ if(isEmpty()==false) return dequeue(); else throw new IllegalArgumentException(); } public boolean isEmpty(){ if(this.size()<=0){ return true; }else{ return false; } } public int size(){ return this.size; } public void enqueue(Request request){ size++; this.addLast(request); } public void push(Request request){ this.enqueue(request); } }
public class Elevator { private int currentFloor; private int elevatorState; private Request request; private final int IDLE = 0; private final int TO_SOURCE = 1; private final int TO_DESTINATION = 2; public Elevator(){ setRequest(null); setElevatorState(IDLE); setCurrentFloor(1); } public int getCurrentFloor() { return currentFloor; } public void setCurrentFloor(int currentFloor) { this.currentFloor = currentFloor; } public int getElevatorState() { return elevatorState; } public void setElevatorState(int elevatorState) { this.elevatorState = elevatorState; } public Request getRequest() { return request; } public void setRequest(Request request) { if(this.getElevatorState()==IDLE) this.request = null; else this.request = request; } public int getTO_SOURCE() { return TO_SOURCE; } public int getTO_DESTINATION() { return TO_DESTINATION; } }
public class Simulator { public static void simulate(double probability,int numFloors,int numElevators,int lengthSim) throws FullQueueException, EmptyQueueException{ RequestQueue s = new RequestQueue(); BooleanSource arrival =new BooleanSource(probability); int totalWaitTime=0; int done=0; double avgWaitTime =(double)totalWaitTime/done; int currentSecond; Elevator[] elevators=new Elevator[numElevators]; for ( currentSecond = 1;currentSecond <= lengthSim;currentSecond++){ if(arrival.occurs()){ Request r=new Request(numFloors); r.setTimeEntered(currentSecond); s.push(r); } for(int i = 0; i < numElevators; i++){ Elevator j=new Elevator(); elevators[i]=j; elevators[i] = new Elevator(); if(elevators[i].getElevatorState()==0 && !(s.isEmpty())){ elevators[i].setElevatorState(1); elevators[i].setRequest(s.pop()); } if(elevators[i].getElevatorState()==1){ if(elevators[i].getRequest().getSourceFloor() > elevators[i].getCurrentFloor()){ elevators[i].setCurrentFloor(elevators[i].getCurrentFloor()+1); if(elevators[i].getRequest().getSourceFloor() == elevators[i].getCurrentFloor()){ elevators[i].getRequest().setArrival(currentSecond); totalWaitTime +=elevators[i].getRequest().getArrival()-elevators[i].getRequest().getTimeEntered(); done++; elevators[i].setElevatorState(2); } } else if(elevators[i].getRequest().getSourceFloor() < elevators[i].getCurrentFloor()){ elevators[i].setCurrentFloor(elevators[i].getCurrentFloor()-1); if(elevators[i].getRequest().getSourceFloor() == elevators[i].getCurrentFloor()){ elevators[i].getRequest().setArrival(currentSecond); totalWaitTime+=elevators[i].getRequest().getArrival()-elevators[i].getRequest().getTimeEntered(); done++; elevators[i].setElevatorState(2); } } else{ if(elevators[i].getRequest().getSourceFloor() == elevators[i].getCurrentFloor()){ elevators[i].getRequest().setArrival(currentSecond); totalWaitTime+=elevators[i].getRequest().getArrival()-elevators[i].getRequest().getTimeEntered(); done++; elevators[i].setElevatorState(2); } } } else if(elevators[i].getElevatorState()==2){ if(elevators[i].getRequest().getSourceFloor() > elevators[i].getCurrentFloor()){ elevators[i].setCurrentFloor(elevators[i].getCurrentFloor()+1); if(elevators[i].getRequest().getDestinationFloor()==elevators[i].getCurrentFloor()){ elevators[i].setElevatorState(0); } } else if(elevators[i].getRequest().getSourceFloor() < elevators[i].getCurrentFloor()){ elevators[i].setCurrentFloor(elevators[i].getCurrentFloor()-1); if(elevators[i].getRequest().getDestinationFloor() == elevators[i].getCurrentFloor()){ elevators[i].setElevatorState(0); } } else { if(elevators[i].getRequest().getSourceFloor() == elevators[i].getCurrentFloor()){ elevators[i].setElevatorState(0); } } } else if(elevators[i].getElevatorState()==0&&!(s.isEmpty())){ elevators[i].setElevatorState(1); elevators[i].setRequest(s.pop()); } } } System.out.println("Total Wait Time:"+totalWaitTime); System.out.println("Total Requests:"+done); System.out.printf("Average Wait Time: %.2f \n",avgWaitTime); } }
public class Analyzer { public static void main(String[] args) throws FullQueueException, EmptyQueueException{ System.out.println("Welcome to the Elevator simulator!"); System.out.println(""); Scanner input=new Scanner(System.in); System.out.print("Please enter the probability of arrival for Requests: "); double e=input.nextDouble(); while(e<0.0||e>1.0){ System.out.print("Invalid. Please try again: "); e=input.nextDouble(); } System.out.print("Please enter the number of floors: "); int y=input.nextInt(); while(y<2){ System.out.print("Invalid. Please try again: "); y=input.nextInt(); } System.out.print("Please enter the number of elevators: "); int u=input.nextInt(); while(u<1){ System.out.print("Invalid. Please try again: "); u=input.nextInt(); } System.out.print("Please enter the length of the simulation (in time units): "); int j=input.nextInt(); while(j<1){ System.out.print("Invalid. Please try again: "); j=input.nextInt(); } Simulator.simulate(e, y, u, j); } }
David_Wimbley
Вы действительно ожидаете, что кто-то прочитает эту домашнюю задачу и ответит на нее за вас? Я даже не вижу, где вы указываете ошибку в этом романе описания.
Вы должны использовать ссылку улучшить вопрос, чтобы дать четкое и краткое объяснение вашей ошибки, с которой вы столкнулись. Я почти уверен, что никто здесь не хочет читать весь этот текст.
Mohibur Rashid
Пожалуйста, будьте более конкретны
Member 13336693
Я улучшил его