Implement a shape hierarchy. you must have your superclass shape and 2 subclasses two-dimensional shape and three-dimensional shape. Under two-dimensional shape, you have other subclasses, circle, square, and triangle. Under the three-dimensional shape you have the sphere, cube, and tetrahedron. Each two-dimensional shape should contain a method getArea to calculate the area of the two-dimensional shape. Each three-dimensional shape should have a method getArea and getVolume to calculate the surface area and volume, respectively, of the three-dimensional shape. Create a 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 two-dimensional shape or a three-dimensional shape. If a shape is a two-dimensional shape, display it's area. If a shape is a three-dimensional shape, display its area and volume.

## All 28 Replies

This is part of the Shape Hierarchy so it should help you get started...

``````public class Shape
{

}

public class Shape2d extends Shape
{

}

public class Shape3d extends Shape
{

}``````

Thank you! but how about the 2d-shapes circle, square and triangle also the 3-shapes sphere, cube and tetrahedron? Do i have to put them in classes??? where should i put them? I'm still confused. Help me please... Thank you!!!

You should search the web for some more information on polymorphism to help you. The other 2d and 3d shapes are subclasses of the Shape superclass, but you should be able to figure out how to implement them with my above code. The code above can be stated as:

Shape is a superclass
Shape2d is a subclass of Shape
Shape3d is a subclass of Shape

Whenever you see the word subclass it means that you are going to have to extend the functionality of a superclass. Hence the word "extends."

Does any of this make sense to you?

Oh Yeah! got it! I'll try my best to figure it out. Anyway, thanks for the tip!

Its easiest if you break things down line by line. For example...

"You must have your superclass shape and 2 subclasses two-dimensional shape and three-dimensional shape."

``````//superclass shape
public class Shape
{
}
//subclass 2d shape
public class Shape2d extends Shape
{
}
//subclass 3d shape
public class Shape3d extends Shape
{
}``````

"Under two-dimensional shape, you have other subclasses, circle, square, and triangle."

so now circle, square, and triangle are all subclasses of 2d shape so

``````public class Circle extends Shape2d
{
}``````

etc..

Understand it now?

ahh! I see!!! i understand it now! So all of the 2d shapes extends shape2d and all 3d shapes extends shape3d?? Am i right??? Thank you very much! You're good!!!

Yes, that is correct.

Ok Thank you! I'll try figuring out whats next on this...hehe

Your goal is to define a datatype Shape that can answer the question "Get Area?". You need to define the method getArea in each of the subclasses and declare the abstract method in the base class Shape. By declaring the abstract method in Shape, you're saying to the rest of the program that "a Shape is something that can answer the question 'Get Area'." Anything that is a Shape must be able to answer that question. Therefore, you need to define the method in your subclasses.

Also, you need to define the shape-specific information that each type of shape would like to know about itself.

Which part are you stuck on?

I don't know. I don't know where to start at the first place. It's kind of new to me because our teacher haven't discussed it yet to us. Can you help me???

Start by creating all the classes like I showed in my previous posts. Next add all the methods to the classes. Next implement all the methods.

Like this?

public class Shape
{

}

public class 2dshape extends Shape
{

}

public class Circle extends 2dshape
{

}

public class Square extends 2dshape
{

}

public class Triangle extends 2dshape
{

}

public class 3dshape extends Shape
{

}

public class Sphere extends 3dshape
{

}

public class Cube extends 3dshape
{

}

public class Tetrahedron extends 3dshape
{

}

So what's next?

Thats a start but Java doesn't allow you to start a class with a number so thats why I named mine Shape2d and Shape3d, so just rename them.

Now start adding all the constructors and methods to the classes.

OK!

what have you coded so far?

ui,anu n?

ui,anu n?

Read the rules and stick to using the English language

Can anyone please help me out with my program? I don't know what to do. I'm stuck with the classes. What is wrong with my program? i haven't finished it yet. please do help. Thanks a lot!

