ocumentation: detail as possible-8% I. Explain the purpose of the program as 2.
ID: 3886492 • Letter: O
Question
ocumentation: detail as possible-8% I. Explain the purpose of the program as 2. Develop a solution for the problem and mention algorithms to be used-12% 3. list data structures to be used in solution. _ 5% 4. Give a description of how to use the program and expected input/output-5% 5. Explain the purpose of each class you develop in the program,-5% Programming I. 2. 3. For each method, give the pre and post conditions and invariant, if any-10% Program execution according to the requirements given-50% 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 whern going down and place it in another ArrayList Sort this Arravtist 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 12* 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 AayList is empty, then it can start going down from that floorExplanation / Answer
package Online;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Random;
//Class Elevator definition
public class Elevator
{
//Creates array list for going up
ArrayList<Integer> upArray;
//Creates array list for going down
ArrayList<Integer> downArray;
//Constructor
Elevator()
{
// Initialize the object
upArray = new ArrayList<Integer>();
downArray = new ArrayList<Integer>();
}//End of constructor
//For the floors going up and down.
//The upArray is filled with 8 unique integers between 1 and 12.
public void populateFloors()
{
// Object for creating new random numbers
Random numberGenerator = new Random();
//For up array
// Loop until the array contains 8 elements
while(upArray.size() < 8)
{
//Generate random number between 1 and 12
int generatedFloor = numberGenerator.nextInt(12) + 1;
//If the array does not contain a number already
if(!upArray.contains(generatedFloor))
{
// Add the floor
upArray.add(generatedFloor);
}//End of if
}//End of while loop
//For down array
//Loop until the array contains 5 elements
while(downArray.size() < 5)
{
//Generate random number between 1 and 12
int generatedFloor = numberGenerator.nextInt(12) + 1;
//If the array does not contain a number already
if(!downArray.contains(generatedFloor))
{
// Add the floor
downArray.add(generatedFloor);
}//End of if
}//End of while loop
//Sort the array in ascending order for upArray
Collections.sort(upArray);
//Sort the array in descending order for downArray
Collections.sort(downArray, Collections.reverseOrder());
}//End of method
//Method to move the Elevator Up
void moveElevatorUp(Elevator e)
{
//Loops 12 times for up move
for(int counter= 1; counter < 12;)
{
//Displays the elevator start floor
System.out.println("Starting at floor " + counter);
//Extracts a value from the upArray
for(int c: upArray)
{
//Checks if the counter value is equal to c then display the information once
if(counter == c)
{
//Increase the floor counter value by one and display the floor number
System.out.println(" Going Up: now at floor " + ++counter);
}//End of if
//If not same
else
{
//Loop till counter value is c
while(counter < c)
{
//Increase the floor counter by one and display the floor value
System.out.println(" Going Up: now at floor " + ++counter);
}//End of while
}//End of else
//Display the floor stop
System.out.print("Stopping at floor " + counter + " for 3 seconds ");
//Loops 3 times
for(int st = 1; st <= 3; st++)
//Display the pause time
System.out.print(st + ", ");
System.out.println();
}//End of inner for loop
}//End of outer for loop
}//End of method
//Method to move the Elevator Down
void moveElevatorDown(Elevator e)
{
//Loops 12 times for down move
for(int counter = 12; counter > 1;)
{
//Display the start floor number
System.out.println("Starting at floor " + counter);
//Extracts a value from downArray
for(int c: downArray)
{
//If counter value is equal to c then display the floor once
if(counter == c)
{
//Reduce the floor value by one and display the floor number
System.out.println(" Going down: now at floor " + (--counter));
}//End of if
//If not equal
else
{
//Loop till counter value is c
while(counter > c)
{
//Reduce the floor value by one and display the floor number
System.out.println(" Going down: now at floor " + (--counter));
}//End of while loop
}//End of else
//Display the floor stop
System.out.print("Stopping at floor " + counter + " for 3 seconds ");
//Loops 3 times
for(int st = 1; st <= 3; st++)
//Display the pause time
System.out.print(st + ", ");
System.out.println();
}//End of inner loop
}//End of outer loop
}//End of method
//Main method definition
public static void main(String[] args)
{
//Elevator class object created
Elevator elev = new Elevator();
//Generates upArray and downArray
elev.populateFloors();
//Displays upArray values
System.out.println(elev.upArray);
//Displays downArray values
System.out.println(elev.downArray);
//Moves the elevator up
elev.moveElevatorUp(elev);
//Moves the elevator down
elev.moveElevatorDown(elev);
}//End of main method
}//End of class
Sample Run 1:
[3, 5, 6, 7, 8, 9, 11, 12]
[7, 6, 5, 3, 1]
Starting at floor 1
Going Up: now at floor 2
Going Up: now at floor 3
Stopping at floor 3 for 3 seconds 1, 2, 3,
Going Up: now at floor 4
Going Up: now at floor 5
Stopping at floor 5 for 3 seconds 1, 2, 3,
Going Up: now at floor 6
Stopping at floor 6 for 3 seconds 1, 2, 3,
Going Up: now at floor 7
Stopping at floor 7 for 3 seconds 1, 2, 3,
Going Up: now at floor 8
Stopping at floor 8 for 3 seconds 1, 2, 3,
Going Up: now at floor 9
Stopping at floor 9 for 3 seconds 1, 2, 3,
Going Up: now at floor 10
Going Up: now at floor 11
Stopping at floor 11 for 3 seconds 1, 2, 3,
Going Up: now at floor 12
Stopping at floor 12 for 3 seconds 1, 2, 3,
Starting at floor 12
Going down: now at floor 11
Going down: now at floor 10
Going down: now at floor 9
Going down: now at floor 8
Going down: now at floor 7
Stopping at floor 7 for 3 seconds 1, 2, 3,
Going down: now at floor 6
Stopping at floor 6 for 3 seconds 1, 2, 3,
Going down: now at floor 5
Stopping at floor 5 for 3 seconds 1, 2, 3,
Going down: now at floor 4
Going down: now at floor 3
Stopping at floor 3 for 3 seconds 1, 2, 3,
Going down: now at floor 2
Going down: now at floor 1
Stopping at floor 1 for 3 seconds 1, 2, 3,
Sample Run 2:
[1, 3, 4, 6, 7, 8, 9, 12]
[12, 8, 5, 4, 2]
Starting at floor 1
Going Up: now at floor 2
Stopping at floor 2 for 3 seconds 1, 2, 3,
Going Up: now at floor 3
Stopping at floor 3 for 3 seconds 1, 2, 3,
Going Up: now at floor 4
Stopping at floor 4 for 3 seconds 1, 2, 3,
Going Up: now at floor 5
Going Up: now at floor 6
Stopping at floor 6 for 3 seconds 1, 2, 3,
Going Up: now at floor 7
Stopping at floor 7 for 3 seconds 1, 2, 3,
Going Up: now at floor 8
Stopping at floor 8 for 3 seconds 1, 2, 3,
Going Up: now at floor 9
Stopping at floor 9 for 3 seconds 1, 2, 3,
Going Up: now at floor 10
Going Up: now at floor 11
Going Up: now at floor 12
Stopping at floor 12 for 3 seconds 1, 2, 3,
Starting at floor 12
Going down: now at floor 11
Stopping at floor 11 for 3 seconds 1, 2, 3,
Going down: now at floor 10
Going down: now at floor 9
Going down: now at floor 8
Stopping at floor 8 for 3 seconds 1, 2, 3,
Going down: now at floor 7
Going down: now at floor 6
Going down: now at floor 5
Stopping at floor 5 for 3 seconds 1, 2, 3,
Going down: now at floor 4
Stopping at floor 4 for 3 seconds 1, 2, 3,
Going down: now at floor 3
Going down: now at floor 2
Stopping at floor 2 for 3 seconds 1, 2, 3,
Starting at floor 2
Stopping at floor 2 for 3 seconds 1, 2, 3,
Stopping at floor 2 for 3 seconds 1, 2, 3,
Stopping at floor 2 for 3 seconds 1, 2, 3,
Stopping at floor 2 for 3 seconds 1, 2, 3,
Going down: now at floor 1
Stopping at floor 1 for 3 seconds 1, 2, 3,
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.