public class Circle
{
private double radius;
//Default constructor
//Sets the radius to 0
Circle()
{
radius = 0;
}
//Constructor with a parameter
//Sets the radius to the value specified by the parameter r.
Circle(double r)
{
radius = r;
}
//Method to set the radius of the circle.
//Sets the radius to the value specified by the parameter r.
public void setRadius(double r)
{
radius = r;
}
//Method to return the radius of the circle.
//Returns the radius of the circle.
public double getRadius()
{
return radius;
}
//Method to compute and return the area of the circle.
//Computes and returns the area of the circle.
public double area()
{
return Math.PI * Math.PI * radius;
}
//Method to compute and return the perimeter of the circle.
//Computes and returns the area of the circle.
public double perimeter()
{
return 2 * Math.PI * radius;
}
//Method to return the radius, area, perimeter of the circle
//as a string.
public String toString()
{
return String.format("Radius = %.2f, Perimeter = %.2f"+ ", Area = %.2f%n", radius, perimeter(), area());
}
}
We leave the UML class diagram of the class Circle as an exercise for you.
The following program shows how to use the class Circle in a program.
// Program to test various operations of the class Circle.
import java.util.*; //Line 1
public class TestProgCircle //Line 2
{ //Line 3
static Scanner console = new Scanner(System.in); //Line 4
public static void main(String[] args) //Line 5
{ //Line 6
Circle firstCircle = new Circle(); //Line 7
Circle secondCircle = new Circle(12); //Line 8
double radius; //Line 9
System.out.println("Line 10: firstCircle: " + firstCircle); //Line 10
System.out.println("Line 11: secondCircle: " + secondCircle); //Line 11
System.out.print("Line 12: Enter the radius: "); //Line 12
radius = console.nextDouble(); //Line 13
System.out.println(); //Line 14
firstCircle.setRadius(radius); //Line 15
System.out.println("Line 16: firstCircle: " + firstCircle ); //Line 16
if (firstCircle.getRadius() > secondCircle.getRadius()) //Line 17
System.out.println("Line 18: The radius of "+ "the first circle is greater than "+ "the radius of the second circle. "); //Line 18
else if (firstCircle.getRadius()< secondCircle.getRadius()) //Line 19
System.out.println("Line 20: The radius of "+ "the first circle is less than the " + "radius of the second circle. "); //Line 20
else //Line 21
System.out.println("Line 22: The radius of " + "both the circles are the same."); //Line 22
}//end main //Line 23
} //Line 24
Sample Sample Output:
Line 10: firstCircle: Radius = 0.00, Perimeter = 0.00, Area = 0.00
Line 11: secondCircle: Radius = 12.00, Perimeter = 75.40, Area = 118.44
Line 12: Enter the radius: 10
Line 16: firstCircle: Radius = 10.00, Perimeter = 62.83, Area = 98.70
Line 20: The radius of the first circle is less than the radius of the second circle.
The preceding program works as follows. The statement in Line 7 creates the object
firstCircle and using the default constructor sets the radius to 0. The statement in
Line 8 creates the object secondCircle and sets the radius to 12. The statement in Line
9 declares the double variable radius. The statement in Line 10 outputs the radius, area,
and perimeter of the firstCircle. Similarly, the statement in Line 11 outputs the
radius, area, and perimeter of the secondCircle The statement in Line 12 prompts the
user to enter the value of radius. The statement in Line 13 stores the value entered by
the user in the variable radius. The statement in Line 15 uses the value of radius to set
the radius of firstCircle. The statement in Line 16 outputs the radius, area, and
perimeter of the firstCircle. The statements in Lines 17 to 23 compare the radius of
firstCircle and secondCircle and output the appropriate result.
No comments:
Post a Comment