``````abstract class Shape
{

double PI;

{

PI = pi;

}
{

}
public double getPI()
{
return PI;
}
class shape2d extends Shape
{
double length;
shape2d(double r, double p, double l)
{

super(r, p);
length = l;

}

/*double getshape2d()
{
}

}*/

class Circle extends shape2d
{

Circle(double r, double p)
{
super(r,p);

}

public double getArea()
{
}

}

class Square extends shape2d
{   double side;
Square(double s)
{

side = s;
}
double getArea (){
return Math.pow(side,2.0);
}

}
class Triangle extends shape2d
{

Triangle(){

}
}
class shape3d extends Shape
{

}

class Sphere extends shape3d
{
double PI;
// Sphere(double p,double r)
//super();

}

}

class Cube extends shape3d
{

}

class Tetrahedron extends shape3d
{

}
``````

what i need is that the idea of object oriented programming in java on how display shape ,colour and calculate area

object programming in java on how to display geometrical shape like square ,circle,rectrangular,rectrangle,colour,

``````//Whoops, one too many comment test lol...
//well here is what's coded hope this helps those out there on the struggle :D
//these are all the Two D Shapes and the start of ThreeDShapes....

public abstract class MyShapes

public abstract double getArea(); //this will be called on later by two D and three D shapes.
//all other subclasses will call this. all will use getArea.
public String toString(){
return String.format("Area");
}
}
/////////////////////
public abstract class TwoDShapes extends MyShapes
{
public abstract double getArea(); //this gets areas for the two D shapes

}
/////////////////////
public abstract class ThreeDshapes extends MyShapes
{
public abstract double getArea();

public abstract double getVolume();

}
//////////////
public class Circle extends TwoDShapes
{

{

}

}

}

public double getArea(){
}

@Override
public String toString(){

}
}// ends class Circle

public class Square extends TwoDShapes
{
private double side;

public Square(double side){

if(side <= 0.0)
throw new IllegalArgumentException("A side must have length greater then 0");

this.side = side;

}

public void setSide(double side){
if(side <= 0.0)
throw new IllegalArgumentException("A side must have length greater then 0");
this.side = side;
}

public double getSide(){
return side;
}

public double getArea(){
return getSide() * getSide();
}

@Override
public String toString(){

return String.format("%s = %.2f%n%17s = %.2f", "Side", getSide(), super.toString(), getArea());
}

}
///////////////////
public class Triangle extends TwoDShapes
{
private double base;
private double height;

public Triangle(double base, double height){

if(base <= 0.0)
throw new IllegalArgumentException("Base length > 0");
if(height <= 0.0)
throw new IllegalArgumentException("Hieght length > 0");

this.base = base;

this.height = height;
}

public void setBase(double base){
if(base <= 0.0)
throw new IllegalArgumentException("Base length > 0");
this.base = base;
}

public double getBase(){
return base;
}

public void setHieght(double hieght){
if(hieght <= 0.0)
throw new IllegalArgumentException("Hieght length > 0");
this.height = hieght;
}

public double getHieght(){
return height;
}

public double getArea(){
return (getBase()*getHieght()/2);
}

@Override
public String toString(){
return String.format("%s = %.2f%n%19s = %.2f%n%17s = %.2f", "Base", getBase(), "Hieght", getHieght(),super.toString(), getArea());
}

}
``````

Wiggles: you do understand that this is a 6 year old thread, right?

When showing off code, make it good code.

``````public double getSide(){
return side;
}
public double getArea(){
return getSide() * getSide();
}
``````

Can you come up with any reason that the above, as you have it, is better than:

``````public double getSide(){
return side;
}
public double getArea(){
return side * side;
}
``````

In defense of that particular point...
`side` is private. Where I come from it's considered good practice to use the public accessors for private variables, even in the same class. Consider what happens if someone later changes the internal representation or implementation of that information. As long as they update the accessor all will be OK - unless they have missed a use of that info somewhere else in the class that didn't use the accessor.
Having said that, I'm a fan of making classes like these immutable, with `side` as a public final variable, and forget the accessors; but thats a different argument.

Going back to Wiggle's code, which is mostly OK:

Shape declares the getArea() method - but Shapes can be 3D, so what is the meaning of "area" for an arbitrary 3D shape? Maybe this method should only apply to 2D shapes.

TwoDShapes extends MyShapes, so there's no point repeating the declaration of getArea() to be the same as the one you inherit.

Line 10: toString() should always return a description of the object - type/class and significant instance data. This implementation is worse than nothing because it provides incorrect information.

Not showing off any code. This problem still comes up in class. I was simply putting up a solution for those who encounter it. I'm no programer and I can understand when people have difficulties with prolems. I was just putting up a solution is all. I'm currently take a programming class and needed help. It was hard to find so when I figured it out I posted it. If you want to make it better go right ahead. I'm no programmer.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.