6. (5 points) An equals function is used to compare two objects for equality. Is the
following hashCode function poorly written? yes or no and why.
@Override
public boolean equals(Object o) {
if(this == o) return true;
return this.x == o.x
&& this.y == o.y
&& this.angle == o.angle
&& this.currentHealth == o.currentHealth;
}

interface Shape {
public void draw();
}
class Triangle implements Shape {
public void draw() {
System.out.println(“Triangle Drawn”);
}
}
class Box implements Shape {
public void draw() {
System.out.println(“Box Drawn”);
}
public static void specialDraw(){
System.out.println(“Box Drawn Specially”);
}
}
class Cube extends Box {
public void draw() {
System.out.println(“Cube Drawn”);
}
public static void specialDraw(){
System.out.println(“Cube Drawn Specially”);
}
}
public class Exam {
public static void main(String[] args) {
Shape s = new Box();
s.draw();
Cube c = new Cube();
s = c;
s.draw();
}
}
7. (3 points) [True] or [False] The output of the program is “Box Drawn” then “Cube
Drawn” on separate lines.
8. (3 points) [True] or [False] The function specialDraw dened in Cube overrides the
specialDraw function dened in Box.
9. (3 points) [True] or [False] Given the statement s.draw(), the method behavior is
attached at run-time (i.e. dynamically bound).
10. (3 points) True] or [False] The function draw dened in Cube overloads the draw
function dened in Box.
11. (3 points) [True] or [False] The variable s is an interface type.



Source link

Leave a Reply

Your email address will not be published.