simple graphics package that will allow you to draw and manipulate simple geometric shapes

profilegazm_8

You are developing a simple graphics package that will allow you to draw and manipulate simple geometric shapes such as squares, rectangles, etc. Your initial design is along the following lines:

public class Rectangle {
   private int h, w;
   public int getHeight() {…}
   public int setHeight(int x) {…}
   ...
}

 

public class Square extends Rectangle {
   // ???
}

This question has a ‘devil’s advocate’ nature to it and is in two parts.

  1. You intend to model a square as inheriting from a rectangle. How would you complete the code on the above right to realize this intention?
  2. No matter how you do design the code for the square, you will be able to write code that will break the meaning of what it means to be a square or rectangle.  After completing Part a, discuss the flaw in your design and outline some code that will cause it to break (your code should compile fine and will also execute without an error but will violate how a rectangle or square should behave).
    • 9 years ago
    • 15
    Answer(0)
    Bids(0)