Question
Consider the following three example classes. These are OO faults taken from Joshua Blochs Effective Java, Second Edition. Answer the following questions about each. (a)
Consider the following three example classes. These are OO faults taken from Joshua Blochs Effective Java, Second Edition. Answer the following questions about each.
(a) Explain what is wrong with the given code. Describe the fault precisely by proposing a modification to the code. (b) If possible, give a test case that does not execute the fault. If not, briefly explain why not. (C) Implement your repair and verify that the given test now produces the expected output. Submit a screen printout or other evidence that your new program works.
(D) submit your answers along with a the implemented code. add a comment to indicate your change.
class Vehicle implements Cloneable { private int x; public Vehicle (int y) { x = y;} public Object clone() { Object result = new Vehicle (this.x); // Location "A" return result; } // other methods omitted } class Truck extends Vehicle { private int y; public Truck (int z) { super (z); y = z;} public Object clone() { Object result = super.clone(); // Location "B" ((Truck) result).y = this.y; // throws ClassCastException return result; } // other methods omitted } // Test: Truck suv = new Truck (4); Truck co = suv.clone() // Expected: suv.x = co.x; suv.getClass() = co.getClass() Note: Revelant to Bloch, Item 11 page 54. Book website: Vehicle.java, Truck.java, CloneTest.java
public class BigDecimalTest { BigDecimal x = new BigDecimal ("1.0"); BigDecimal y = new BigDecimal ("1.00"); // Fact: !x.equals (y), but x.compareTo (y) == 0 Set
class Point { private int x; private int y; public Point (int x, int y) { this.x=x; this.y=y; } @Override public boolean equals (Object o) { // Location A if (!(o instanceof Point)) return false; Point p = (Point) o; return (p.x == this.x) && (p.y == this.y); } } class ColorPoint extends Point { private Color color; // Fault: Superclass instantiable; subclass state extended public ColorPoint (int x, int y, Color color) { super (x,y); this.color=color; } @Override public boolean equals (Object o) { // Location B if (!(o instanceof ColorPoint)) return false; ColorPoint cp = (ColorPoint) o; return (super.equals(cp) && (cp.color == this.color)); } // Tests: Point p = new Point (1,2); ColorPoint cp1 = new ColorPoint (1,2,RED); ColorPoint cp2 = new ColorPoint (1,2,BLUE); p.equals (cp1); // Test 1: Result = true; cp1.equals (p); // Test 2: Result = false; cp1.equals (cp2); // Test 3: Result = false; // Expected: p.equals (cp1) = true; cp1.equals (p) = true, // cp1.equals (cp2) = false Note: Relevant to Bloch, Item 17 page 87. Book website: Point.java, ColorPoint.java, PointTest.java
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started