CYLINDER CHALLENGE

 1 Write a class with the name Circle.. the class needs one field (instance variable) with name radius of type double.
  The class needs to have one constructor with a 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 method):
  • the method named getRadius without any parameters, it needs to return the value of the radius field.
  •  ''        ''          ''       getArea          ''        ''           ''         , ''     ''      ''     ''       ''    calculated area (radius*radius*PI) for PI use Math.PI constant.  
2 Write the name Cylinder that extends Circle class. The class needs one field (instance variable) with the name height of type double.
The class needs to have one constructor with two parameters radius and height of both of type double. It needs to call the parent constructor and initialize the height field.
In case the height parameter is less than 0 it needs to set the height field to 0.
Write the following methods:
  • Method named getHeight without any parameters, it needs to return the value of the height field.
  •   ''                ''    getVolume      ''       ''          ''          , ''    ''       ''     ''        ''      ''     ''    ''        ''     area.
Soln :
package com.cnc;

public class Circle {
private double radius;

public Circle(double radius) {
this.radius = radius < 0 ? 0:radius;
}

public double getRadius() {
return this.radius;
}

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

package com.cnc;

public class Cylinder extends Circle {
private double height;

public Cylinder(double radius, double height) {
super(radius);
this.height = height < 0 ? 0:height;
}

public double getHeight() {
return this.height;
}

public double getVolume() {
return this.height * getArea();
}
}


Popular posts from this blog

NUMBER PALINDROME CHALLENGE

ENCAPSULATION CHALLENGE

LARGEST PRIME FACTOR OF A NUMBER