在 (c) 和 (d) 中放入什么(当我没有在这个问题中编码时,我仍然得到输出)
What to put in (c) and (d) (when i didn"t code in this question i still get the output)
public class Circle {
public static void main(String[] args) {
int diameter; // (a) declare variable(s)
Circle2 c = new Circle2(50); // (b) create a circle with diameter 50
// (c) call to calculate perimeter
// without putting codes here i still get the
// output
// (d) call to calculate area
// (e) display area and perimeter of the circle
System.out.println("Area:" + c.calcArea());
// (e) display area and perimeter of the circle
System.out.println("Perimeter:" + c.calcPerimeter());
}
}
class Circle2 {
double diameter, radius;
public Circle2() {
diameter = 0.0;
radius = 0.0;
}
public Circle2(double cDiameter) {
// (f) construct a circle with a specified diameter and radius
diameter = cDiameter;
radius = diameter / 2;
}
public double calcPerimeter() {
// (g) calculate perimeter
return (22 * diameter) / 7;
}
public double calcArea() {
// (h) calculate area
return (22 * radius * radius) / 7;
}
}
这是一道考试题,这意味着我无法添加或更改任何内容。我所能做的就是填写有问题的代码 (a-h) 我已经多次尝试调用 are 和 perimeter 但它不起作用
a : 你必须声明将被方法填充的变量 :
double perimeter;
double area;
b : 正确
c,d:您将使用方法填充变量:
perimeter = c.calcPerimeter();
area = c.caclArea();
e :你必须显示它们,无论如何你都做对了,因为你直接打印了方法中的值,但问题是要你打印填充的变量
System.out.println("Area:" + area);
System.out.println("Perimeter:" + perimeter);
关于 Perimeter:您必须改用它 return Math.PI * 2 * radius
- 关于区域:
return Math.PI * radius * radius
public class Circle {
public static void main(String[] args) {
int diameter; // (a) declare variable(s)
Circle2 c = new Circle2(50); // (b) create a circle with diameter 50
// (c) call to calculate perimeter
// without putting codes here i still get the
// output
// (d) call to calculate area
// (e) display area and perimeter of the circle
System.out.println("Area:" + c.calcArea());
// (e) display area and perimeter of the circle
System.out.println("Perimeter:" + c.calcPerimeter());
}
}
class Circle2 {
double diameter, radius;
public Circle2() {
diameter = 0.0;
radius = 0.0;
}
public Circle2(double cDiameter) {
// (f) construct a circle with a specified diameter and radius
diameter = cDiameter;
radius = diameter / 2;
}
public double calcPerimeter() {
// (g) calculate perimeter
return (22 * diameter) / 7;
}
public double calcArea() {
// (h) calculate area
return (22 * radius * radius) / 7;
}
}
这是一道考试题,这意味着我无法添加或更改任何内容。我所能做的就是填写有问题的代码 (a-h) 我已经多次尝试调用 are 和 perimeter 但它不起作用
a : 你必须声明将被方法填充的变量 :
double perimeter; double area;
b : 正确
c,d:您将使用方法填充变量:
perimeter = c.calcPerimeter(); area = c.caclArea();
e :你必须显示它们,无论如何你都做对了,因为你直接打印了方法中的值,但问题是要你打印填充的变量
System.out.println("Area:" + area); System.out.println("Perimeter:" + perimeter);
关于 Perimeter:您必须改用它
return Math.PI * 2 * radius
- 关于区域:
return Math.PI * radius * radius