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

Write in Java * Create a new client class called Plants.java * Write code in the...
Write in Java * Create a new client class called Plants.java * Write code in the Plants class that solves problems 1,2,3 and 4 * Include the solutions of the different problems in different methods and call them from the main method * Use the BagInterface.java and ArrayBag.java, but do not add any code Problem 1: Create a bag plantsCart, which holds the following spring seedlings(represented by String) Rose, Daisy, Cabbage, Cucumber, Carrot, Cucumber, Daffodil, Daisy, Rose, Iris, Rose, Spinach....
In Java Create a class called "TestZoo" that holds your main method. Write the code in...
In Java Create a class called "TestZoo" that holds your main method. Write the code in main to create a number of instances of the objects. Create a number of animals and assign a cage and a diet to each. Use a string to specify the diet. Create a zoo object and populate it with your animals. Declare the Animal object in zoo as Animal[] animal = new Animal[3] and add the animals into this array. Note that this zoo...
JAVA code Create a new class called BetterDiGraph that implements the EditableDiGraph interface See the interface...
JAVA code Create a new class called BetterDiGraph that implements the EditableDiGraph interface See the interface below for details. EditableDigraph below: import java.util.NoSuchElementException; /** * Implements an editable graph with sparse vertex support. * * */ public interface EditableDiGraph {    /** * Adds an edge between two vertices, v and w. If vertices do not exist, * adds them first. * * @param v source vertex * @param w destination vertex */ void addEdge(int v, int w); /** *...
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...
This Code Is Supposed To Be Performed In JAVA 1.) Create an abstract class DiscountPolicy. It...
This Code Is Supposed To Be Performed In JAVA 1.) Create an abstract class DiscountPolicy. It should have a single abstract method computeDiscount that will return the discount for the purchase of a given number of a single item. The method has two parameters, count and itemCost. Create a driver class that tests this class and provide the UML. 2.) In a separate program, define DiscountPolicy as an interface instead of the abstract class. Create a driver class that tests...
Please Code Using Java Create a class called SoccerPlayer Create 4 private attributes: First Name, Last...
Please Code Using Java Create a class called SoccerPlayer Create 4 private attributes: First Name, Last Name, Games, and Goals Have two constructors Constructor 1 – default constructor; all values to "NONE" or zero Constructor 2 – accepts input of first name, last name, games and goals. Create get and set methods for each of the four attributes Create a method the returns a double that calculates the average goals per game This method checks for zero games played: If...
for java Welcome to a classic homework problem! Create a public class called Last8. You should...
for java Welcome to a classic homework problem! Create a public class called Last8. You should exposed two public methods: add: adds a value, does not return a value last: returns an array containing the last 8 values that were added, in any order. You do not need a constructor, but you can add an empty one if you need. Until 8 values have been added you should return 0s in their place. For example, here's how an instance of...
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT