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

You are to write a program name elevator.java that simulates one elevator servic

ID: 3883684 • Letter: Y

Question

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 Array List 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 1t floor and the first request is made from the ArrayList which must be a request to go up 4. From the 1t 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 ArrayList is empty, then it can start going down from that floor. 5. The direction in which the clevater 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 anywbere from floor 2 to 12. If it is going down, it can be from 11 to I, with 1 being the default.

Explanation / Answer

Explanation::

Code in java::

import java.util.ArrayList;

import java.util.Collections;

import java.util.Random;

import java.util.Scanner;

public class elevator {

public static void main(String[] args) {

while(true){

/*

* Creating object of Random class named rand to generate random

* integers.

*/

Random rand=new Random();

  

/*

* Creating two ArrayList's named up and down respectively.

* Both are declared with size 12.

*/

ArrayList<Integer> up=new ArrayList<Integer>(12);

ArrayList<Integer> down=new ArrayList<Integer>(12);

  

/*

* Following is work on up operations where 8 random integers

* are generated.

* Here random number has to be in range 2 to 12 (Both inclusive).

* So declaring two integer variables named upMax=12 and upMin=2.

* count=0 is used to keep track of how many unique integers

* have been added into ArrayList up.

*/

int upMax=12,upMin=2,count=0;

while(count!=8){

/*

* This while loop exits when count==8

* randomNum stores random number generated ineach iteration

* of loop.

* Formula used gives us number in range 2 to 12.

*/

int randomNum = rand.nextInt((upMax - upMin) + 1) + upMin;

/*

* Now to have only unique random numbers we need to check

* whether randomNum is already in up ArrayList.

* If it is not in ArrayList up then only if condition is true.

*/

if(!up.contains(randomNum)){

/*

* I.e if randomNum is not in up ArrayList then we add

* it into up.

* And increment count.

*/

up.add(randomNum);

count++;

}

}//while loop ends

/*

* Sorting the ArrayList up using built in function

*/

Collections.sort(up);

System.out.println("=============================================================");

System.out.println(" ArrayList up generated in Ascending order ");

System.out.println(" "+up);

  

  

/*

* Following is work on down operations where 5 random integers

* are generated.

* Here random number has to be in range 11 to 1 (Both inclusive).

* So declaring two integer variables named downMax=11 and downMin=1.

* count=0 is used to keep track of how many unique integers

* have been added into ArrayList down.

*/

  

//Updating count variable to 0

count=0;

int downMax=11,downMin=1;

while(count!=5){

/*

* This while loop exits when count==5

* randomNum stores random number generated ineach iteration

* of loop.

* Formula used gives us number in range 2 to 12.

*/

int randomNum = rand.nextInt((downMax - downMin) + 1) + downMin;

/*

* Now to have only unique random numbers we need to check

* whether randomNum is already in down ArrayList.

* If it is not in ArrayList down then only if condition is true.

*/

if(!down.contains(randomNum)){

/*

* I.e if randomNum is not in up ArrayList then we add

* it into up.

* And increment count.

*/

down.add(randomNum);

count++;

}

}//while loop ends

/*

* Sorting the ArrayList down in descending order using built in function

*/

Collections.sort(down, Collections.reverseOrder());

System.out.println(" ArrayList down generated in Descending order ");

System.out.println(" "+down);

System.out.println("=============================================================");

  

System.out.println("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");

System.out.println(" =============================================================");

System.out.println(" Elevator Going Up");

System.out.println("=============================================================");

  

/*

* An integer variable named startFloor is declared and initialized to 1

* As elevator is at floor 1 at start.

*/

int startFloor=1;

while(true){

/*

* This while loop runs until up ArrayList is not empty.

*/

if(up.isEmpty()){

/*

* If arraylist up is empty then while loop terminates.

*/

break;

}

/*

* Integer variable value stores value at index 0 of arraylist up.

* Then we print statements in required format as follows.

*/

int value=up.get(0);

System.out.println("Staring at floor "+startFloor);

System.out.println(" Going up: now at floor "+value);

System.out.println("Stopping at floor "+value+" for 3 seconds -> 1,2,3");

System.out.println("");

/*

* now startFloor is updated to value.

*/

startFloor=value;

/*

* Now value at index 0 is removed from ArrayList up.

*/

up.remove(0);

}

  

System.out.println(" =============================================================");

System.out.println(" Elevator Going Down");

System.out.println("=============================================================");

  

/*

* Elevator is at floor 12 when it is coming down.

* startFloor is updated to value 12.

*/

startFloor=12;

while(true){

/*

* This loop exactly similar to up while loop

*/

if(down.isEmpty()){

break;

}

int value=down.get(0);

System.out.println("Staring at floor "+startFloor);

System.out.println(" Going down: now at floor "+value);

System.out.println("Stopping at floor "+value+" for 3 seconds -> 1,2,3");

System.out.println("");

startFloor=value;

down.remove(0);

}

System.out.println("Do you want to run the elevator again?");

Scanner sc=new Scanner(System.in);

char choice=sc.nextLine().charAt(0);

if(choice=='N'||choice=='n'){

break;

}

  

}//while loop of continue or exit ends here

}//main ends here

}//class ends here

