Home CPSC 240

Lab 4: Time Class

 

Objective

To get more practice writing classes with constructors and methods in Java


 

Task

For this lab, you will create a class called "Time" that will allow us to store and work with time objects.

Your class should contain the following:

You are just writing the Time class for this lab. To test your class, you can use this main class:


public class Main {
    public static void main(String args[]) {
        Time t1 = new Time();              // midnight
        Time t2 = new Time(17, 30, 0);     // 5:30 PM

        // add an hour onto t1, then print it in both formats
        t1.increment(3600);
        t1.print(true);
        t1.print(false);

        // add an hour and a half, and 30 seconds onto t2
        t2.increment(5430);

        // print t2 in both formats
        t2.print(true);
        t2.print(false);

    }
}

The goal output is given below:

1:00:00
1:00:00 AM
19:00:30
7:00:30 PM

 

Submitting

When you are done, please submit the Java code under the assignment in Canvas.

You only need to submit the Time.java file, not the Main.java one.


 

Solution

A solution to this lab can be found in Time.java.

Copyright © 2024 Ian Finlayson | Licensed under a Attribution-NonCommercial 4.0 International License.