import java.util.Scanner;
public class TestCircle {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
/*Circle c1 = new Circle();
System.out.print("第一个圆已经生成, 它的半径是:");
System.out.println(c1.getRadius() + "cm");
System.out.print("它的面积是:");
System.out.printf("%.2f平方厘米\n", c1.getArea());
System.out.print("它的颜色是默认的");
System.out.println(c1.getColor());
System.out.print("\n输入第二个圆的半径和颜色: ");
Circle c2 = new Circle(sc.nextDouble(), sc.nextLine());
System.out.print("第二个圆已经生成, 它的半径是:");
System.out.println(c2.getRadius() + "cm");
System.out.print("它的面积是:");
System.out.printf("%.2f平方厘米\n", c2.getArea());
System.out.print("它的颜色是");
System.out.println(c2.getColor());
System.out.print("\n修改第二个圆的颜色为: ");
c2.setColor(sc.nextLine());
System.out.print("修改后它的颜色是");
System.out.println(c2.getColor());
*/
System.out.print("\n输入第三个圆的半径: ");
Circle c3 = new Circle(sc.nextDouble());
System.out.println(c3.toString());
sc.close();
}
}
class Circle {
private double radius;
private String color;
public Circle() {
radius = 1;
color = "红色";
}
public Circle(double radius) {
this.radius = radius;
this.color = "白色";
}
public Circle(double radius, String color) {
this.radius = radius;
this.color = color;
}
public double getRadius() {
return radius;
}
public void setRadius(double radius) {
this.radius = radius;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public String toString(){
StringBuffer sb = new StringBuffer();
sb.append("Circle: [ radius=");
sb.append(this.radius);
sb.append(", color=");
sb.append(this.color);
sb.append(" ]");
return sb.toString();
}
public double getArea() {
return Math.PI * this.radius * this.radius;
}
}
一键复制
编辑
Web IDE
原始数据
按行查看
历史