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.
Download files: Rectangle.java, Shape.java, Circle.java, Shapes.java, Square.java
Areas: More advanced classes and objects.
|
|
Rectangle.java |
|
View full source |
|
|
Shape.java |
|
View full source |
|
|
Circle.java |
|
View full source |
|
|
Shapes.java |
|
View full source |
|
|
Square.java |
|
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? |
| Question 2 |
| Can we instantiate a Shape object? |
| Question 3 |
| Suppose we add the following code to Shape.java.
public Shape() { // do nothing } |
| Question 4 |
| If we remove the keyword abstract from the class declaration of Shape, can we then instantiate a Shape object? |
| Question 5 |
| If we remove the abstract method getArea() from Shape.java, can we then instantiate a Shape object? |
| 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? |
| 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? |
| Question 8 |
| Can you guess what would happen if we change the getArea() method to abstract private? |
| Question 9 |
| If we add the constructor below to Shape.java
public Shape(int xVal, int yVal) { x = xVal; y = yVal; } |
| Question 10 |
| If we add the constructor below to Shape.java, will
the program compile?
private Shape() { // do nothing } |
| 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? |
| Question 12 |
| Will the statement below cause any compile or run time error?
Shape r = new Rectangle(10, 10); |
| Question 13 |
| In Rectangle.java, the toString() method uses
the code:
return "x = " + x + "; y = " + y + "; height = " + height + "; width = " + width; |
| 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? |
| Question 15 |
| If we change x and y to private and leave everything else unchanged, will Rectangle.java be affected? |
| Question 16 |
| If we change x and y to private, will Circle.java be affected? |
| Question 17 |
| What is the output of the code segment below?
Square squ = new Square(5); system.out.println(squ.toString()); |
| Question 18 |
| Can you explain the meaning of ((Rectangle) a[1]) on line 59 in Shapes.java? |
| Question 19 |
| In the context of Shapes.java, will the code ((Rectangle) a[2]).exclusiveRectangleMethod(), cause any compile or run time error? |
| Question 20 |
| Can a Square object call the exclusiveRectangleMethod() method? |
