【问题标题】:Learning about Inheritance, and can't get my calculations to work学习继承,但无法让我的计算工作
【发布时间】:2016-05-17 10:22:09
【问题描述】:

作业要求执行以下操作,但我无法获得除 0 以外的任何内容来显示面积或体积。我不知道这是继承问题还是数学问题(我想我正确地转换为 int ?) 任何帮助将不胜感激。

设计并实现三个不同的类,它们共同定义形状:圆形、圆锥和球体。对于每个类,存储有关其大小的基本数据,并提供访问和修改这些数据的方法。此外,请提供适当的方法来计算 Sphere 和 Cone 的面积和体积。

在您的设计中,请考虑形状之间的关系以及可以在何处实现继承。不要创建重复的实例变量。创建一个 main 方法,实例化 2 个 Sphere 对象(任何参数)、2 个 Cone 对象(任何参数),使用 ToString() 显示它们,更改每个参数(您的选择),然后再次显示它们。

Attached 是一个可选的基本文件,可帮助您在同一个文件中定义不同的类。请记住,您必须重命名基本文件和类以包含您的姓氏。

这是我的代码。作业要求我们将所有内容都提交到一个文件中,而每个班级需要单独的文件,这就是为什么它们都在同一个文件中。

public class Coughlin_A04Q1
{
public int radius;
public int height;
public int area;
public int volume;

public static void main(String[] args)
{
    Cone cone1 = new Cone(10,20);
    Cone cone2 = new Cone(5,10);
    Sphere sphere1 = new Sphere(3);
    Sphere sphere2 = new Sphere(5);
    System.out.println(cone1);
    System.out.println(cone2);
    System.out.println(sphere1);
    System.out.println(sphere2);


}


public static class Round extends Coughlin_A04Q1
{

private int volume()
{
return (int)(Math.PI * Math.pow(radius,2.0) * height);
}
private int area()
{
return (int)((2*Math.PI*radius*height) + (2*Math.PI*Math.pow(radius,2)));
}
public Round(int radius, int height)
{
this.radius = radius;
this.height = height;
}
}

/////////////////////////////////////// ////////////////////////////////////p>

public static class Cone extends Coughlin_A04Q1
{


public Cone(int radius, int height)
{
    this.radius = radius;
    this.height = height;
}
public int area()
{
return (int)(Math.PI*radius*(radius +      

Math.sqrt(Math.pow(height,2.0)+Math.pow(height,2.0))));
}
public int volume()
{
return (int)(Math.PI*Math.pow(radius,2)*(height/3));
}
public String toString()
{
    return "A Cone of radius: " +radius+ ", area: "+area+", and volume: "  

+volume+".";
}
}

/////////////////////////////////////// /////////////////////////////////////////p>

public static class Sphere extends Coughlin_A04Q1
{

public Sphere(int radius)
{
    this.radius=radius;
}
public int area()
{
return (int)(4*Math.PI*Math.pow(this.radius,2));

}

public int volume()
{
return (int)((4/3)*Math.PI*Math.pow(radius,3));

}
public String toString()
{
    return "A Sphere of radius: " +radius+ ", area: "+ area +", and volume: 

" +volume+".";
}
}
}

【问题讨论】:

  • 您显示了很多代码,但没有结果。显示结果和任何错误消息。
  • 对不起,这是输出。半径圆锥:10,面积:0,体积:0。半径圆锥:5,面积:0,体积:0。半径球体:3,面积:0,体积:0。球体半径:5,面积:0,体积:0。
  • int 指的是整数。为什么不使用doublefloat 来计算面积和体积?注意4/31...
  • 最大的问题是函数ToString返回的是Coughlin_A04Q1类的成员areavolume的值,它们永远不会被修改,即使在函数area()和@中也是如此987654334@.

标签: java class inheritance math subclass


【解决方案1】:

从一个界面开始:

package math.Shapes;

/**
 * 3DShape interface
 * Created by Michael
 * Creation date 2/7/2016.
 * @link https://stackoverflow.com/questions/35258063/learning-about-inheritance-and-cant-get-my-calculations-to-work
 */
public interface Shape3D {
    double area();
    double volume();
}

实现锥体:

package math.Shapes;

/**
 * Cone 3D shape
 * Created by Michael
 * Creation date 2/7/2016.
 * @link https://stackoverflow.com/questions/35258063/learning-about-inheritance-and-cant-get-my-calculations-to-work
 */
public class Cone implements Shape3D {

    private final double radius;
    private final double height;

    public Cone(double radius, double height) {
        if (radius <= 0.0) throw new IllegalArgumentException("radius must be positive");
        if (height <= 0.0) throw new IllegalArgumentException("height must be positive");
        this.radius = radius;
        this.height = height;
    }

    @Override
    public double area() {
        return Math.PI*this.radius*(this.radius+this.getDiscriminant());
    }

    @Override
    public double volume() {
        return Math.PI*this.radius*this.radius*this.height/3.0;
    }

    private double getDiscriminant() {
        double discriminant = 0.0;
        if (this.radius > this.height) {
            double ratio = this.height/this.radius;
            discriminant = this.radius*Math.sqrt(1.0+ratio*ratio);
        } else {
            double ratio = this.radius/this.height;
            discriminant = this.height*Math.sqrt(1.0+ratio*ratio);
        }
        return discriminant;
    }

    @Override
    public String toString() {
        return String.format("radius: %10.3f height: %10.3f area: %10.3f volume: %10.3f", this.radius, this.height, this.area(), this.volume());
    }
}

实现球体:

package math.Shapes;

/**
 * Sphere class
 * Created by Michael
 * Creation date 2/7/2016.
 * @link https://stackoverflow.com/questions/35258063/learning-about-inheritance-and-cant-get-my-calculations-to-work
 */
public class Sphere implements Shape3D {

    private final double radius;

    public Sphere(double radius) {
        if (radius <= 0.0) throw new IllegalArgumentException("radius must be positive");
        this.radius = radius;
    }

    @Override
    public double area() {
        return 4.0*Math.PI*this.radius*this.radius;
    }

    @Override
    public double volume() {
        return this.area()*this.radius/3.0;
    }

    @Override
    public String toString() {
        return String.format("radius: %10.3f area: %10.3f volume: %10.3f", this.radius, this.area(), this.volume());
    }
}

终于让驱动测试一下了:

package math.Shapes;

import java.util.ArrayList;
import java.util.List;

/**
 * Driver for Shape3D
 * Created by Michael
 * Creation date 2/7/2016.
 * @link https://stackoverflow.com/questions/35258063/learning-about-inheritance-and-cant-get-my-calculations-to-work
 */
public class DriverShape3D {
    public static void main(String[] args) {
        List<Shape3D> shapes = new ArrayList<Shape3D>() {{
            add(new Cone(1.0, 1.0));
            add(new Sphere(1.0));
            add(new Cone(5.0, 5.0));
            add(new Sphere(5.0));
        }};
        for (Shape3D shape : shapes) {
            System.out.println(shape);
        }
    }
}

保持简单。注意样式和格式:可读性就是一切。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-06-14
    • 1970-01-01
    • 2020-02-27
    • 1970-01-01
    • 1970-01-01
    • 2018-04-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多