Computer Program Reading Comprehension Project
U of T Computer Program Reading Comprehension Project
Shapes
This exercise contains multiple Java classes that implement different geometric shapes.

Download files: Rectangle.java, Shape.java, Circle.java, Shapes.java, Square.java
Areas: More advanced classes and objects.


Rectangle.java
1/**
2 *  instructor: Ray Ortigas
3 *  Date: Summer 2000
4 *  http://www.dgp.toronto.edu/~rayo/tutorials/108/2000summer/
5 */
6
7// A rectangle.
8public class Rectangle extends Shape {
9
10    // The height of this rectangle.
Displaying 10 of 37 lines
View full source


Shape.java
1/**
2 * instructor: Ray Ortigas
3 * Date: Summer 2000
4 * http://www.dgp.toronto.edu/~rayo/tutorials/108/2000summer/
5 */
6
7// A shape.
8abstract public class Shape {
9
10    // The x-coordinate of this shape.
Displaying 10 of 29 lines
View full source


Circle.java
1/**
2 * instructor: Ray Ortigas
3 * Date: Summer 2000
4 * http://www.dgp.toronto.edu/~rayo/tutorials/108/2000summer/
5 */
6
7// A circle.
8public class Circle extends Shape {
9
10    // The radius of this circle.
Displaying 10 of 30 lines
View full source


Shapes.java
1/** instructor: Ray Ortigas
2 *  Date: Summer 2000
3 *  http://www.dgp.toronto.edu/~rayo/tutorials/108/2000summer/
4 *
5 *
6 * A small program to test the classes in the Shape hierarchy.
7 *
8 * The Shape hierarchy looks like this:
9 *
10 *         Shape
Displaying 10 of 62 lines
View full source


Square.java
1/**
2 * instructor: Ray Ortigas
3 * Date: Summer 2000
4 * http://www.dgp.toronto.edu/~rayo/tutorials/108/2000summer/
5 */
6
7// A square.
8public class Square extends Rectangle {
9
10        // Constructs a new square with side length 'x'.
Displaying 10 of 14 lines
View full source


Question 1
This exercise consists of four Java classes and one driver program. Can you tell which files implement the class hierarchy and which file is the driver program?
Hints Solution

Question 2
Can we instantiate a Shape object?
Hints Solution

Question 3
Suppose we add the following code to Shape.java.

  public Shape() {
     // do nothing
  }
      
That is, we provide Shape with a constructor. Can we then instantiate a Shape object?
Hints Solution

Question 4
If we remove the keyword abstract from the class declaration of Shape, can we then instantiate a Shape object?
Hints Solution

Question 5
If we remove the abstract method getArea() from Shape.java, can we then instantiate a Shape object?
Hints Solution

Question 6
If we remove the keyword abstract from both the class declaration and the method declaration of getArea(), can we then instantiate a Shape object?
Hints Solution

Question 7
If we remove abstract from the class declaration of Shape and remove the abstract method getArea(), can we then instantiate a Shape object?
Hints Solution

Question 8
Can you guess what would happen if we change the getArea() method to abstract private?
Hints Solution

Question 9
If we add the constructor below to Shape.java

  public Shape(int xVal, int yVal) {
     x = xVal;
     y = yVal;
  }
            
will the program still compile?
Hints Solution

Question 10
If we add the constructor below to Shape.java, will the program compile?

  private Shape() {
     // do nothing
  }
            
Hints Solution

Question 11
If we cannot instantiate Shape objects without changing the code, why does the statement Shape[] a = new Shape[5]; in Shapes.java cause no error?
Hints Solution

Question 12
Will the statement below cause any compile or run time error?
Shape r = new Rectangle(10, 10);
Hints Solution

Question 13
In Rectangle.java, the toString() method uses the code:

  return "x = " + x + "; y = " + y + "; height = " 
      + height + "; width = " + width;
      
Where are the variables x and y from?
Hints Solution

Question 14
The variables x and y belong to the Shape class; however, Rectangle.java can access them directly. Is this a good programming practice?
Solution

Question 15
If we change x and y to private and leave everything else unchanged, will Rectangle.java be affected?
Hints Solution

Question 16
If we change x and y to private, will Circle.java be affected?
Hints Solution

Question 17
What is the output of the code segment below?

  Square squ = new Square(5);
  system.out.println(squ.toString());
      
Hints Solution

Question 18
Can you explain the meaning of ((Rectangle) a[1]) on line 59 in Shapes.java?
Hints Solution

Question 19
In the context of Shapes.java, will the code ((Rectangle) a[2]).exclusiveRectangleMethod(), cause any compile or run time error?
Hints Solution

Question 20
Can a Square object call the exclusiveRectangleMethod() method?
Hints Solution