For this module you are exploring the infant stages of object-oriented programmi
ID: 3683076 • Letter: F
Question
For this module you are exploring the infant stages of object-oriented programming by create abstract data types. For this assignment, you are to create a class called Circle. Circle should have a double attribute called radius. The class should have appropriate getters and setters, a no-arg and a parameterized constructor. Also include the following member method: getArea(), getCircumference() and getDiameter(). Write a demo program that creates an object of the Circle class you created and demonstrates uses of the methods defined.
You program should declare a package named circle.
Explanation / Answer
//Program
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package circle;
/**
*
* @author RAM
*/
abstract class Circle{
private double radius;
//setter and getters
public double getRadius() {
return radius;
}
public void setRadius(double radius) {
this.radius = radius;
}
public abstract double getArea();
public abstract double getDiameter();
public abstract double getCircumference();
}
class CircleDemo extends Circle{
public double calradius;
//default constrcutor
public CircleDemo(){
System.out.println("Default Constructor");
}
//parameterized constructor
public CircleDemo(double radius){
calradius = radius;
}
@Override
public double getArea() {
double area = Math.PI *calradius * calradius;
return area;
}
@Override
public double getDiameter() {
double diameter = 2 * calradius;
return diameter;
}
@Override
public double getCircumference() {
double circumference= Math.PI * 2*calradius;
return circumference;
}
}
class circleCalcualte {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
CircleDemo cd = new CircleDemo();
//set the radius of circle
cd.setRadius(10.5);
//get the radius of circle
double rad;
rad = cd.getRadius();
System.out.println(rad);
//call the function
double area = cd.getArea();
// area of the circle
System.out.println(area);
//getCircumference of circle
double circumfer = cd.getCircumference();
System.out.println(circumfer);
//get diameter
double diameter = cd.getDiameter();
System.out.println(diameter);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.