JAVA大神求救!!!求修改代码,求圆的面积和周长。
定义一个Circle类(圆形类),该类中有圆心的坐标x,y;圆的半径r,面积area,周长length五个属性,另外还有一个带三个参数的构造方法(三个参数分别传递圆心的坐...
定义一个Circle类(圆形类),该类中有圆心的坐标x,y;圆的半径r,面积area,周长length五个属性,另外还有一个带三个参数的构造方法(三个参数分别传递圆心的坐标,及半径),有设置圆心坐标的方法,有设置圆半径的方法、求圆的面积的方法、求圆的周长的方法。在此基础上写一个类,该类中有一个public static void main(String arg[])方法,在该方法中创建一个圆形对象,打印出该圆的面积和周长;修改该圆形对象的半径为50,再打印出该圆的面积和周长。
public class Lab4_4 {
public class Circle {
double x; double y; double radius; double area; double length;
Circle(double r) {
radius=r;
}
void setXY(double x, double y) {
this.x = x; this.y = y;
}
double getArea() {
return 3.14*radius*radius;
}
void setRadius(double r) {
radius=r;
}
double getRadius() {
return radius;
}
void getLength() {
length = 2 * java.lang.Math.PI * radius;
}
}
public class Circular {
double x; double y; double radius; double area; double length;
public static void main(String[] args) {
double r=50;
Circular c = new Circular();
System.out.println("半径:"+c.getRadius());
System.out.println("周长:"+c.getLength());
System.out.println("面积:"+c.getArea());
}
}
} 展开
public class Lab4_4 {
public class Circle {
double x; double y; double radius; double area; double length;
Circle(double r) {
radius=r;
}
void setXY(double x, double y) {
this.x = x; this.y = y;
}
double getArea() {
return 3.14*radius*radius;
}
void setRadius(double r) {
radius=r;
}
double getRadius() {
return radius;
}
void getLength() {
length = 2 * java.lang.Math.PI * radius;
}
}
public class Circular {
double x; double y; double radius; double area; double length;
public static void main(String[] args) {
double r=50;
Circular c = new Circular();
System.out.println("半径:"+c.getRadius());
System.out.println("周长:"+c.getLength());
System.out.println("面积:"+c.getArea());
}
}
} 展开
1个回答
展开全部
//修改后的代码
public class Lab4_4 {
public static void main(String[] args) {
Circle c = new Circle(1, 1, 10);
System.out.println("半径:" + c.getRadius());
System.out.println("周长:" + c.getLength());
System.out.println("面积:" + c.getArea());
c.setRadius(50);
System.out.println("半径:" + c.getRadius());
System.out.println("周长:" + c.getLength());
System.out.println("面积:" + c.getArea());
}
}
class Circle {
private double x;
private double y;
private double radius;
private double area;
private double length;
public Circle(double r) {
radius = r;
}
public Circle(double x, double y, double r) {
this.x = x;
this.y = y;
radius = r;
}
void setXY(double x, double y) {
this.x = x;
this.y = y;
}
double getArea() {
return Math.PI * radius * radius;
}
void setRadius(double r) {
radius = r;
}
double getRadius() {
return radius;
}
public double getLength() {
return 2 * java.lang.Math.PI * radius;
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询