暑课有道Java的题做不来 望各路大神求助
Write a class Circle with the object method:Double area():the method computes the area of the circle and returns the result.
Double diameter(double area): the method gets the circle’s area assigned,computes the diameter of the circle and returns the result.
Implement the main method into the class and test your application with an example public class Circle {
public static final double PI = 3.14;
double radius;
Circle(double radius){
this.radius = radius;
}
Circle(){
this.radius = 0;
}
double area() {
return PI*radius*radius;
}
public static double diameter(double area) {
return Math.sqrt(area/PI)*2;
}
public static void main(String[] args) {
Circle c1 = new Circle(10);
System.out.println("圆c1半径为 : "+c1.radius);
System.out.println("圆c1面积为 : "+c1.area());
Circle c2 = new Circle();
System.out.println("圆c2半径为 : "+c2.radius);
System.out.println("圆c2面积为 : "+c2.area());
System.out.println("面积为78.5的圆的直径为 : "+Circle.diameter(78.5));
}
} 楼上可以的{:10_256:} public class Test {
public static void main(String[] args) {
Circle c=new Circle(1);
double area=c.area();
double diameter =c.diameter(area);
System.out.println(area);
System.out.println(diameter);
}
}
class Circle{
final double p=3.14;
private double radius;
private double area;
private double diameter;
public Circle(double radius) {
this.radius=radius;
}
public double area() {
area=p*radius*radius;
return area;
}
public double diameter(double s) {
diameter=2*Math.sqrt(p*s);
return diameter;
}
}
页:
[1]