EECS2030E Test 3

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

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 Gray.java):

submit 2030 test3E Gray.java


package eecs2030.test3;

import java.awt.Color;

public final class Gray implements Comparable {

    // You could store both the double and int gray values, but
    // just the double gray value is sufficient.
    // The final modifier isn't necessary.
    // The field asInt is only necessary to implement toString;
    // otherwise everything else can be implemented using just a double
    // gray value.
    final private double gray;
    final private boolean isInt;

    public final static double MIN_DOUBLE_VALUE = 0.0;
    
    public final static double MAX_DOUBLE_VALUE = 1.0;
    
    public final static int MIN_INT_VALUE = 0;
    
    public final static int MAX_INT_VALUE = 255;

    public Gray(double g) {
        if (g < 0.0 || g > 1.0) {
            throw new IllegalArgumentException("value must be between 0 and 1");
        }
        this.gray = g;
        this.isInt = false;
    }

    public Gray(int g) {
        this.gray = Gray.toDouble(g);
        this.isInt = true;
    }

    public Gray(Gray other) {
        this.gray = other.gray;
        this.isInt = other.isInt;
    }

    public int asInt() {
        return Gray.toInt(this.gray);
    }

    public double asDouble() {
        return this.gray;
    }

    public static double toDouble(int value) {
        if (value < 0 || value > 255) {
            throw new IllegalArgumentException("value must be between 0 and 255");
        }
        return value / Gray.MAX_DOUBLE_VALUE;
    }

    public static int toInt(double value) {
        if (value < 0.0 || value > 1.0) {
            throw new IllegalArgumentException("value must be between 0 and 1");
        }
        return (int) Math.round(Gray.MAX_DOUBLE_VALUE * value);
    }

    public static Gray fromRGB(Color c) {
        double red = (double) c.getRed() / Gray.MAX_INT_VALUE;
        double green = (double) c.getGreen() / Gray.MAX_INT_VALUE;
        double blue = (double) c.getBlue() / Gray.MAX_INT_VALUE;
        double gray = 0.299 * red + 0.587 * green + 0.114 * blue;
        return new Gray(gray);
    }

    @Override
    public String toString() {
        String s = "";
        if (this.isInt) {
            s += this.asInt();
        } else {
            s += this.asDouble();
        }
        return s;
    }

    @Override
    public int compareTo(Gray other) {
        int result = 0;
        if (this.gray < other.gray) {
            result = -1;
        } else if (this.gray > other.gray) {
            result = 1;
        }
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        Gray other = (Gray) obj;
        if (this.asInt() != other.asInt())
            return false;
        return true;
    }

}

Short Answer Questions

Question 1

What term best describes a composition relationship?

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

Question 2

What kind of constructor is public Gray(Gray other)? (The answer is not "A public constructor").

A copy constructor.


Question 3

State 3 different test cases for the method toDouble(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.
A test case for toInt(double) requires an double input value and the expected result of invoking toInt(double) with that input value:

input : -0.001
expected result: IllegalArgumentException

input : 1.001
expected result: IllegalArgumentException

input : 0.0
expected result: 0

input : 1.0
expected result: 255

input : 0.5
expected result: 128


Question 4

Explain whether or not the method compareTo in the class Gray is consistent with equals.

compareTo is not consistent with equals. compareTo compares gray values as double values and equals compares gray values as int values. Consider the following:

Gray g1 = new Gray(0.0);
Gray g2 = new Gray(0.00001);
int c = g1.compareTo(g2); // compare as double: 0.0 < 0.00001
boolean eq = g1.equals(g2); // compare as int: 0 == 0

c is a value less than 0 and eq is true.



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