University of Washington, AP/CS A

Lab 5: arrays

Except where otherwise noted, the contents of this document are Copyright 2013 Stuart Reges and Marty Stepp.

lab document created by Marty Stepp, Stuart Reges and Whitaker Brand

Today's lab

Goals for this lab:

Declaring a class (syntax)

public class ClassName {
    // fields
    fieldType fieldName;

    // methods
    public returnType methodName() {
        statements;
    }
}
A couple things look different than programs for past homeworks:

Exercise : Client code method call syntax practice-it

Suppose you have the following BankAccount class (on the left) and client code (on the right):

public class BankAccount {
   public BankAccount() {...}
   public double computeInterest(int rate) {...}
}
public class BankAccountClient {
   public static void main(String[] args) {
      BankAccount acct = new BankAccount();
      // your code goes here
   }
}
What would be a valid call to computeInterest given the following client code, and a desired rate of 42?

Exercise : Point class errors

Can you find all the errors in the following code?
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
public class Point {
    int x;                                       // Each Point object has
    int y;                                       // an int x and y inside.

    public void Point(int initX, int initY) {    // Constructor
        initX = x;
        initY = y;
    }

    public static double distanceFromOrigin() {  // Returns this point's
        int x;                                   // distance from (0, 0).
        int y;
        double dist = Math.sqrt(x*x + y*y);
        return dist;
    }

    public void translate(int dx, int dy) {      // Shifts this point's x/y
        int x = x + dx;                          // by the given amounts.
        int y = y + dy;
    }
}

Exercise - solution

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
public class Point {
    private int x;                               // Each Point object has
    private int y;                               // an int x and y inside.
    // a constructor does not have a return type
    public void Point(int initX, int initY) {    // Constructor
        // we want to be changing the values of the fields!
        x = initX;
        y = initY;
    }
   
    public static double distanceFromOrigin() {  // Returns this point's
        int x;                                   // distance from (0, 0).
        int y;
        double dist = Math.sqrt(x*x + y*y);
        return dist;
    }

    public void translate(int dx, int dy) {
	// we want to change the values of fields, not create new local variables
        int x = x + dx;
        int y = y + dy;
    }
}

Exercise : Printing objects practice-it

Point p1 = new Point();
...
System.out.println(p1);

The above println statement (the entire line) is equivalent to what?

Exercise : quadrant practice-it

Add the following method to the Point class:

public int quadrant()

Returns which quadrant of the x/y plane this Point object falls in. Quadrant 1 contains all points whose x and y values are both positive. Quadrant 2 contains all points with negative x but positive y. Quadrant 3 contains all points with negative x and y values. Quadrant 4 contains all points with positive x but negative y. If the point lies directly on the x and/or y axis, return 0.

(Test your code in Practice-It! or by running the PointMain program.)

Exercise : flip practice-it

Add the following method to the Point class:

public void flip()

Negates and swaps the x/y coordinates of the Point object. For example, if an object pt initially represents the point (5, -3), after a call of pt.flip(); , the object should represent (3, -5). If the same object initially represents the point (4, 17), after a call to pt.flip();, the object should represent (-17, -4).

Test your code in Practice-It! or by running the PointMain program.

Exercise : Point toString practice-it

Modify the toString method in the Point class. Make it return a string in the following format. For example, if a Point object stored in a variable pt represents the point (5, -17), return the string:

Point[x=5,y=-17]

If the client code were to call System.out.println(pt); , that text would be shown on the console. Note that this format is slightly different in the Practice-it problem.

(Test your code in Practice-It, or by running your PointClient or PointMain and printing a Point there.)

Exercise : manhattanDistance practice-it

Add the following method to the Point class:

public int manhattanDistance(Point other)

Returns the "Manhattan distance" between the current Point object and the given other Point object. The Manhattan distance refers to how far apart two places are if the person can only travel straight horizontally or vertically, as though driving on the streets of Manhattan. In our case, the Manhattan distance is the sum of the absolute values of the differences in their coordinates; in other words, the difference in x plus the difference in y between the points.

Click on the check-mark above to try out your solution in Practice-it! (Write just the new method, not the entire Point class.)

Exercise : TimeSpan practice-it

Define a class named TimeSpan. A TimeSpan object stores a span of time in hours and minutes (for example, the time span between 8:00am and 10:30am is 2 hours, 30 minutes). The minutes should always be reported as being in the range of 0 to 59. That means that you may have to "carry" 60 minutes into a full hour.

Exercise : Circle practice-it

Define a class named Circle. A Circle object stores a center point and a radius.

See the Practice-It link above for a full description of the class and the methods/constructors it should have. You can also test your class in Practice-It.