user2913004
user2913004

Reputation: 123

How to use circle.java to calculate getVolume of a cylinder. [2 classes]

edit2

Sorry never mind I just added

 public double cylinderSurfaceArea() {
      return 2 * base.circleArea() + base.circleCirumference() * 2 * height;
    }
}

With no error codes. This would be correct?

edit:


Thank you to all those who have answered. I have since changed my previous Cylinder class to read. Now I want to take it a step further and add

  public double cylinderSurfaceArea() {
      return 2 * Math.PI * radius * radius +  2 * Math.PI * radius * h;
    }

However it now says that radius (or even r) returns an error "cannot find symbol - variable radius). Shouldn't the symbol be found/declared from the Circle class?


What I am trying to do is calculate the volume of a cylinder using a separate Circle.java class.

So for instance, I have the following so far for my circle.java

public class Circle {
  public double radius;

  public Circle(double r) {
  radius = r;
  }

  public double circleArea() {
      return Math.PI * radius * radius;
  }

  public double circleCirumference() {
      return Math.PI * 2 * radius;
    }
}

Now here are where the questions start. When making the Cylinder class do I start with:

public class Cylinder extends Circle {

If so, overall I have:

public class Cylinder extends Circle {
  public Circle base;
  public double height;

  public Cylinder(double r, double h) {
    height = h;
    base = new Circle(r);
      }

  public double getVolume() {
    return base.circleArea * height;
  }
}

However, I keep getting an error after:

public Cylinder(double r, double h) {

Stating that:

constructor Circle in class Circle cannot be applied to given types; required:double; found: noarguments; reason:actual and formal arguments lists differ in length."

Can someone push me in the right direction? What am I doing wrong?

Upvotes: 1

Views: 6926

Answers (4)

Prabhakaran Ramaswamy
Prabhakaran Ramaswamy

Reputation: 26094

Problem 1:

The problem in your program is no default constructor present in your Circle. While creating the Cylinder object its looking for the default constructor in Circle.

if you modify your Circle as below it will work

 class Circle {     
      public Circle(){

      } 
 }

problem 2

There is "base.circleArea" method only present in Circle, you have forgot "()"

base.circleArea need to change to base.circleArea().

public class Cylinder extends Circle {

  public double getVolume() {
    return base.circleArea() * height;
  }
}

Problem 3

Your Cylinder should be like below. You are already extended circle so no need to create variable Circle base inside Cylinder.

 class Cylinder extends Circle {

      public double height;

      public Cylinder(double r, double h) {
          super(r);
        height = h;
      }

      public double getVolume() {
        return circleArea * height;
      }
 }

Upvotes: 0

Eel Lee
Eel Lee

Reputation: 3543

That happens because the first call of you constructor is implicit super()

Quote from the Java Tutorials:

If a constructor does not explicitly invoke a superclass constructor, the Java compiler automatically inserts a call to the no-argument constructor of the superclass. If the super class does not have a no-argument constructor, you will get a compile-time error.

You need to make a parameterless constructor in your Circle class or change your Cylinder constructor like this:

public Cylinder(double r, double h) {
    super(r);
    height = h;
}

Upvotes: 2

millimoose
millimoose

Reputation: 39970

You don't need an explicit base field in Java when using inheritance. To initialise the base class (or "superclass"), you need to use the super statement in your child class constructor:

class Circle {
    public Circle(double radius) {
        // …
    }
}

class Cylinder extends Circle {
    public Cylinder(double radius, double height) {
        super(radius); // calls the parent class constructor
        // … 
    }
}

Alternately, you could use composition instead of inheritance - probably a better design in this case:

class Circle {
    public Circle(double radius) { /* … */ }
}

class Cylinder { // no `extends` here
    public Cylinder(Circle base, double height) {
        // …
    }

    public Cylinder(double radius, double height) {
        this(new Circle(radius)); // calls the above constructor
        // …
    }
}

(I'm omitting trivial assignments and fields in the above code sample for brevity.)

Upvotes: 1

Denys Séguret
Denys Séguret

Reputation: 382264

You're implicitly calling the super constructor with no argument, but there's no such constructor.

But you have a design problem : You're trying to use both composition and inheritance. One would be enough.

Using inheritance :

public class Cylinder extends Circle {
  public double height;

  public Cylinder(double r, double h) {
    super(r);
    height = h;
  }

  public double getVolume() {
    return circleArea() * height;
  }
}

Using composition (almost always better) :

public class Cylinder {
  public Circle base;
  public double height;

  public Cylinder(double r, double h) {
    height = h;
    base = new Circle(r);
  }

  public double getVolume() {
    return base.circleArea * height;
  }
}

Upvotes: 1

Related Questions