ITI 1121. Introduction to Computer Science II

Laboratory 3

Winter 2015


Declaring new types

Class declarations are one of the ways of defining new types in Java. For this laboratory, we will create two classes that both represent time values for a 24 hours period. The valid operations are as follows.

Note: at the end of this laboratory, you will submit the content of a directory called l3_123456, where 123456 has been substituted by your student id. You might as well create the directory now and save all the files that you will be creating into that directory.

1 Time1

Consult the declaration of the class Time1.

Modify the declaration of the class Time1 and implement the following two methods.

Create a new class called Utils1 containing the following two class methods:

2 Time2

Let’s consider an alternative way of representing time values. Create a new class called Time2. Instead of having three instance variables (hours, minutes and seconds) like Time1, Time2 has a single instance variable, timeInSeconds. For instance, the time value 2:5:10 will be represented as

timeInSeconds = 10 + 5*60 + 2*60*60 = 10 + 300 + 7200 = 7510

The maximum value for the variable timeInSeconds is the total number of seconds for a 24 hours period minus 1.

Note: Just like Time1, the constructor of the class Time2 has three parameters, hours, minutes, and seconds.

Copy the file to and make all the necessary changes so that Utils2 uses Time2 objects instead of Time1 objects.

3 Quiz (1 mark)

Knowing that the instance variable timeInSeconds of the class Time2 has been declared to be private. The following implementation of the method equals, within the class Time2, is valid. True or False.

public boolean equals(Time2 other) {  
    return timeInSeconds == other.timeInSeconds;  

Answer the question on Blackboard Learn:

Last Modified: January 29, 2015