Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Knute Snortum
  • Paul Clapham
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Frits Walraven
Bartenders:
  • Ganesh Patekar
  • Tim Holloway
  • salvin francis

Java Polymorphism and inheritance in Shape Hierarchy  RSS feed

 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello

Question

Implement the Shape hierarchy shown in Fig. 9.3.

1. create an abstract class Shape that has
a. two private instance variable
X which specifies the X coordinate and
Y which specifies the Y coordinate
b. A constructor with two arguments that sets the values of both instance variables to those passed as parameters
c. set and get methods
d. A toString method and an abstract method getName().

2. Derive an abstract class TwoDimensionalShape that inherits Shape and has
a. Two private instance variable
Dimension1 and Dimension2
b. A constructor with four arguments and also
c. set and get methods
d. An abstract method getArea()

3. Derive an abstract class ThreeDimensionalShape that inherits Shape and has
a. Three private instance variable
Dimension1 , Dimension2 and Dimension3
b. A constructor with five arguments and also
c. set and get methods
d. abstract methods getArea() and getVolume()


4. Derive classes circle and square from TwoDimensionalShape with
a. Appropriate constructor method,
b. Set and get methods and
c. Implement Override getArea() to calculate area of circle and square.

5. Derive classes cube and sphere from ThreeDimensionalShape with
d. Appropriate constructor method,
e. Set and get methods and
f. Implement Overridden getArea() to calculate area of cube and sphere.
g. Implement getVolume to calculate volume of cube and sphere.

Each TwoDimensionalShape should contain method getArea to calculate the area of the two-dimensional shape.
Each ThreeDimensionalShape should have methods getArea and getVolume to calculate the surface area and volume, respectively, of the three-dimensional shape.

Create a driver program that uses an array of Shape references to objects of each concrete class in the hierarchy.

The program should print a text description of the object to which each array element refers. Also, in the loop that processes all the shapes in the array, determine whether each shape is a TwoDimensionalShape or a ThreeDimensionalShape.

If it’s a TwoDimensionalShape, display its area.

If it’s a ThreeDimensionalShape, display its area and volume

The code I wrote so far



























My questions

How do I Implement Override getArea() to calculate area of circle and square?
Are there specific rules to calculate the shapes in Java?
Is my code good so far am I going the right way?










 
Marshal
Posts: 64493
225
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have a serious design problem, that you are repeating fields. In the square class you have a local field called side which you do not set in the constructor and two superclass fields which you don't set in the setSide method.
Remember that fields are not polymorphic, so you must not repeat fields in the subclasses. In the case of square and circle you will have to make the setXXX methods set both fields simultaneously.
 
Lucy Margret
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:You have a serious design problem, that you are repeating fields. In the square class you have a local field called side which you do not set in the constructor and two superclass fields which you don't set in the setSide method.
Remember that fields are not polymorphic, so you must not repeat fields in the subclasses. In the case of square and circle you will have to make the setXXX methods set both fields simultaneously.



Can you please write a code to show me how to implement override getArea method to calculate area of circle and square?




I edited the code for the Circle class, do I write the same code for the Square class? but how i implement the override getArea method?

 
Lucy Margret
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Re wrote the Square class



 
Campbell Ritchie
Marshal
Posts: 64493
225
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The formatting is better but the problem of having excess fields remains. There is no need for any fields in the square class; the private fields in the 2D class can be accessed via getXXX and setXXX methods. In the case of square or circle, you may have to set both to the same value, rather like what you are doing in the square constructor. I have only looked at the square class, so I suspect the same problem will occur in the other classes.
You can simplify your getArea method and get rid of the calculate method. I presume getArea is an abstract method in the 2D class?Every object implicitly implements this method and if you go through what that method returns, you will find several getXXXName methods. You might be able to use that in your getName method.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!