1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
amm1812
4 years ago
15

1. Define class Elevator. For simplicity, we do not consider property like capacity (or weight limit), door (open or close), or

status (running, maintenance, alarm). (a) Data members of an elevator can be as follows, they are all ints. currentFloor, baseFloor, and topFloor; (b) Define constructors for Elevator class.A default constructor setting baseFloor to be 1, and topFloor to be 6, and currentFloor to be1. Define an non-default constructor public Elevator(int currentFloor, int baseFloor, int topFloor), where topFloor should be at least 2, baseFloor should be smaller than topFloor, and currentFloor is somewhere between baseFloor and topFloor. (c) Define method up with a given parameter numFloors (an int). If numFloors is positive, then move that many floors up from the currentFloor, update value for currentFloor. Note that after you run this method, currentFloor cannot be bigger than topFloor.(d) Define method down with a given parameter numFloors (an int). if numFloors is positive, then move that many floors down from the currentFloor, update value for currentFloor. Note that after you run this method, currentFloor cannot be smaller than the baseFloor.(e) Define a getter for each data member. (f) NO need to define other method.2. Define Passenger class. It takes data members: currentFloor and targetFloor. Both are ints.(a) Define constructor of Passengers. This constructor takes two int parameters, use them to initialize the corresponding data member.(b) Define a getter for each data member.(c) NO need to define other methods.3. Define class PassengersTakeElevator. (a) Define data member as an elevator and a list of passengers (read API of ArrayList). The main difference between an array and a list is that a list can grow (add an element to the list) or shrink (remove an element from the list) dynamically; however, to access an individual element, we need to start from the head of the list.(b) Define a default constructor, use an elevator created from a default constructor, and build a list of three passenger. For each passenger, currentFloor and targetFloor are random ints between the baseFloor and topFloor of the elevator. Note that for each passenger, currentFloor cannot be the same as targetFloor (otherwise, there is no need to move the passenger using elevator).(c) Define a non-default constructor that takes an elevator and a list of passengers.(c) Define method schedule. Let the elevator serve passengers in the first-in and first-out order. For simplicity, you can assume that2 there is only one elevator, and the elevator can only carry one passenger a time. For each passenger, we first need to move the elevator to the current floor of that passenger if the elevator is not in that floor yet. Then bring the passenger to his/her target floor.Print out a message every time the elevator movesFor example, Passenger 0 is at Fl 5, will go to Fl 2 elevator is at Fl 1 elevator moves up from Fl 1 to Fl 5 elevator carries passenger down from Fl 5 to Fl 24. Define a client class to test PassengersTakeElevator.A sample output may look like:A. Passenger 0 is at Fl 5, will go to Fl 2 elevator is at Fl 1 elevator moves up from Fl 1 to Fl 5 elevator carries passenger down from Fl 5 to Fl 2B. Passenger 1 is at Fl 3, will go to Fl 2 elevator is at Fl 2 elevator moves up from Fl 2 to Fl 3 elevator carries passenger down from Fl 3 to Fl 2C. Passenger 2 is at Fl 4, will go to Fl 3 elevator is at Fl 2 elevator moves up from Fl 2 to Fl 4 elevator carries passenger down from Fl 4 to Fl 3
Computers and Technology
1 answer:
mihalych1998 [28]4 years ago
7 0

Answer:

The source code is given below using Java

Explanation:

/*

* File: Passenger.java

*/

public class Passenger {

int currentFloor,targetFloor;//data members

//argumented constructor

public Passenger(int currentFloor, int targetFloor) {

this.currentFloor = currentFloor;

this.targetFloor = targetFloor;

}

//getter method

public int getCurrentFloor() {

return currentFloor;

}

//getter method

public int getTargetFloor() {

return targetFloor;

}

 

}

/***********************/

/*

File: Elevator.java

*/

public class Elevator {

int currentFloor, baseFloor, topFloor;//data members

public Elevator()//default constructor

{

baseFloor=1;

topFloor=6;

currentFloor=1;

}

//argumented constructor

public Elevator(int currentFloor, int baseFloor, int topFloor)

{ if(topFloor>=2 && baseFloor < topFloor && currentFloor>baseFloor && currentFloor < topFloor)

{

this.baseFloor=baseFloor;

this.topFloor=topFloor;

this.currentFloor=currentFloor;

}

else

{

baseFloor=1;

topFloor=6;

currentFloor=1;

}

}

// up method

public void up(int numFloors )

{

if(numFloors>0 && currentFloor+numFloors <=topFloor)

currentFloor+=numFloors;    

}

// down method

public void down(int numFloors )

{

if(numFloors>0 && currentFloor-numFloors >=baseFloor)

currentFloor-=numFloors;

}

//getter method

public int getCurrentFloor() {

return currentFloor;

}

//getter method

public int getBaseFloor() {

return baseFloor;

}

//getter method

public int getTopFloor() {

return topFloor;

}

 

}

/****************************/

/*

* File: PassengersTakeElevator.java

*/

import java.util.ArrayList;

public class PassengersTakeElevator {

//data members

Elevator elevator;

ArrayList <Passenger> passengers;

//default constructor

public PassengersTakeElevator() {

passengers=new ArrayList<>();

elevator=new Elevator();

Passenger p0=new Passenger(5, 2);

passengers.add(p0);

Passenger p1=new Passenger(3, 2);

passengers.add(p1);

Passenger p2=new Passenger(4, 3);

passengers.add(p2);

}

//argumented constructor

public PassengersTakeElevator(Elevator elevator, ArrayList<Passenger> passengers) {

this.elevator = elevator;

this.passengers = passengers;

}

//schedule method

public void schedule(){

for(int i=0;i<3;i++)

{

Passenger p=passengers.get(0);//gets passenger at head

 

System.out.println("Passenger "+i+" is at Fl "+p.getCurrentFloor()+", will go to Fl "+p.getTargetFloor());

System.out.println("Elevator is at Fl "+elevator.getCurrentFloor());

int numFloors=p.getCurrentFloor()-elevator.getCurrentFloor();

if(numFloors>0)

{

System.out.println("Elevator moves up from Fl "+elevator.getCurrentFloor()+" to Fl "+p.getCurrentFloor());

elevator.up(numFloors);

System.out.println("Elevator carries passenger down from Fl "+p.getCurrentFloor()+" to Fl "+p.getTargetFloor());

numFloors=p.getCurrentFloor()-p.getTargetFloor();

elevator.down(numFloors);

}

else if(numFloors<0)

{

System.out.println("Elevator moves down from Fl "+elevator.getCurrentFloor()+" to Fl "+p.getCurrentFloor());

elevator.down(-numFloors);

System.out.println("Elevator carries passenger up from Fl "+p.getCurrentFloor()+" to Fl "+p.getTargetFloor());

numFloors=p.getTargetFloor()-p.getCurrentFloor();

elevator.up(numFloors);

 

}

else

{ numFloors=p.getTargetFloor()-p.getCurrentFloor();

if(numFloors>0)

{

System.out.println("Elevator carries passenger up from Fl "+p.getCurrentFloor()+" to Fl "+p.getTargetFloor());

elevator.up(numFloors);

}

else

{

System.out.println("Elevator carries passenger down from Fl "+p.getCurrentFloor()+" to Fl "+p.getTargetFloor());

elevator.down(-numFloors);

}

}

passengers.remove(0);//removes passenger at head

}

}

}

/*****************************/

/*

* File: TestPassengersTakeElevator.java

*/

import java.util.ArrayList;

public class TestPassengersTakeElevator {

public static void main(String[] args) {

//object of PassengersTakeElevator

System.out.println("Schedule 1");

PassengersTakeElevator obj1=new PassengersTakeElevator();

obj1.schedule();//calling schedule method of PassengersTakeElevator

ArrayList <Passenger> passengers=new ArrayList<>();

Elevator elevator=new Elevator();

Passenger p0=new Passenger(1, 5);

passengers.add(p0);

Passenger p1=new Passenger(2, 4);

passengers.add(p1);

Passenger p2=new Passenger(4, 1);

passengers.add(p2);

System.out.println();

System.out.println("Schedule 2");

PassengersTakeElevator obj2=new PassengersTakeElevator(elevator,passengers);

obj2.schedule();//calling schedule method of PassengersTakeElevator

 

 

}

 

}

You might be interested in
Describe four features of a well designed input screen
Sladkaya [172]

Answer:

A well design input screen should:1.Be titled2.Navigation aids should be used

Explanation:

3 0
3 years ago
Read 2 more answers
For every $1 of deposits, the banks can increase ________________ by the value of the Money Multiplier.
Snowcat [4.5K]
<span>A. Aggregate Price Level</span>
8 0
3 years ago
When you receive five job offers what does it mean
7nadin3 [17]

that you are a great canidate, in high demand, and well trained!

6 0
3 years ago
write a function named list_total that accepts a list as an argument (assume the list contains integers) and returns the cumulat
lutik1710 [3]

Answer:

def listSum(mylist):

  sum =0

  for item in range(0,len(mylist)):

      sum=sum+mylist[item]

  print(sum)

Explanation:

Using the Python programming language, I defined a function as listSum that accepts a list as a parameter and returns the sum of the elements in the list.

The most important logic here is defining the range of elements in the list not to exceed the length of the list using the len function. Then using a for statement, we loop through each element and add them up to a sum variable initially assigned the value of 0.

3 0
3 years ago
10. Calculate the checksum for blocks of data with the following byte sum. (a) 1220 (b) 950​
OLga [1]

Answer:kalo gk salah yang

a

Explanation:

5 0
3 years ago
Other questions:
  • Most GUIs provide all of the following except _____.
    7·1 answer
  • Database management systems _____. a. include transaction-processing reports for database analysis b. are used to create, organi
    6·1 answer
  • The c++ operator _______________ is used to destroy dynamic variables.
    5·1 answer
  • Every chart has a corresponding ____ that contains the numerical data displayed by the chart.
    6·1 answer
  • For a wire with a circular cross section and a diameter = 3.00mm calculate the following: (m means meter. mm means millimeter. c
    5·1 answer
  • &amp;. Give three differences between a<br>web browser<br>and web page​
    11·1 answer
  • James would like to send a document he has saved on the hard drive to coworkers in Ireland, Brazil, and India. These coworkers h
    6·2 answers
  • Give a regular expression for binary numbers. They can be integers or binary fractions. A leading - sign is always allowed. Lead
    15·1 answer
  • What can help you estimate how much money you might get in
    7·1 answer
  • If you click on repeat header rows what will happen?
    9·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!