java
Create a program that defines a class called circle. Circle should have a member
variable called radius that is used to store the radius of the circle. Circle should also
have a member method called calcArea that calculates the area of the circle using
the formula area = pi*r^2. Area should NOT be stored in a member variable of
circle to avoid stale data.
Use the value 3.14 for PI.
For now, make radius public and access it directly using the dot operator. Do not
worry about input validation for now. In main instantiate two objects of class circle
and have the user enter a radius for each. Store the radii in the appropriate objects'
member variables, then print the radius and area of each circle using the member
variable and function.
public class circle
{
public double radius = 0;
void printArea()
{
System.out.println(radius * radius * 3.14159);
}
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Modify what is above so that the radius variable in class circle is private. Write a setter
and getter method for radius, as well as a default constructor that initializes radius
to 0 and a constructor that takes one parameter for radius. If radius is given as a
negative in a call to the setter or the constructor set it to 0 instead.
In main, instantiate (declare/create) and use circle objects as needed to prove that
all of your functions, including both constructors, work properly (construct a test
case for your class in main).
Dear Student ,
As per requirement submitted above kindly find below solution.
Demonstration 1:
circle.java :
//Java class
public class circle {
//member variable
public double radius = 0;
//method to calculate area
public void calcArea()
{
//print area of circle
System.out.println("Area of circle
with radius "+this.radius+" is "+(this.radius * this.radius *
3.14159));
}
}
****************************
CircleTest.java :
//import package
import java.util.*;
//Java class
public class CircleTest {
//entry point of the program , main() method
public static void main(String[] args) {
//creating object of Scanner
class
Scanner sc=new
Scanner(System.in);
//asking user radius
System.out.print("Enter Radius 1:
");
double
radius1=sc.nextDouble();//reading radius
//asking user radius
System.out.print("Enter Radius 2:
");
double
radius2=sc.nextDouble();//reading radius
//creating object of Circle
class
circle c1=new circle();
c1.radius=radius1;//set
radius
//call method to print area
c1.calcArea();
//creating object of Circle
class
circle c2=new circle();
c2.radius=radius2;//set
radius
//call method to print area
c2.calcArea();
}
}
==================================
Output :Compile and Run CircleTest.java to get the screen as shown below
Screen 1:
******************************
Demonstration 2:
circle.java :
//Java class
public class circle {
//member variable
private double radius;
//default constructor
public circle()
{
this.radius=0;//set radius to
0
}
//constructor with one parameter
public circle(double r)
{
setRadius(r);//calling setter
method
}
//getter method
public void setRadius(double r)
{
//checking value of radius
if(r<0)
{
//if radius is
negative then
this.radius=0;//set radius to 0
}
else {
this.radius=r;
}
}
//getter method
public double getRadius()
{
return this.radius;//return
radius
}
//method to calculate area
public void calcArea()
{
//print area of circle
System.out.println("Area of circle
with radius "+getRadius()+" is "+(getRadius() * getRadius() *
3.14159));
}
}
*****************************
CircleTest.java :
//import package
import java.util.*;
//Java class
public class CircleTest {
//entry point of the program , main() method
public static void main(String[] args) {
//creating object of Scanner
class
Scanner sc=new
Scanner(System.in);
//creating object of Circle
class
circle c1=new circle();
//asking user radius
System.out.print("Enter Radius 1:
");
c1.setRadius(sc.nextDouble());//reading radius
//asking user radius
System.out.print("Enter Radius 2:
");
double
radius=sc.nextDouble();//reading radius
//creating object of Circle
class
circle c2=new
circle(radius);
//call method to print area
c1.calcArea();
//call method to print area
c2.calcArea();
}
}
==================================
Screen 1:
Screen 2:When radius is negative
NOTE :PLEASE FEEL FREE TO PROVIDE FEEDBACK ABOUT THE SOLUTION.
Get Answers For Free
Most questions answered within 1 hours.