Exercise 1: Analyse, compile and fix bugs in the following program class Circle
ID: 3826788 • Letter: E
Question
Exercise 1: Analyse, compile and fix bugs in the following program
class Circle {
private double r;
public void Circle( double r )
{
r = r;
}
private double calCircumference()
{
return 2*Math.PI*r;
}
public static double calArea()
{
return Math.PI*r*r;
}
}
class CircleApp
{
public static void main( String[] args )
{
double rd = Double.parseDouble( args[0] );
System.out.println( "Circle radius = " + rd );
// create an object of Circle with the radius rd
Circle circle1 = new Circle( rd );
double cir = circle1.calCircumference();
double area = circle1.calArea();
System.out.println("Circumference = " + cir);
System.out.println("Area = " + area);
}
}
Explanation / Answer
Circle.java
class Circle {
private double r;
public Circle(double r) {
this.r = r;
}
public double calCircumference() {
return 2 * Math.PI * r;
}
public double calArea() {
return Math.PI * r * r;
}
}
___________________
CircleApp.java
import java.util.Scanner;
public class CircleApp {
public static void main(String[] args) {
//Declaring variable
double radius;
//Scanner object is used to get the inputs entered by the user
Scanner sc=new Scanner(System.in);
//Getting the value of the radius entered by the user
System.out.print("Enter Radius of the Circle :");
radius=sc.nextDouble();
// create an object of Circle with the radius rd
Circle circle1 = new Circle(radius);
double cir = circle1.calCircumference();
double area = circle1.calArea();
System.out.printf("Circumference = %.2f " , cir);
System.out.printf("Area = %.2f", area);
}
}
_______________________
Output:
Enter Radius of the Circle :4.5
Circumference = 28.27
Area = 63.62
__________Thank You
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.