EECS2030E Test 3

Tuesday October 25, 2016, 16:00-17:20
Lab 02
Version F

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 test3F Weight.java


package eecs2030.test3;


public class Weight {

    public static final String KG = "kg";

    public static final String LB = "lb";
    
    public static final double KG_PER_LB = 0.45359237;

    // Only one of kg or lb is needed; the other value can be computed
    // when it is required.
    private double kg;
    private double lb;
    private String units;

    public Weight() {
        this(0.0, Weight.KG);
    }

    public Weight(Weight other) {
        this(other.get(), other.getUnits());
    }

    public Weight(double wt, String units) {
        this.setUnits(units);
        this.set(wt);
    }

    public double get() {
        if (this.units.equals(Weight.KG)) {
            return this.kg;
        }
        return this.lb;
    }

    public final void set(double wt) {
        if (wt < 0.0) {
            throw new IllegalArgumentException();
        }
        if (this.units.equals(Weight.KG)) {
            this.kg = wt;
            this.lb = Weight.toPounds(wt);
        } else {
            this.lb = wt;
            this.kg = Weight.toKilograms(wt);
        }
    }

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

    public final void setUnits(String units) {
        if (!units.equals(Weight.KG) && !units.equals(Weight.LB)) {
            throw new IllegalArgumentException();
        }
        this.units = units;
    }

    public static double toPounds(double kg) {
        return kg / Weight.KG_PER_LB;
    }

    public static double toKilograms(double lb) {
        return lb * Weight.KG_PER_LB;
    }

    @Override
    public String toString() {
        if (units.equals(Weight.KG)) {
            return this.kg + " " + Weight.KG;
        } else {
            return this.lb + " " + Weight.LB;
        }
    }

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

}

Short Answer Questions

Question 1

What is a final method?

A. a method that removes the object from memory
B. a method that the implementer can override
C. a method that the implementer can not override
D. a method that the implementer must override

Question 2

Suppose we implement a Person class where a Person has-a Weight. What is the relationship between the classes Person and Weight called if a Person object owns its Weight object?

Person and weight form a composition.


Question 3 State 3 different test cases for the method equals(Object) in the class Weight. Make sure that the test cases test different ways that the method might fail.

There are many possible answers; here are a few:

input: null
expected result: false

input: String (or any other object with type not Weight)
expected result: false

input: a Weight equal to this Weight but with different units
expected result: true

input: a Weight equal to this Weight but with same units
expected result: true


Question 4

Suppose that we wanted to provide the following two constructors for Weight:

/**
 * Initializes a weight to the given weight in kilograms.
 */
public Weight(double kg) { // implementation not shown }


/**
 * Initializes a weight to the given weight in pounds.
 */
public Weight(double lb) { // implementation not shown }

(a) Why can we not provide both of these constructors?
(b) What technique was described in the lecture slides that could be used in place of one or both of the constructors?

(a) They have the same signature (they have the same name and the parameter lists both consist of one double value).
(b) Create static factory methods:

public static Weight kilograms(double kg) { // ... }
public static Weight pounds(double lb) { // ... }



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