Reputation: 9
The class needs to have one constructor with parameter radius of type double and it needs to initialize the fields.
In case the radius parameter is less than 0 it needs to set the radius field value to 0.
Write the following methods (instance methods):
The class needs to have one constructor with two parameters radius and height both of type double. It needs to call parent constructor and initialize a height field.
In case the height parameter is less than 0 it needs to set the height field value to 0.
Write the following methods (instance methods):
TEST EXAMPLE
→ TEST CODE:
Circle circle = new Circle(3.75);
System.out.println("circle.radius= " + circle.getRadius());
System.out.println("circle.area= " + circle.getArea());
Cylinder cylinder = new Cylinder(5.55, 7.25);
System.out.println("cylinder.radius= " + cylinder.getRadius());
System.out.println("cylinder.height= " + cylinder.getHeight());
System.out.println("cylinder.area= " + cylinder.getArea());
System.out.println("cylinder.volume= " + cylinder.getVolume());
→ OUTPUT
circle.radius= 3.75
circle.area= 44.178646691106465
cylinder.radius= 5.55
cylinder.height= 7.25
cylinder.area= 96.76890771219959
cylinder.volume= 701.574580913447
NOTE: All methods should be defined as public NOT public static.
NOTE: In total, you have to write 2 classes.
NOTE: Do not add a main method to the solution code.
this is my Circle class
public class Circle {
private double radius;
public Circle(double radius) {
this.radius = radius;
}
public double getRadius() {
if(radius < 0){
radius = 0;
return radius;
}else{
return radius;
}
}
public double getArea(){
double area = (radius * radius * Math.PI);
return area;
}
}
this is my Cylinder Class
public class Cylinder extends Circle{
private double height;
public Cylinder(double radius, double height) {
super(radius);
this.height = height;
}
public double getHeight() {
if(height > 0){
return height;
}else{
height = 0;
return height;
}
}
public double getVolume(){
double area = getArea() * getHeight();
return area;
}
}
But the question said that constructor object in class object cannot be applied to given types and that super(radius) required no arguments. and I also get an error at the double area = getArea() * getHeight(); that said cannot find symbol. can anyone help me with this problem ?
Upvotes: 0
Views: 1289
Reputation: 11
private double radius;
public Circle(double radius) {
if (radius < 0) {
this.radius = 0;
} else {
this.radius = radius;
}
}
public double getRadius() {
return radius;
}
public double getArea() {
return this.radius * this.radius * Math.PI;
}
The Cylinder class: public class Cylinder extends Circle { private double height;
public Cylinder(double radius, double height) {
super(radius);
if (height < 0) {
this.height = 0;
} else {
this.height = height;
}
}
public double getHeight() {
return height;
}
public double getVolume() {
return getArea() * this.height;
}
}
!!!But it's important to put Cylinder EXTENDS Circle on Udemy course, for right compile...
Upvotes: 1