posted 12 years ago

ok im supposed to design an abstract class called Shape. It's methods are area and circumference. Then i have 3 child classes called circle, rectangle, and square....their methods are how they specifically calculate area and circumference...then i have a main driver class called ShapeAnalyzer that exercises these behaviors...however i don't think i wrote my classes correctly....can someone tell me what i did wrong..my rectangle class is the only one thats producing the correct output..here is my code's....

public abstract class Shapes ///this is my abstract class

{

protected double area, circumference;

public double getArea()

{

return area;

}

public double getCircumference()

{

return circumference;

}

}

public class Circle extends Shapes //this is my circle child class

{

protected double radius;

public Circle(int radius)

{

}

public double getArea()

{

area = Math.PI*Math.pow(radius,2);

return area;

}

public double getCircumference()

{

circumference = 2*Math.PI*radius;

return circumference;

}

}

public class Rectangle extends Shapes //this is the only producing correct output

{

protected double w,h;

public Rectangle(double w,double h)

{

this.w=w;

this.h=h;

}

public double getWidth()

{

return w;

}

public double getHeight()

{

return h;

}

public double getArea()

{

area = w*h;

return area;

}

public double getCircumference()

{

circumference = 2*(w+h);

return circumference;

}

}

public class Square extends Shapes //this is the square child class

{

protected int s;

public Square(int s)

{

}

public double getArea()

{

return s*s;

}

public double getCircumference()

{

return 4*s;

}

public double getSide()

{

return s;

}

}

public class ShapeAnalyzer //main driver class to exercise the behaviors

{

public static void main(String[] args)

{

Circle myCircle = new Circle(4);

Square mySquare = new Square(6);

Rectangle myRectangle = new Rectangle(10, 5);

System.out.println("The Circle's area is: " + myCircle.getArea());

System.out.println("The Circles circumference is: " + myCircle.getCircumference());

System.out.println("The Square's area is: " + mySquare.getArea());

System.out.println("The Square's circumference is: " + mySquare.getCircumference());

System.out.println("The Rectangle's area is: " + myRectangle.getArea());

System.out.println("The Rectangle's circumference is: " + myRectangle.getCircumference());

}

}

public abstract class Shapes ///this is my abstract class

{

protected double area, circumference;

public double getArea()

{

return area;

}

public double getCircumference()

{

return circumference;

}

}

public class Circle extends Shapes //this is my circle child class

{

protected double radius;

public Circle(int radius)

{

}

public double getArea()

{

area = Math.PI*Math.pow(radius,2);

return area;

}

public double getCircumference()

{

circumference = 2*Math.PI*radius;

return circumference;

}

}

public class Rectangle extends Shapes //this is the only producing correct output

{

protected double w,h;

public Rectangle(double w,double h)

{

this.w=w;

this.h=h;

}

public double getWidth()

{

return w;

}

public double getHeight()

{

return h;

}

public double getArea()

{

area = w*h;

return area;

}

public double getCircumference()

{

circumference = 2*(w+h);

return circumference;

}

}

public class Square extends Shapes //this is the square child class

{

protected int s;

public Square(int s)

{

}

public double getArea()

{

return s*s;

}

public double getCircumference()

{

return 4*s;

}

public double getSide()

{

return s;

}

}

public class ShapeAnalyzer //main driver class to exercise the behaviors

{

public static void main(String[] args)

{

Circle myCircle = new Circle(4);

Square mySquare = new Square(6);

Rectangle myRectangle = new Rectangle(10, 5);

System.out.println("The Circle's area is: " + myCircle.getArea());

System.out.println("The Circles circumference is: " + myCircle.getCircumference());

System.out.println("The Square's area is: " + mySquare.getArea());

System.out.println("The Square's circumference is: " + mySquare.getCircumference());

System.out.println("The Rectangle's area is: " + myRectangle.getArea());

System.out.println("The Rectangle's circumference is: " + myRectangle.getCircumference());

}

}

posted 12 years ago

Yup, compare and contrast the constructors:

Also, at least for "version one" I would try to keep code as simple as possible. You are caching the values for the circumference and area in your base class, but not consistently:

You can simplify this be getting rid of variables area and circumference. Then Shapes could even be an interface, because it has no code or data.

Also, at least for "version one" I would try to keep code as simple as possible. You are caching the values for the circumference and area in your base class, but not consistently:

You can simplify this be getting rid of variables area and circumference. Then Shapes could even be an interface, because it has no code or data.

There is no emoticon for what I am feeling!