EECS2030Z Test 2
PRACTICE
Question 1
Implement the utility
class described by this API .
You do not have to include javadoc comments.
Question 1
Starting with the code given below, implement the constructors for the
class described by this API . Use constructor chaining where
possible when implementing your constructors.
You do not have to include javadoc comments.
package test2;
import java.awt.Color;
public class Circle {
/**
* The radius of the circle.
*/
private double radius;
/**
* The color of the circle.
*/
private Color color;
}
Question 3
Create a text file named answers.txt
(use File ->New ->
Untitled text file in eclipse). Type your answer
to the following question in the text file.
A.
Consider the field Utility2P.TEST_VERSION
. What does the
keyword final
mean when used to modify a field?
B.
Suppose a client writes a main
method that includes the
following two lines of Java code:
List<Character> aList = Arrays.asList('g', 'o', 'o', 'd', 'b', 'y', 'e');
Utility2P.shuffle(aList);
The first line of code creates the list ['g', 'o', 'o', 'd', 'b', 'y', 'e']
,
and the second line of code calls the shuffle
method from Question 1.
The memory diagram illustrating the state of memory for the two lines of code
is shown below.
What suitable values of a, b, and c would complete the memory diagram?
---------------------
| main method |
---------------------
aList | a? |
---------------------
| |
| |
---------------------
b? | List object |
---------------------
| |
| |
---------------------
| Utility2P.shuffle |
---------------------
t | c? |
---------------------
C.
Consider the method Utility2P.hello(String)
from Question 1.
What precondition does the method have? Suppose that as the implementer,
you wanted to remove the precondition from Utility2P.hello(String)
;
how could you change your implementation to remove the precondition?
D.
Provide 3 test cases for the method Utility2P.toString(List<Character>)
.
Make sure that each test case tests a different feature of the method (i.e., don't
provide 3 test cases that all check if the correct string was returned).
For each test case, provide a one sentence explanation of what the test case
is testing.
E.
Consider the following implementation of equals(Object)
for the Circle
class in Question 2:
@Override
public boolean equals(Object obj) {
if (obj == null) {
return false;
}
if (this.getClass() != obj.getClass()) {
return false;
}
Circle other = (Circle) obj;
if (!this.color.equals(other.color)) {
return false;
}
if (Double.doubleToLongBits(this.radius) != Double
.doubleToLongBits(other.radius)) {
return false;
}
return true;
}
What is missing from the implementation shown above compared to how
equals
is normally implemented? Explain whether or not
the implementation shown above still satisfies the equals contract.