Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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 BigDecimalTree = new TreeSet (); BigDecimalTree.add (x); BigDecimalTree.add (y); // TreeSet uses compareTo(), so BigDecimalTree now has 1 element Set BigDecimalHash = new HashSet (); BigDecimalHash.add (x); BigDecimalHash.add (y); // HashSet uses equals(), so BigDecimalHash now has 2 elements } // Test: System.out.println ("BigDecimalTree = " + BigDecimalTree); // System.out.println ("BigDecimalHash = " + BigDecimalHash); // Expected: BigDecimalTree = 1; BigDecimalHash = 1 // See Java Doc for add() in Set Interface // The problem is that in BigDecimal, equals() and compareTo() // are inconsistent. Lets suppose we decide that compareTo() is correct, // and that equals()is faulty. Note: Relevant to Bloch, Item 12 page 62. Book website: BigDecimalTest.java

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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Rules In Database Systems Third International Workshop Rids 97 Sk Vde Sweden June 26 28 1997 Proceedings Lncs 1312

Authors: Andreas Geppert ,Mikael Berndtsson

1997th Edition

3540635165, 978-3540635161

More Books

Students also viewed these Databases questions