Question

In: Computer Science

java code: Problem 1: Create a class called Elevator that can be moved between floors in...

java code:

Problem 1:

Create a class called Elevator that can be moved between floors in an N-storey building. Elevator uses a constructor to initialize the number of floors (N) in the building when the object is instantiated.

Elevator also has a default constructor that creates a five storey building.

The Elevator class has a termination condition that requires the elevator to be moved to the main (i.e., first) floor when the object is cleaned up. Write a finalize() method that satisfies this termination condition and verifies the condition by printing a message to the output, Elevator ending: elevator returned to the first floor.

In main(), test at least five (5) possible scenarios that can occur when Elevator is used in a building with many floors (e.g., create, move from one floor to another, etc.).

Tip: Termination occurs when the instance is set to null. You may wish to investigate “garbage collection” for this exercise.

Solutions

Expert Solution


public class Elevator extends Thread{

   int floor;
  
   public Elevator() {
       // TODO Auto-generated constructor stub
       floor = 5;
   }
  
  
   public Elevator(int floor) {
       super();
       this.floor = floor;
   }

   public void move() {
       try {
           for(int i=1;i<=floor;i++) {
               System.out.println("Elevator is at floor "+i);
               sleep(1000);
           }
       }catch (Exception e) {
           // TODO: handle exception
       }
   }
  
   public void move(int a,int b) {
       System.out.println("Elevator is at floor "+a+" and moving to floor "+b);
   }
  
   @Override
   protected void finalize() throws Throwable {
       // TODO Auto-generated method stub

       System.out.println("Elevator ending: elevator returned to the first floor.");
   }
  
   public static void main(String[] args) throws Throwable {
       // TODO Auto-generated method stub
       Elevator e = new Elevator();
       e.move();
       e.move(5, 3);
       e.finalize();
      
       Elevator e1 = new Elevator(10);
       e1.move();
       e1.move(10, 4);
       e1.finalize();
   }

}


Elevator is at floor 1 Elevator is at floor 2 Elevator is at floor 3 Elevator is at floor 4 Elevator is at floor 5 Elevator is at floor 5 and moving to floor 3 Elevator ending: elevator returned to the first floor. Elevator is at floor 1 Elevator is at floor 2 Elevator is at floor 3 Elevator is at floor 4 Elevator is at floor 5 Elevator is at floor 6 Elevator is at floor 7 Elevator is at floor 8 Elevator is at floor 9 Elevator is at floor 10 Elevator is at floor 10 and moving to floor 4 Elevator ending: elevator returned to the first floor.


Related Solutions

Problem 1 Create a new project called Lab7 Create a class in that project called ListORama...
Problem 1 Create a new project called Lab7 Create a class in that project called ListORama Write a static method in that class called makeLists that takes no parameters and returns no value In makeLists, create an ArrayList object called avengers that can hold String objects. Problem 2 Add the following names to the avengers list, one at a time: Chris, Robert, Scarlett, Clark, Jeremy, Gwyneth, Mark Print the avengers object. You will notice that the contents are displayed in...
1. Create a new Java project called L2 and a class named L2 2. Create a...
1. Create a new Java project called L2 and a class named L2 2. Create a second class called ArrayExaminer. 3. In the ArrayExaminer class declare the following instance variables: a. String named textFileName b. Array of 20 integers named numArray (Only do the 1st half of the declaration here: int [] numArray; ) c. Integer variable named largest d. Integer value named largestIndex 4. Add the following methods to this class: a. A constructor with one String parameter that...
Create a Java project called Lab3B and a class named Lab3B. Create a second new class...
Create a Java project called Lab3B and a class named Lab3B. Create a second new class named Book. In the Book class: Add the following private instance variables: title (String) author (String) rating (int) Add a constructor that receives 3 parameters (one for each instance variable) and sets each instance variable equal to the corresponding variable. Add a second constructor that receives only 2 String parameters, inTitle and inAuthor. This constructor should only assign input parameter values to title and...
Create a Java project called Lab3A and a class named Lab3A. Create a second new class...
Create a Java project called Lab3A and a class named Lab3A. Create a second new class named Employee. In the Employee class: Add the following private instance variables: name (String) job (String) salary (double) Add a constructor that receives 3 parameters (one for each instance variable) and sets each instance variable equal to the corresponding variable. (Refer to the Tutorial3 program constructor if needed to remember how to do this.) Add a public String method named getName (no parameter) that...
Create a Java project called 5 and a class named 5 Create a second new class...
Create a Java project called 5 and a class named 5 Create a second new class named CoinFlipper Add 2 int instance variables named headsCount and tailsCount Add a constructor with no parameters that sets both instance variables to 0; Add a public int method named flipCoin (no parameters). It should generate a random number between 0 & 1 and return that number. (Important note: put the Random randomNumbers = new Random(); statement before all the methods, just under the...
In Java, using the code provided for Class Candle, create a child class that meets the...
In Java, using the code provided for Class Candle, create a child class that meets the following requirements. Also compile and run and show output ------------------------------------------------------------------------ 1. The child class will be named  ScentedCandle 2. The data field for the ScentedCandle class is:    scent 3. It will also have getter and setter methods 4. You will override the parent's setHeight( ) method to set the price of a ScentedCandle object at $3 per inch (Hint:   price = height * PER_INCH) CODE...
JAVA Programming ECLIPSE IDE 1. Create an abstract class called Book. The class should declare the...
JAVA Programming ECLIPSE IDE 1. Create an abstract class called Book. The class should declare the following variables: an instance variable that describes the title - String an instance variable that describes the ISBN - String an instance variable that describes the publisher - String an instance variable that describes the price - double an instance variable that describes the year – integer Provide a toString() method that returns the information stored in the above variables. Create the getter and...
JAVA Programming ECLIPSE IDE 1. Create an abstract class called Book. The class should declare the...
JAVA Programming ECLIPSE IDE 1. Create an abstract class called Book. The class should declare the following variables: an instance variable that describes the title - String an instance variable that describes the ISBN - String an instance variable that describes the publisher - String an instance variable that describes the price - double an instance variable that describes the year – integer Provide a toString() method that returns the information stored in the above variables. Create the getter and...
(JAVA) 1.) Create a class called Rabbit that with 2 attributes: 1) speed and 2) color....
(JAVA) 1.) Create a class called Rabbit that with 2 attributes: 1) speed and 2) color. Then, create a constructor that has no parameters, setting the default speed to 0 and the color to “white”; this is called a default constructor. Next, create a second constructor that takes in two parameters. The second constructor should assign those parameters to the attributes. Then, in main, create two Rabbit objects. For the first Rabbit object, call the first constructor. For the second...
Consider a class called Building. This class consists of a number of floors (numberOfFloors) for the...
Consider a class called Building. This class consists of a number of floors (numberOfFloors) for the building, a current floor for the elevator (current), a requested floor of a person waiting for the elevator (requestedFloor), and methods for constructing the building object, for moving the elevator one floor up, for moving the elevator one floor down, for requesting the elevator and for starting the elevator going. Assume that requestedFloor will be set to 0 if there are currently no requests...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT