Allocation of points for all assignments: I. 35% of the points are allocated to
ID: 3885221 • Letter: A
Question
Allocation of points for all assignments: I. 35% of the points are allocated to Documentation 2, 65% of the points are allocated to programming, including pre-and post-conditions. Documentation: I. 2. 3. 4. 5. Explain the purpose of the program as detail as possible-8% Develop a solution for the problem and mention algorithms to be used-12% List data structures to be used in solution,-5%. Give a description of how to use the program and expected input/output-5% Explain the purpose of each class you develop in the program-5% Programming I. For each method, give the pre-and post-conditions and invariant, if any-10% 2. Program execution according to the requirements given-50% 3. Naming of program as required-5%. Description of program: You are to write a program name elevator.java that simulates one elevator services in a 12-floor building NOTE: There is only one elevator working this building Here are some requirements for the program: 1. The floors of the building are numbered 1 to 12 2. At the start of the program, randomly generate 8 of the 12 floors to stop at while going up. These must be placed in an ArrayList which should be sorted in ascending order. Also generate another set of 5 numbers representing floors to stop at when going down and place it in another ArrayList. Sort this ArrayList in descending order 3. Also at the start of the program the elevator must be located on the 1st floor and the first request is made from the ArrayList which must be a request to go up 4. From the 1st floor, the elevator can only go in one direction - up. And from the 12th floor the elevator can only go in one direction - down. For the other floors, the elevator can go in either direction i.e. if it is going down when you get on and you want to go up and the down ArrayList is empty, then the elevator could start going up from that floor. The same will be true if the elevator is going up when you get on, and you want to go down and the up ArrayList is empty, then it can start going down from that floor 5. The direction in which the elevator is going must be known at all times and the floor to which it is going. If the elevator is going up, the request could be anywhere from floor 2 to 12. If it is going down, it can be from 11 to 1, with 1 being the defaultExplanation / Answer
-> purpose of program is to present the utility of lift algo.
-> we will use the array and stack for this algo. to insert the data from array to stack, we will sort it and then we will add it to stack.
-> Simple while loop algo can be developed for this program, it is tried and tested.
-> Please find below the complete utility:
// A utility to represent the structure of elevator program execution.
import java.io.*;
import java.util.*;
import java.lang.*;
// A class to start the execution
class elevator
{
// floor count.
public static final int floorcount = 12;
// floor pass wait period in seconds
public static final int floorpasswait = 2;
// floor stop wait period in seconds
public static final int floorstopwait = 3;
// elevator current position
public static int elevatorcurrentposition = 1;
// direction
public static String dir = "up";
// upstacks and downstacks
public static Stack<Integer> upstack = new Stack<Integer>();
public static Stack<Integer> downstack = new Stack<Integer>();
// starting point
public static void main (String[] args) throws java.lang.Exception
{
while(true)
{
prepareupstack();
preparedownstack();
int nextdest = 1;
while(nextdest != 0)
{
if (dir == "up")
nextdest = pop(upstack);
if (dir == "down")
nextdest = pop(downstack);
if (elevatorcurrentposition == nextdest)
continue;
if (nextdest == 0)
{
if(dir == "up")
{
dir = "down";
nextdest = pop(downstack);
}
else if(dir == "down")
{
dir = "up";
nextdest = pop(upstack);
}
if (nextdest == 0)
break;
}
System.out.println(" ");
System.out.println("starting at floor " + elevatorcurrentposition);
if (dir == "up")
{
for(int i = elevatorcurrentposition; i < nextdest; i=i+0)
{
System.out.println("going " + dir + ". Now at floor: " + ++i);
elevatorcurrentposition = i;
waitfor(2);
}
System.out.print("stoping at floor " + elevatorcurrentposition + ". waiting for 3 seconds -> ");
waitfor(3);
}
else
{
for(int j = elevatorcurrentposition; j > nextdest; j=j+0)
{
System.out.println("going " + dir + ". Now at floor: " + --j);
elevatorcurrentposition = j;
waitfor(2);
}
System.out.print("stoping at floor " + elevatorcurrentposition + ". waiting for 3 seconds -> ");
waitfor(3);
}
}
System.out.println(" do you want to run elevator again? y or n:");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String s = br.readLine();
if (s.toLowerCase() != "y")
break;
}
}
public static void waitfor(int count)
{
for(int i = 0; i < count; i++)
{
if (count == 3)
{
System.out.print((i+1) + " ");
}
try{
Thread.sleep(1000);
} catch (Exception e) {
}
}
System.out.println();
}
// pull from stack
public static int pop(Stack<Integer> stack)
{
try
{
int val = stack.pop();
return val;
}
catch (Exception e)
{
return 0;
}
}
// function to generate the up-trip of elevator
// generate random numbers
// sort them and push to stack
public static void prepareupstack()
{
Random rand = new Random();
int[] randomfloors = new int[8];
System.out.println("Up ArrayList");
for(int i = 0; i < 8; i++)
{
randomfloors[i] = rand.nextInt(12) + 1;
System.out.print(randomfloors[i] + " ");
}
Arrays.sort(randomfloors);
System.out.println(" Sorted Up ArrayList");
for(int i = 0; i < 8; i++)
{
System.out.print(randomfloors[i] + " ");
}
for(int j = 7; j >= 0; j--)
{
upstack.push(randomfloors[j]);
}
}
// function to generate the down-trip of elevator
// generate random numbers
// sort them and push to stack
public static void preparedownstack()
{
Random rand = new Random();
int[] randomfloors = new int[5];
System.out.println(" down ArrayList");
for(int i = 0; i < 5; i++)
{
randomfloors[i] = rand.nextInt(12) + 1;
System.out.print(randomfloors[i] + " ");
}
Arrays.sort(randomfloors);
System.out.println(" Sorted down ArrayList");
for(int i = 4; i >= 0; i--)
{
System.out.print(randomfloors[i] + " ");
}
for(int j = 0; j < 5; j++)
{
downstack.push(randomfloors[j]);
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.