EECS2030E Test 3

Monday October 24, 2016, 14:00-15:20
Lab 01
Version D

Instructions

There is one programming question and a set of short answer questions.

Instructions for submitting your programming question solution are given in the Programming question section. You may submit as many times as you want; your most recent submission will be the one recorded.

Instructions for submitting your answers to the short answer questions are given in the Short Answer Questions section. You may submit as many times as you want; your most recent submission will be the one recorded.

Programming question

Implement this API. A skeleton can be found here here. The skeleton compiles, but the constructors are not implemented and the methods do not return the correct values; you should complete the constructors and methods to satisfy their APIs. The provided code does not provide you with non-static fields; you need to decide which fields the class requires.

For reference: the Java API is here.

Submit your program using the following command in a terminal (make sure you are in the directory containing your file Temperature.java):

submit 2030 test3D Hour.java


package eecs2030.test3;

public class Hour {
    public static final String AM = "AM";

    public static final String PM = "PM";

    // The hour on a 24-hour clock.
    private int hour;

    public static boolean isValid(int hour) {
        return hour >= 0 && hour <= 23;
    }

    public static boolean isValid(int hour, String ampm) {
        return hour >= 1 && hour <= 12 && (ampm.equals(Hour.AM) || ampm.equals(Hour.PM));
    }

    public static void check12(int hour, String ampm) {
        if (!Hour.isValid(hour, ampm)) {
            throw new IllegalArgumentException("");
        }
    }

    public static void check24(int hour) {
        if (!Hour.isValid(hour)) {
            throw new IllegalArgumentException("hour must be between 0 and 23 on a 24-hour clock");
        }
    }


    public static int to24HourClock(int hour, String ampm) {
        Hour.check12(hour, ampm);
        if (hour == 12 && ampm.equals(Hour.AM)) {
            return 0;
        } else if (hour == 12 && ampm.equals(Hour.PM)) {
            return 12;
        } else if (ampm.equals(Hour.AM)) {
            return hour;
        }
        return hour + 12;
    }


    public static int to12HourClock(int hour) {
        Hour.check24(hour);
        if (hour == 0 || hour == 12) {
            return 12;
        } else if (hour < 12) {
            return hour;
        }
        return hour - 12;
    }


    public Hour(int hour) {
        Hour.check24(hour);
        this.hour = hour;
    }


    public Hour(int hour, String ampm) {
        Hour.check12(hour, ampm);
        this.hour = Hour.to24HourClock(hour, ampm);
    }


    public Hour(Hour other) {
        this.hour = other.hour;
    }


    public int getHour() {
        return this.hour;
    }


    public void setHour(int hour) {
        Hour.check24(hour);
        this.hour = hour;
    }


    public void setHour(int hour, String ampm) {
        Hour.check12(hour, ampm);
        this.hour = Hour.to24HourClock(hour, ampm);
    }


    public boolean equals(String h) {
        String[] parts = h.split(" ");
        int hour = Integer.parseInt(parts[0]);
        String ampm = parts[1];
        boolean eq = Hour.isValid(hour, ampm);
        if (eq) {
            eq = this.hour == Hour.to24HourClock(hour, ampm);
        }
        return eq;
    }

}

Short Answer Questions

Question 1

What term best describes an aggregation relationship?

A. has-a
B. is-a
C. owns-a
D. uses-a

Question 2

In one sentence state what the purpose of a singleton class is.

To ensure that there is exactly 0 or 1 instance of a class.


Question 3

State 3 different test cases for the method to12HourClock(int) in the class Hour. Make sure that the test cases test different ways that the method might fail.

There are many possible answers; here are a few:
inputs : 0
expected result: 12

inputs : 23
expected result: 11

inputs : -1
expected result: IllegalArgumentException

inputs : 24
expected result: IllegalArgumentException


Question 4

State two class invariants that the Hour class has.

1. The hour is greater than or equal to zero.
2. The hour is less than or equal to 23.



Enter your answers into a text file named answers.txt. You can use the editor of your choice for this. You can also create a text file in Eclipse by selecting "New" and then "Untitled Text File" in the "File" menu. Make sure that you save your file. Also make sure that you indicate which question you are answering, e.g. Q1: answer to question 1 Q2: answer to question 2 Q3:...

Submit your answers using the following command in a terminal (make sure you are in the directory containing your file answers.txt):

submit 2030 test3D answers.txt