14.0k views
0 votes
Complete the constructors and the area method of the Circle class.

public class Circle
{

private double radius;

// constructors
// postcondition: the instance variable is initialized
public Circle(double rad)
{

}

// postcondition: the instance variable is initialized
public Circle(int diameter)

{

}

// postcondition: returns the area of this circle, according to the
// formula: area = PI * r^2, where PI is the value of pi (3.1415…),
// r is the radius of the circle, and "^2" means raised to the second power.
// Use the Math class constant to represent the value of pi.
public double area()
{

}

// There may be other instance variables, constructors,
// and methods that are not shown.
}

Please help

Complete the constructors and the area method of the Circle class. public class Circle-example-1

1 Answer

3 votes

Answer (sorry for loss of some indentation):

public class Circle {

// instance variable to store the radius of the circle

private double radius;

// constructors

// creates a circle with the specified radius

public Circle(double rad) {

radius = rad;

}

// creates a circle with the specified diameter

public Circle(int diameter) {

radius = diameter / 2.0;

}

// calculates and returns the area of the circle

// using the formula: area = PI * r^2

// where PI is the value of pi (3.1415…),

// r is the radius of the circle

public double area() {

return Math.PI * Math.pow(radius, 2);

}

// There may be other instance variables, constructors,

// and methods that are not shown.

}

User Chris Warnes
by
8.4k points