Output::


C:CheggJava>javac elevator.java

C:CheggJava>java elevator
=============================================================
ArrayList up generated in Ascending order
[3, 4, 6, 7, 8, 9, 11, 12]
ArrayList down generated in Descending order
[11, 6, 4, 2, 1]
=============================================================
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

=============================================================
Elevator Going Up
=============================================================
Staring at floor 1
Going up: now at floor 3
Stopping at floor 3 for 3 seconds -> 1,2,3

Staring at floor 3
Going up: now at floor 4
Stopping at floor 4 for 3 seconds -> 1,2,3

Staring at floor 4
Going up: now at floor 6
Stopping at floor 6 for 3 seconds -> 1,2,3

Staring at floor 6
Going up: now at floor 7
Stopping at floor 7 for 3 seconds -> 1,2,3

Staring at floor 7
Going up: now at floor 8
Stopping at floor 8 for 3 seconds -> 1,2,3

Staring at floor 8
Going up: now at floor 9
Stopping at floor 9 for 3 seconds -> 1,2,3

Staring at floor 9
Going up: now at floor 11
Stopping at floor 11 for 3 seconds -> 1,2,3

Staring at floor 11
Going up: now at floor 12
Stopping at floor 12 for 3 seconds -> 1,2,3


=============================================================
Elevator Going Down
=============================================================
Staring at floor 12
Going down: now at floor 11
Stopping at floor 11 for 3 seconds -> 1,2,3

Staring at floor 11
Going down: now at floor 6
Stopping at floor 6 for 3 seconds -> 1,2,3

Staring at floor 6
Going down: now at floor 4
Stopping at floor 4 for 3 seconds -> 1,2,3

Staring at floor 4
Going down: now at floor 2
Stopping at floor 2 for 3 seconds -> 1,2,3

Staring at floor 2
Going down: now at floor 1
Stopping at floor 1 for 3 seconds -> 1,2,3

Do you want to run the elevator again?
y
=============================================================
ArrayList up generated in Ascending order
[3, 4, 5, 6, 7, 8, 10, 11]
ArrayList down generated in Descending order
[10, 6, 5, 4, 1]
=============================================================
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

=============================================================
Elevator Going Up
=============================================================
Staring at floor 1
Going up: now at floor 3
Stopping at floor 3 for 3 seconds -> 1,2,3

Staring at floor 3
Going up: now at floor 4
Stopping at floor 4 for 3 seconds -> 1,2,3

Staring at floor 4
Going up: now at floor 5
Stopping at floor 5 for 3 seconds -> 1,2,3

Staring at floor 5
Going up: now at floor 6
Stopping at floor 6 for 3 seconds -> 1,2,3

Staring at floor 6
Going up: now at floor 7
Stopping at floor 7 for 3 seconds -> 1,2,3

Staring at floor 7
Going up: now at floor 8
Stopping at floor 8 for 3 seconds -> 1,2,3

Staring at floor 8
Going up: now at floor 10
Stopping at floor 10 for 3 seconds -> 1,2,3

Staring at floor 10
Going up: now at floor 11
Stopping at floor 11 for 3 seconds -> 1,2,3


=============================================================
Elevator Going Down
=============================================================
Staring at floor 12
Going down: now at floor 10
Stopping at floor 10 for 3 seconds -> 1,2,3

Staring at floor 10
Going down: now at floor 6
Stopping at floor 6 for 3 seconds -> 1,2,3

Staring at floor 6
Going down: now at floor 5
Stopping at floor 5 for 3 seconds -> 1,2,3

Staring at floor 5
Going down: now at floor 4
Stopping at floor 4 for 3 seconds -> 1,2,3

Staring at floor 4
Going down: now at floor 1
Stopping at floor 1 for 3 seconds -> 1,2,3

Do you want to run the elevator again?
n

C:CheggJava>

Thank you!!

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote