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
What is the output of the AWK program?
zhannawk [14.2K]
Luckily your file uses YYYY-MM-SS HH:MM:SS timestamps, which sort alphabetically. Those can be compared with < > <= etc without worrying about any date math. MIN="..." and MAX="..." are where those values are input into awk.
4 0
3 years ago
Let f and g be two one-argument functions. The composition f after g is defined to be the function x 7→ f (g (x)). Define a proc
Ghella [55]

Answer and Explanation:

(define (compose f g)

(lambda (x) (f (g x))))

6 0
3 years ago
. Write the advantages and disadvantages of CLI?
Sindrei [870]

\huge\mathcal\colorbox{blue}{{\color{red}{AnSwEr~↓~↓~}}}

\huge\mathbb\colorbox{black}{\color{white}{Advantages=}}

  • If you know the commands, a CLI can be a lot faster and efficient than any other type of interface. ...

  • A CLI requires less memory to use in comparison to other interfaces. ...

  • A CLI doesn't require Windows and a low-resolution monitor can be used.

\huge\mathbb\colorbox{black}{\color{white}{Disadvantages}}

  • Commands have to be typed precisely. If there is a spelling error the command will fail

\huge\mathbb\colorbox{Blue}{\color{red}{Hope \: It\: Help \: U}}

7 0
3 years ago
Read 2 more answers
What are some areas to fill out in the New Formatting Rule dialog box? Check all that apply. rule type function name condition c
spayn [35]

Answer:

The answer to this question is given below in the explanation section.

Explanation:

In this question the given options are:

  1. rule type
  2. function name
  3. condition
  4. cell reference
  5. argument
  6. format

<u>The correct options to this question are:</u>

1. rule type  

3. condition

6.  format

These are the areas that can be used to fill out in the New Formatting Rule dialog box. Other options are not correct becuase these are not used to fill out new formatting rule dialog box.

6 0
3 years ago
Read 2 more answers
¿Cuánta energía consumirá una lavadora de 1200W de potencia, si se deja conectada durante 20 horas? ¿Cuánto deberemos pagar si e
9966 [12]

Answer:

The amount to be paid is €2.88 for the amount of power consumed

Explanation:

The first thing to do here is to convert the power consumption to kilo-watt

1 kilowatt = 1000 watt

x kilowatt = 1200 watt

x = 1200/1000 = 1.2 KW

we now convert this to kilowatt hour by multiplying the number of hours by the number of kilowatt.

That would be 20 * 1.2 = 24 KWh

Now, the charge is 0.12€/kWh

for 24 kWh, we have 24 * 0.12 = €2.88

6 0
3 years ago
Other questions:
  • Stock ____ firms trade financial securities between a buyer and a seller.
    11·1 answer
  • which one of these steps describe saving a newly created file. click on the save icon. minimize the file. name the file. select
    12·2 answers
  • Write a SELECT statement that returns the same result set as this SELECT statement, but don’t use a join. Instead, use a subquer
    12·1 answer
  • Count characters Write a program whose input is a string which contains a character and a phrase, and whose output indicates the
    6·1 answer
  • You want to deploy a software package that's available to all users in the domain if they want to use it, but you don't want the
    7·1 answer
  • In your own words, what is pair-programming? What is the role of the driver? What is the role of the navigator? What are some be
    15·1 answer
  • PLS HELP
    5·1 answer
  • I need them on this question
    5·1 answer
  • What type of malicious software tries to gather information about you without your consent?
    7·1 answer
  • Which of the following media forms is the Federal Communications Commission able to regulate content on?
    12·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!