Skip to content

Lab 9: Java Visualizer

Note

This lab is more like an interactive lecture with in-class exercises. Your instructor will review memory diagrams on the board, and you will practice drawing memory diagrams on paper. The Java Visualizer links below can be used to check your work. For best results, do not use Java Visualizer until you have attempted the exercises.

Review

Example

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
package lab09;

import lab03.Car;

public class Example {

    public static void main(String[] args) {
        Car[] cars = new Car[3];
        cars[0] = new Car("Dodge", 1989);
        cars[1] = new Car("Tesla", 2024);
        cars[2] = cars[0];

        for (Car car : cars) {
            car.accelerate();
            System.out.println(car);
        }

        System.out.println();
        race(cars);
    }

    public static void race(Car[] cars) {
        int i;
        int best = 0;
        for (i = 1; i < cars.length; i++) {
            if (cars[i].getSpeed() > cars[best].getSpeed()) {
                best = i;
            }
        }
        System.out.println("Winner at index " + best);
        System.out.println(cars[best]);
    }

}

Output:

A 1989 Dodge that is going 5.0 mph
A 2024 Tesla that is going 5.0 mph
A 1989 Dodge that is going 10.0 mph

Winner at index 0
A 1989 Dodge that is going 10.0 mph

Practice