Skip to content

Activity 5: Classes and UML

Instructions

Example Code

Model 1

Die.java
 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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
/**
 * Simulates a die object.
 */
public class Die {

    private int face;

    /**
     * Constructs a die with face value 1.
     */
    public Die() {
        this.face = 1;
    }

    /**
     * @return current face value of the die
     */
    public int getFace() {
        return this.face;
    }

    /**
     * Simulates rolling the die.
     * 
     * @return new face value of the die
     */
    public int roll() {
        this.face = (int) (Math.random() * 6) + 1;
        return this.face;
    }

    /**
     * Example application that plays dice.
     * 
     * @param args command-line arguments
     */
    public static void main(String[] args) {
        Die d1 = new Die();
        Die d2 = new Die();
        System.out.println("Initial dice");
        System.out.println("d1 = " + d1.getFace() + ", d2 = " + d2.getFace());

        System.out.println();
        int sum = d1.roll() + d2.roll();
        System.out.println("You rolled " + sum);
        System.out.println("d1 = " + d1.getFace() + ", d2 = " + d2.getFace());
    }

}

Model 2

Circle.java solution (don't look until after the activity)
 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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
/**
 * Geometric shape defined by a radius value.
 */
public class Circle {

    /** The radius of the circle. */
    private double radius;

    /**
     * Constructs a circle with the given radius.
     * 
     * @param radius initial radius value
     */
    public Circle(double radius) {
        setRadius(radius);
    }

    /**
     * Gets the radius value (accessor method).
     * 
     * @return current radius value
     */
    public double getRadius() {
        return this.radius;
    }

    /**
     * Sets the radius value (mutator method).
     * 
     * @param radius new radius value
     */
    public void setRadius(double radius) {
        if (radius >= 0) {
            this.radius = radius;
        } else {
            this.radius = 0;
        }
    }

    /**
     * Calculates the area.
     * 
     * @return area of the circle
     */
    public double area() {
        return Math.PI * this.radius * this.radius;
    }

    /**
     * Calculates the circumference.
     * 
     * @return circumference of the circle
     */
    public double circumference() {
        return 2.0 * Math.PI * this.radius;
    }

    /**
     * Example application that declares, instantiates, and prints circles.
     * 
     * @param args command-line arguments
     */
    public static void main(String[] args) {
        Circle one = new Circle(1);
        System.out.println("one area = " + one.area());
        System.out.println("one circ = " + one.circumference());

        Circle two = new Circle(2);
        System.out.println("two area = " + two.area());
        System.out.println("two circ = " + two.circumference());
    }

}

Model 3

SwapCircle.java
 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
35
36
37
38
39
40
41
42
43
44
45
46
public class SwapCircle {

    private static int circleCount = 0;

    private double radius;

    public SwapCircle(double radius) {
        circleCount++;
        if (radius > 0) {
            this.radius = radius;
        } else {
            this.radius = 1;
        }
    }

    public static int getCircleCount() {
        return circleCount;
    }

    public double getRadius() {
        return this.radius;
    }

    public static void swapInts(int x, int y) {
        System.out.println("\tInside swapInts");
        System.out.println("\tswapping integers " + x + " and " + y);
        int temp = x;
        x = y;
        y = temp;
        System.out.println("\tfinished swapping " + x + " and " + y);
    }

    public static void swapCircles(SwapCircle c1, SwapCircle c2) {
        System.out.println("\tInside swapCircles");
        System.out.println("\tswapping circles " + c1 + " and " + c2);
        double r = c1.radius;
        c1.radius = c2.radius;
        c2.radius = r;
        System.out.println("\tfinished swapping " + c1 + " and " + c2);
    }

    public String toString() {
        return String.format("Circle(%.0f)", this.radius);
    }

}
SwapDriver.java
 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
public class SwapDriver {

    public static void main(String[] args) {

        // first try swapping integers
        int a = 7, b = 4;
        System.out.println("BEFORE swap:");
        System.out.println("a = " + a);
        System.out.println("b = " + b);
        SwapCircle.swapInts(a, b);
        System.out.println("AFTER swap:");
        System.out.println("a = " + a);
        System.out.println("b = " + b);
        System.out.println();

        // next try swapping SwapCircle radii
        SwapCircle first, second;
        first = new SwapCircle(7);
        second = new SwapCircle(4);
        System.out.println("BEFORE swap:");
        System.out.println("first = " + first);
        System.out.println("second = " + second);
        SwapCircle.swapCircles(first, second);
        System.out.println("AFTER swap:");
        System.out.println("first = " + first);
        System.out.println("second = " + second);
        System.out.println();

        System.out.printf("This program created %d circles",
                SwapCircle.getCircleCount());
        System.out.println();
    }

}