gpt4 book ai didi

java - 为什么我的 setter 和 getter 方法不起作用?

转载 作者:行者123 更新时间:2023-11-29 03:35:36 24 4
gpt4 key购买 nike

我一直在查看我的源代码,但无法弄清楚它有什么问题。我认为问题出在 Circle 类上。当我从DriverCircle 类给了我错误的输出。对于 getDiameter,它只是打印出 0。

public class Circle{
private double radius;
private double pi;
private double diameter;
private double circumference;
private double area;

public Circle(){
pi = Math.PI;
radius = 0;
}

public Circle(double radius){
this.radius = radius;
}
public void setDiameter(){
diameter = (2 * radius);
}
public double getDiameter(){
//diameter = 2 * radius;
return diameter;
}
public void setCircumference(){
circumference = (2 * pi * radius);
}
public double getCircumference(){
//circumference = 2 * pi * radius;
return circumference;
}
public double getArea(){
//area = pi * Math.pow(radius, 2);
return area;
}
public void setArea(){
area = (pi * Math.pow(radius, 2));
}
public void setRadius(double radius){
this.radius = radius;
}
public double getRadius(){
return radius;
}
public String toString(){
return "The radius is " + radius;
}
}

(测试人员)...

import java.util.Scanner;

public class CircleDriver {

public static void main(String[] args) {

Scanner input = new Scanner(System.in);
System.out.println("Please enter the radius: ");
Circle[] circles = new Circle[10];
Circle objectCircle = new Circle();
objectCircle.setRadius(input.nextDouble());


circles[1] = new Circle();
circles[2] = new Circle(2.0);
circles[3] = new Circle(3.5);
circles[4] = new Circle(5.0);
circles[5] = new Circle(0.0);
circles[6] = new Circle(15);
circles[7] = new Circle(25);
circles[8] = new Circle(-7);
circles[9] = new Circle(-10.0);

System.out.println("Initial call to toString():");
for (Circle c : circles)
{System.out.println("\t" + c);}

System.out.println("Call to getRadius (should be same as above):");
for (Circle r : circles)
{if (r != null)
{System.out.println("\t" + r.getRadius());}}

System.out.println("Call to getDiameter (should be twice the value shown above):");
for (Circle d : circles)
{if (d != null)
{System.out.println("\t" + d.getDiameter());}}

System.out.println("Calls to getCircumference:");
System.out.println("\tShould be 2 * PI: " + circles[1].getCircumference());
System.out.println("\tShould be 0.0: " + circles[5].getCircumference());

System.out.println("\nCall to getArea:");
System.out.println("\tShould be PI: " + circles[1].getArea());
System.out.println("\tShould be 0.0: " + circles[5].getArea());

System.out.println("Testing out the setRadius method:");
for (int i = 0; i < circles.length / 2; i++)
{if (circles[i] != null)
{circles[i].setRadius(i);}}

System.out.println("Call to toString after setting the first half of the objects:");
for (Circle c : circles)
{System.out.println("\t" + c);}
}
}

最佳答案

您的 setter 方法应该有参数并使用参数来设置字段。否则它们实际上不是 setter 方法。您当前的 setter 方法应该全部丢弃,也许 setRadius(...) 除外,并且大部分计算都在各自的 getter 方法中完成。

即不

public void setCircumference(){
circumference = (2 * pi * radius);
}

public double getCircumference(){
//circumference = 2 * pi * radius;
return circumference;
}

而是

public double getCircumference(){
return 2 * Math.PI * radius;
}

关于java - 为什么我的 setter 和 getter 方法不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15714496/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com