I do not know if I am on the right track or not, but I have hit a dead end. I have read and reread my books chapters about super classes and inheritance and am still completely lost. in this assignment I have to: Write a super class encapsulating a circle; this class has one attribute representing the radius of the circle. it has methods returning the perimeter and the area of the circle. This class has a subclass encapsulating a cylinder. A cylinder has a circle as its base and another attribute, its length; it has two methods, calculating and returning its area and volumes.

I am wondering if any one could help make sure I am on the right track and give me some advice on what I need to do to finish this as I am completely lost. I am a complete newbie with programimg and I have been working on this for 1 and 1/2 weeks and this is all i have. I also have no idea what needs to go in the Figure class. Thank You

first class

public class Question48 
{
public static void main(String[] args) 
{
    Circle circle = new Circle();
    System.out.println(circle.GetArea());
} 
}

Second

public abstract class Figure 
{
    //Varibles 

    //constructor 
    public Figure()
    {

    }     
}

Third

public class Circle extends Figure
{
//Varibles
double area;
double radius;
double perimeter;

public double setArea()
{
    return area;
}

//accessors
public double GetArea() 
{
    return area;
}

public double GetPerimeter() 
{
    return perimeter;
}

public double getRadius()
{
    return radius;
}   

public Circle()
{

} 
}

Fourth

public class Cylinder extends Circle 
{
double length;

public double GetVolume() 
{
    return length * GetArea();
}
}

your Circle class should have only one attribute, the radius, so remove the doubles area and perimeter. you'll need to calculate those from the radius.

your Figure class ... well, it makes no sense. just remove it from your project.

public class Cylinder extends Circle

this looks good to me, but you still haven't got your classes that encapsulates Circle and Cylinder.

so, for instance:

public class SuperClass{

  private Circle myCircle;
  // add setter and getter

  }

and about the same for it's subclass.

now, I'm not sure what this assignment is all about, I assume it's just a way for you to show you've got the basics, so then you're about set.

I would start with your super classes and move on from there. Build from the ground up. So first ask yourself what you expect from that class and move forward like that. CDCs are great and help you set a clear path in your mind.

As stultuske mentioned the figure class is pretty pointless, for this question specifically. Suppose you intend on building that figure class to implement other shapes then it would be useful, but your question doesn't really embark on that.

Though if you intend on sticking with it, I would start with, if not...this should help spark an idea.

abstract class Figure { 

double 
    x,
    y;
    //radius and length

    Figure(double r){ //Circle constructor
    x = r;
    }//Figure constructor d

    //Figure(double x, double y){ //Rectangle Constructor...
    //insert code
    //}//Figure constructor d d

    abstract double area(); //abstract area method

    }//class

lamirp: he shouldn't "change" his Figure class, he should remove it.
it's not mentioned in his assignment, and it's no use making it take the place of Circle.

also: if you meant that class to be the Circle, you're doing it wrong. there is only one attribute expected: radius, you have two, x and y. also, not really the most meaningful names.

If this simple exercise is causing problems its probably because the idiot who designed it doesn't understand inheritance. Inheritance is a is-a-kind-of relationship, and a cylinder is NOT a kind of circle. What happens if I add some perfectly sensible methods to Circle like boolean containsPoint(int x, int y) ? That makes no sense for a cylinder because a cylinder is three dimensional.
class Cylinder extends Circle is the same mistake as class Car extends Gearbox
The correct model is has-a, not is-a, composition not inheritance. A cylinder consists of a circle that's extended into 3d along a line

class Cylinder {
   Circle xSection; 
   int length;
   ...

BUT - you have to do what the teacher asks, so just follow the instructions without thinking too hard about it.

I agreed that he should remove it, I was using it as an example of a super without writing his code for him.

May his teacher push a follow up assignment and now wants a super that can handle different shapes, that would be useful, although that is what I had said earlier.

i agree with james , but then again , school/college teachers rarely follow the best practises...

keep a circle class , it will have ONE variable only : radius.
the methods will be setRadius() , getArea() , and if you need it, keep a getRadius() too. all these methods should be public. and all your variables should be private, thats the whole point with using public getters n setters.

figure{} class doesnt make any sense.

Somjit: at this point you didn't add any new information to the table, you just summarized what's been said yesterday.

I thought for a moment that you were going to add some valuable information about encapsulation of which the OP could benefit, but you ended it with "thats the whole point with using public getters n setters", without actually explaining 'what' that point is.

might be an idea for a next post? it's pretty easy to write a simple example, but just saying that there is a point, won't help a novice understand what that point is.

@stultuske : yes , i should have elaborated that part more.

@bobit : although ur using getters and setters , all the instance variables of your code are open and exposed. you should change them to private. like , instead of double radius , it should be private double radius

reason? well , When you write your code , if all the variables are exposed , they can get accessed and changed from anywhere in the code during run time, and you might find that ur output is quite different from one you hoped for.. the very reason we use public getters and setters is such that the instance variables can stay private, protected from any outside influence , and can only be modified by calling the respective getters and setters for that particular variable. this whole "protect your instance variable " thingy is the famous java concept of encapsulation.

apart from making your code safer through encapsulation, getters and setters also lets you put in checks , like

public void setRadius(int r){
    if( radius < 0 ) System.out.println("negative values not allowed");
    else if(r > 10) radius = r; 
    else radius = 5; // provide a default value of 5 to radius
}

so here, not only is radius not allowed to be zero , and is changed to default value of 5 if a value less than 10 is passed , you also get a warning if negative values are passed to it. you might not need all this much code for the program you posted above, but these are good java practises and as you go into writing more complex code , such checks will become more important.

all this encapsulation stuff is done in java by using whats known as access modifiers : public , private , protected and default or no modifier. you can read more about access modifiers here

commented: there you go :) +14

example could use improvements, but the explanation speaks for itself.

Be a part of the DaniWeb community

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