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- (5) 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.). Hint: Termination occurs when the instance is set to null. You may wish to investigate “garbage collection” for this exercise. When solving this problem make sure that the user can interact with the program and choose which floor they want
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- (5) 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.).
Hint: Termination occurs when the instance is set to null. You may wish to investigate “garbage collection” for this exercise.
When solving this problem make sure that the user can interact with the program and choose which floor they want
The Java finalize() approach of item class is a technique that the garbage Collector always calls simply earlier than the deletion/destroying the object that's eligible for garbage collection to carry out smooth-up pastime. easy-up hobby approach last the assets related to that object like Database Connection, network Connection, or we will say resource de-allocation. don't forget, it isn't a reserved keyword. as soon as the finalize() method completes without delay, garbage Collector destroys that item.
Why finalize() approach is used?
finalize() method releases system sources earlier than the garbage collector runs for a particular object. JVM lets in finalize() to be invoked simplest once according to object.
how to override finalize() method?
The finalize method, that's present within the object magnificence, has an empty implementation. In our magnificence, smooth-up activities are there. Then we need to override this approach to define our smooth-up activities.
How Does garbage collection in Java works?
Java garbage series is an automated method. automated garbage collection is the manner of looking at heap reminiscence, identifying which objects are in use and which aren't, and deleting the unused gadgets. An in-use item, or a referenced item, approach that some a part of your software nonetheless keeps a pointer to that item. An unused or unreferenced object is now not referenced through any a part of your application. So the reminiscence utilized by an unreferenced item may be reclaimed. The developer does no longer need to mark gadgets to be deleted explicitly. The garbage series implementation lives in the JVM.
Varieties of activities in Java garbage collection:
two types of garbage collection activity normally happen in Java. those are:
Minor or incremental garbage series: it is stated to have happened when unreachable gadgets inside the younger era heap memory are eliminated.
most important or full garbage series: it's far said to have passed off while the gadgets that survived the minor garbage series and copied into the antique era or permanent technology heap reminiscence are eliminated. whilst as compared to the younger technology, garbage series occurs much less regularly inside the old generation.
Step by step
Solved in 2 steps