University of Washington, CSE 142

Lab 9: Inheritance

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

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

Basic lab instructions

Today's lab

Goals for today:

ClockTime class

Suppose you are given a class named ClockTime with the following contents:

// A ClockTime object represents an hour:minute time during
// the day or night, such as 10:45 AM or 6:27 PM.
public class ClockTime {
    private int hour;
    private int minute;
    private String amPm;

    // Constructs a new time for the given hour/minute
    public ClockTime(int h, int m, String ap)
    
    // returns the field values
    public int getHour()
    public int getMinute()
    public String getAmPm()

    // returns a String for this time; for example, "6:27 PM"
    public String toString()

    ...
}

Exercise : ClockTime advance practice-it

Exercise : ClockTime isWorkTime practice-it

Rectangle class

Suppose you are given a class named Rectangle with the following contents:

// A Rectangle stores an (x, y) coordinate of its top/left corner, a width and height.
public class Rectangle {
    private int x;
    private int y;
    private int width;
    private int height;

    // constructs a new Rectangle with the given x,y, width, and height
    public Rectangle(int x, int y, int w, int h)

    // returns the fields' values
    public int getX()
    public int getY()
    public int getWidth()
    public int getHeight()

    // returns a string such as {(5,12), 4x8}
    public String toString()

    ...
}

Exercise : Rectangle union practice-it

Exercise : Rectangle contains practice-it

Inheritance (syntax)

public class ClassName extends SuperClass {
    ...
}
super.methodName(parameters);

Exercise : Car and Truck practice-it

public class Car {
   public void m1() {
      System.out.println("car 1");
   }

   public void m2() {
      System.out.println("car 2");
   }

   public String toString() {
      return "vroom";
   }
}
public class Truck extends Car {
   public void m1() {
      System.out.println("truck 1");
   }
}
Truck mycar = new Truck();
System.out.println(mycar);    // vroom
mycar.m1();                   // truck 1
mycar.m2();                   // car 2

Exercise : Car and Truck revisited practice-it

public class Car {
   public void m1() {
      System.out.println("car 1");
   }

   public void m2() {
      System.out.println("car 2");
   }

   public String toString() {
      return "vroom";
   }
}
public class Truck extends Car {
   public void m1() {
      System.out.println("truck 1");
   }
    
   public void m2() {
      super.m1();
   }
    
   public String toString() {
      return super.toString() + super.toString();
   }
}
Truck mycar = new Truck();
System.out.println(mycar);    // vroomvroom
mycar.m1();                   // truck 1
mycar.m2();                   // car 1

Exercise : MonsterTruck practice-it

MonsterTruck bigfoot = new MonsterTruck();
bigfoot.m1();                  // monster 1
bigfoot.m2();                  // truck 1 / car 1
System.out.println(bigfoot);   // monster vroomvroom

Employee class hierarchy

Exercise : Marketer practice-it

Exercise : Janitor practice-it

Exercise : HarvardLawyer practice-it

Point class

Suppose you are given a class named Point with the following contents:

// A Point represents a 2-D (x, y) position with integer coordinates.
public class Point {
    private int x;
    private int y;
    
    public Point()                          // (0, 0)
    public Point(int x, int y)
    
    public int getX()                       // return field values
    public int getY()
    public double distanceFromOrigin()      // distance from (0, 0)
    public double distance(Point other)     // distance from another point
    public void setLocation(int x, int y)   // set x/y to the given values
    public void translate(int dx, int dy)   // shift x/y by the given amounts
    public String toString()                // string e.g. "(4, -7)"

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 : 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 : 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.)

BankAccount class

Suppose you are given a class named BankAccount with the following contents:

// A BankAccount keeps track of a user's money balance and ID,
// and counts how many transactions (deposits/withdrawals) are made.
public class BankAccount {
    private String id;
    private double balance;
    private int transactions;
    
    // Constructs an object with the given id/balance and 0 transactions.
    public BankAccount(String id, double balance)
    
    public double getBalance()
    public String getID()
    public String getTransactions()
    
    // Adds amount to balance if between 0-500. Also counts as 1 transaction.
    public void deposit(double amount)
    
    // Subtracts amount from balance if user has enough money. Counts as 1 transaction.
    public void withdraw(double amount)
    
    ...
}

Exercise : BankAccount transactionFee practice-it

Exercise : BankAccount transfer practice-it

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.

To solve this problem, you may want to refer to the lecture slides about the syntax for constructors.

See the Practice-It link above for a full description of the class and the methods/constructors it should have.

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.

If you finish them all...

If you finish all the exercises, try out our Practice-It web tool. It lets you solve Java problems from our Building Java Programs textbook.

You can view an exercise, type a solution, and submit it to see if you have solved it correctly.

Choose some problems from the book and try to solve them!