It's not a secret anymore!*
The moose likes Beginning Java and the fly likes cant figure out whats wrong with my absract class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "cant figure out whats wrong with my absract class" Watch "cant figure out whats wrong with my absract class" New topic
Author

cant figure out whats wrong with my absract class

alaina peeler
Greenhorn

Joined: Dec 04, 2005
Posts: 29
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());
}
}
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18914
    
  40

With a quick glance, it looks like the rectangle is the only class with an implemented constructor.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Jeff Albertson
Ranch Hand

Joined: Sep 16, 2005
Posts: 1780
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.


There is no emoticon for what I am feeling!
 
jQuery in Action, 2nd edition
 
subject: cant figure out whats wrong with my absract class