作业要求执行以下操作,并且我无法获得除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;
}
}
/////////////////////////////////////////////// //////////////////////////////////
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+".";
}
}
/////////////////////////////////////////////// /////////////////////////////////////
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+".";
}
}
}
答案 0 :(得分:0)
从界面开始:
www.yourdomain.com
实施Cone:
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());
}
}
保持简单。注重风格和格式:可读性就是一切。