Create a class called circle, this class contains: --Two private instance variab
ID: 3677884 • Letter: C
Question
Create a class called circle, this class contains:
--Two private instance variables: radius (date type double) and color (date type String);
--Two overloaded constructors; the first constructor doesn't have parameters , it sets both radius and color to default ( radius: 10, color: red); The second constructor has two parameters, these two parameters can initialize radius and color as specified.
---Two public methods: getColor():A public method for retrieving the color and getArea():A public method for computing the area of circle.
The class should contain a main() method which tests two overloaded constructors , getRadius() and getArea() methods.
Explanation / Answer
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package practice;
/**
*
* @author Shivakumar
*/
public class Circle {
private double radius;
private String color;
public Circle() // Default constructor
{
radius=10;
color="red";
}
public Circle(double radius,String color) // perameterized constructor
{
this.radius=radius; // assigning values to class variables
this.color=color;
}
public double getRadius()
{
return radius; // getting radius
}
public String getColor()
{
return color; // getting color
}
public double getArea()
{
return Math.PI*Math.pow(radius, 2); // computing and returning area using Math class
}
public static void main(String args[])
{
System.out.println("Calling default constructor");
Circle c=new Circle(); // calling default constructor
System.out.println("The default radius is"+c.getRadius());
System.out.println("The default color is"+c.getColor());
System.out.println("The default area is"+c.getArea());
System.out.println("Calling parameterized constructor"); // perameterized constructor
Circle c1=new Circle(5,"blue");
System.out.println("The radius is"+c1.getRadius());
System.out.println("The color is"+c1.getColor());
System.out.println("The area is"+c1.getArea());
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.