I am trying to write a program that moves an elevator randomly through the floors of different buildings with different stories. I need to use the finalize() method to clean up after every building. And I have different floors choosing to activate object = null, System.gc().
public class ElevatorTwo {
static double elev;
double floor = 0;
String next;
ElevatorTwo(double n) {
floor = n;
}
ElevatorTwo() {
next = "The next building has ";
}
public void finalize() {
System.out.println("Elevator Ending, elevator returned to the first floor.");
System.out.println();
}
public static void buildingOne() {
ElevatorTwo buildOne = new ElevatorTwo(10);
System.out.println("This building has 10 stories.");
try {
for (int i = 0; i < buildOne.floor; i++) {
elev = (int) (Math.random() * buildOne.floor) + 1;
// if (elev == i)
// elev = i + elev;
try {
Thread.sleep(500);
} catch (InterruptedException e) {
System.out.println(e);
}
System.out.printf("Floor %2.0f%n", elev);
if (elev == 2 || elev == 4 || elev == 8) {
buildOne = null;
{
System.gc();
}
}
}
} catch (NullPointerException e) {
}
}
public static void buildingTwo() {
ElevatorTwo buildTwo = new ElevatorTwo(15);
ElevatorTwo myBuildTwo = new ElevatorTwo();
System.out.println(myBuildTwo.next + (int) buildTwo.floor + " stories.");
try {
for (int i = 0; i < buildTwo.floor; i++) {
elev = (int) (Math.random() * buildTwo.floor) + 1;
// if (elev == i)
// elev = i + elev;
try {
Thread.sleep(500);
} catch (InterruptedException e) {
System.out.println(e);
}
System.out.printf("Floor %2.0f%n", elev);
if (elev == 2 || elev == 4 || elev == 8) {
buildTwo = null;
{
System.gc();
}
}
}
} catch (NullPointerException nul) {
}
}
public static void buildingThree() {
ElevatorTwo buildThree = new ElevatorTwo(30);
ElevatorTwo myBuildThree = new ElevatorTwo();
System.out.println(myBuildThree.next + (int) buildThree.floor + " stories.");
try {
for (int i = 0; i < buildThree.floor; i++) {
elev = (int) (Math.random() * buildThree.floor) + 1;
// if (elev == i)
// elev = i + elev;
try {
Thread.sleep(500);
} catch (InterruptedException e) {
System.out.println(e);
}
System.out.printf("Floor %2.0f%n", elev);
if (elev == 8 || elev == 13 || elev == 17 || elev == 20 || elev == 25 || elev == 28) {
buildThree = null;
{
System.gc();
}
}
}
} catch (NullPointerException e) {
}
}
public static void main(String[] args) {
System.out.println();
buildingOne();
buildingTwo();
buildingThree();
}
}
This is a sample of the result.
This building has 10 stories.
Floor 7
Floor 3
Floor 3
Floor 5
Floor 4
Elevator Ending, elevator returned to the first floor.
The next building has 15 stories.
Floor 14
Floor 5
Floor 3
Floor 11
Floor 8
Elevator Ending, elevator returned to the first floor.
The next building has 30 stories.
Floor 12
Floor 17
Elevator Ending, elevator returned to the first floor.
Elevator Ending, elevator returned to the first floor.
My question is why the second line of "Elevator Ending, elevator returned to the first floor." is being printed.
If I only call:
buildingOne();
buildingTwo();
There is no repeat of that line.