Subclass: UTC2
Here is the new definition of UTC2
.
The extends UTC1
declares UTC1
to be the superclass, so all of the fields and methods
defined in UTC1
will be inherited by
UTC2
.
There might be some methods in UTC1
that
rely on the h
field being the UTC hour rather
than the EST hour.
We don't want to break those methods, so we define our
own h
field in UTC2
.
The call to super
inside the Java constructor
invokes the Java constructor for the superclass, which
initializes the h
and m
fields
of the superclass.
The assignment to this.h
then initializes
the h
field of UTC2
.
// Constructor template for UTC2: // new UTC2 (h, m) // Interpretation: // h is the UTC hour (between 0 and 23, inclusive) // m is the UTC minute (between 0 and 59, inclusive) // Representation: // Internally, the hour is represented in Eastern Standard Time, // which is five hours behind UTC. class UTC2 extends UTC1 implements UTC { // the h field inherited from UTC1 will not be used // we will use the following h field instead int h; // the hour in EST, limited to [0,23] // the Java constructor UTC2 (int h, int m) { super (h, m); // invokes the constructor for UTC1 this.h = (h >= 5) ? (h - 5) : h + 19; } // public methods // Returns the hour, between 0 and 23 inclusive. @Override public int hour () { if (h < 19) return h + 5; else return h - 19; } // The minute, isEqual, equals, hashCode, and toString // methods are inherited from UTC1. // a main method for unit testing public static void main (String[] args) { UTC2tests.main(args); } }