【问题标题】:Program print keeps print 0.0 for Cylinder area, volume, range + inheritance java程序打印为圆柱体面积、体积、范围+继承java保持打印0.0
【发布时间】:2016-10-25 02:28:17
【问题描述】:

我已经尝试了几种不同的方法。我需要使用继承来扩展这些类。每次我运行程序时,它都会输出 0.0 的体积和面积。半径显示正确。在底部输出。

public class Base_HW04Q1
{
    public double pi = 3.14, l, radius, height, area, volume;

    public static class RoundShape extends Base_HW04Q1 {
        public RoundShape(double radius) {
            this.radius = radius;
        }
        public double calcArea () {
            area = (radius * radius) * pi;
            return area;
        }
        public String toString() {
            return "A Round Shape of radius: " + radius + ", area " + area + ".";
        }
    }


    public static class Cylinder extends Base_HW04Q1
    {
        public Cylinder(double radius, double height) {
            this.radius = radius;
            this.height = height;
        }
        public double calcArea() {
            l = Math.sqrt((radius * radius) + (height * height));
            area = 2 * pi * radius * height + 2 * pi * l;
            return area;
        }
        public double calcVolume() {
            volume = pi * (radius * radius) * height; 
            return volume;
        }
        public String toString() {
            return "A Cylinder of radius: " + radius + ", area " + area + " and a volume of " + volume;
        }
    }


    public static class Cone extends Base_HW04Q1 //TODO: This line is almost, but not quite, complete.
    {
        public Cone(double radius, double height) {
            this.radius = radius;
            this.height = height;
        }
        public double calcArea() {
            l = Math.sqrt((radius * radius) + (height * height));
            area = (pi * radius * l) + (pi * radius * radius);
            return area;
        }
        public double calcVolume() {
            volume = 0.333 * pi * radius * radius * height;
            return volume;
        }
        public String toString() {
            return "A Cone of radius: " + radius + ", area " + area + " and a volume of " + volume;
        }
    }   

    public static void main(String[] args)
    {  
        //object creation
        Cylinder Cylinder1 = new Cylinder(30, 10);
        Cone Cone1 = new Cone(10, 20);
        RoundShape RoundShape1 = new RoundShape(50);

        //print for objects
        System.out.println(Cylinder1);
        System.out.println(RoundShape1);
        System.out.println(Cone1);
    }
}

输出:

一个圆柱体,半径:30.0,面积 0.0,体积 0.0 圆形 半径:50.0,面积 0.0。半径圆锥:10.0,面积 0.0 和 体积为 0.0

【问题讨论】:

  • 你怎么称呼这段代码?

标签: java


【解决方案1】:

您的toString() 从不调用进行计算的方法,而是打印默认的 0.0 字段值。如果在调用 calcXxxx() 方法之前调用了 toString(),您将面临这种风险,即在计算字段被赋予合适的值之前。最好的解决方案是首先通过完全摆脱计算值的字段(例如面积和体积)来防止此问题发生。而是在 toString() 中调用方法来获取这些值。

例如,

public double pi = 3.14, l, radius, height; // , area, volume;

public static class RoundShape extends Base_HW04Q1 {
    public RoundShape(double radius) {
        this.radius = radius;
    }
    public double calcArea () {
        return (radius * radius) * pi;
        // return area;
    }
    public String toString() {
        return "A Round Shape of radius: " + radius + ", area " + calcArea() + ".";
    }
}

【讨论】:

  • 你知道如何将双精度数限制为 3 吗?例如,0.00 ?而不是我当前的答案 7363.62732812
  • @TheMuffinMan:您将数字与其字符串表示混淆了。不要“限制”你的双精度,但是当你打印出来时,format 它,可以使用 DecimalFormat 对象或 System.out.printf。
【解决方案2】:

这是因为你只实例化了对象并进入了构造函数,而没有进入其他方法。

    Cylinder Cylinder1 = new Cylinder(30, 10);
    Cone Cone1 = new Cone(10, 20);
    RoundShape RoundShape1 = new RoundShape(50);

在这些方法中没有调用任何人

    public double calcArea() {
        l = Math.sqrt((radius * radius) + (height * height));
        area = (pi * radius * l) + (pi * radius * radius);
        return area;
    }
    public double calcVolume() {
        volume = 0.333 * pi * radius * radius * height;
        return volume;
    }
    public String toString() {
        return "A Cone of radius: " + radius + ", area " + area + " and a volume of " + volume;
    }

和其他方法。如果您想通过以下方法进行计算,请从构造函数或 main 调用它们:

public static void main(String[] args)
{  
    //object creation
    Cylinder Cylinder1 = new Cylinder(30, 10);
    Cone Cone1 = new Cone(10, 20);
    RoundShape RoundShape1 = new RoundShape(50);

    double roundArea = RoundShape1.calcArea();//then use this
    string roundMessage = RoundShape1.toString();//and this whatever you want.

    //do it in others too

    //print for objects
    System.out.println(Cylinder1);
    System.out.println(RoundShape1);
    System.out.println(Cone1);
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-02-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多