Assignment on inheritance

 


Download the file Rectangle.java and examine it carefully.

Create a program for each question and implement in inherits.java.

Question 1

 

Using inheritance from class Rectangle, create a new class called Square which implements the shape square. (a square is a rectangle with equal witdh and height) Add a main method to the class to execute a suite of test cases. Make sure you document this method by explaining the test cases. You are required to submit  Square.java.

Question 2

 

Using inheritance from class Rectangle, create a class called SafeRectangle in which height and width can not be set to negative values. If an attempt to violate this restriction is made then an error message will be issued after which the program will immediately abort.

System.exit(1)

 

shuts down your program

Add a main method to the class to execute a suite of test cases. Make sure you document this method by explaining the test cases. You are required to submit  SafeRectangle.java.

Question 3

 

Using inheritance from either class Square or SafeRectangle create a class called SafeSquare with a similar added restriction. In the documentation of the class, justify your choice of a parent class. Add a main method to the class to execute a suite of test cases. Make sure you document this method by explaining the test cases. You are required to submit  SafeSquare.java

Question 4

 

In class Rectangle change the access modifiers of the height and width fields from protected to private, then re-compile your classes. If you get any error messages, modify classes Rectangle, Square, SafeRectangle, SafeSquare so that they are correct with regard to the change. You are required to submit the updated files Rectangle.java, Square.java, SafeRectangle.java and SafeSquare.java.

Question 5

 

Suppose we have the following collection of abstract classes and classes: Beaver, Mammal, Dolphin, Flyer, Animal, Emu, Cat, Bird, Walker, Swimmer, Bat, Crow, Cormorant, Penguin. Indicate the following:

  1. which are likely to be the abstract classes?
  2. which are the likely "extends" relationships ?
  3. draw a diagram showing the parent and child classes(grandchildren if necessary)?


  Do this in word.