0

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.

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
  • 2
    Don't do this: `catch (NullPointerException e) { }`. You will never know when you have made a mistake. – tgdavies Nov 19 '22 at 05:22
  • 1
    You create 5 instances of `ElevatorTwo`, so it doesn't seem surprising that 4 of them have finalize called. Why don't you give each instance an id and print that in finalize, so you can see what's happening. – tgdavies Nov 19 '22 at 05:26
  • 1
    Every sentence starting with “*I need to use the finalize() method*” is definitely wrong. What you really need, is to read [why `finalize()` has been deprecated](https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/lang/Object.html#finalize()) or, for example, [Can java finalize an object when it is still in scope?](https://stackoverflow.com/q/24376768/2711488) – Holger Nov 21 '22 at 13:17

0 Answers0