EECS2030E Test 3

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

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 most of 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 the 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 test3C Temperature.java


package eecs2030.test3;

public class Temperature {

    public static final String CELCIUS = "C";

    public static final String FAHRENHEIT = "F";

    // Only one of degC or degF is actually required; the other can
    // be computed when it is needed.
    private double degC;
    private double degF;
    private String units;

    public Temperature() {
        this(0.0, Temperature.CELCIUS);
    }

    public Temperature(Temperature other) {
        this(other.getTemperature(), other.getUnits());
    }

    public Temperature(double temp, String units) {
        this.setUnits(units);
        this.setTemperature(temp);
    }

    public double getTemperature() {
        if (this.units.equals(Temperature.CELCIUS)) {
            return this.degC;
        }
        return this.degF;
    }

    public final void setTemperature(double temp) {
        if (this.units.equals(Temperature.CELCIUS)) {
            this.degC = temp;
            this.degF = Temperature.toFahrenheit(temp);
        } else {
            this.degF = temp;
            this.degC = Temperature.toCelcius(temp);
        }
    }

    public String getUnits() {
        return this.units;
    }

    public final void setUnits(String units) {
        if (!units.equals(Temperature.CELCIUS) && !units.equals(Temperature.FAHRENHEIT)) {
            throw new IllegalArgumentException();
        }
        this.units = units;
    }

    public static double toCelcius(double degF) {
        return (degF - 32.0) * 5.0 / 9.0;
    }

    public static double toFahrenheit(double degC) {
        return degC * 9.0 / 5.0 + 32.0;
    }

    @Override
    public String toString() {
        if (this.getUnits().equals(Temperature.CELCIUS)) {
            return this.getTemperature() + Temperature.CELCIUS;
        } else {
            return this.getTemperature() + Temperature.FAHRENHEIT;
        }
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        Temperature other = (Temperature) obj;
        if (Double.doubleToLongBits(degC) != Double.doubleToLongBits(other.degC))
            return false;
        return true;
    }

}

Short Answer Questions

Question 1

What is an obligatory method?

A. a method that can be invoked using any object reference
B. a method that the client must invoke, such as a constructor
C. a method that the implementer must override
D. a method that the implementer must not override

Question 2

What is the name of the relationship between the two classes shown in the following UML diagram:



aggregation


Question 3 State 3 different test cases for the method setUnits(String) in the class Temperature. Make sure that the test cases test different ways that the method might fail.

There are many possible answers; here are a few.
inputs : "celcius"
expected result: IllegalArgumentException

inputs : "C"
expected result: t.getTemperature() returns temperature in deg C
t.getUnits() returns "C"

inputs : "F"
expected result: t.getTemperature() returns temperature in deg F
t.getUnits() returns "F"


Question 4

Suppose that we have Temperature implement the Comparable interface by providing the following implementation of compareTo:

public int compareTo(Temperature other) {
  double t = this.getTemperature();
  double u = other.getTemperature();
  return Double.compare(t, u);
}

The implementation technically satisfies the requirements of compareTo but could be improved. Briefly explain how you would improve this implementation of compareTo.

The given implementation compares temperatures ignoring the units of the temperatures; a better implementation would compare temperatures using the same units.



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 test3C answers.txt