求帮忙用java做程序
Thepurposeistousetheobjectorientedprogrammingconceptsdiscussedinclass.Youwilldesignan...
The purpose is to use the object oriented programming concepts discussed in class. You will design and implement a series of classes (hierarchy) that deal with geometric shapes, circle, cone, sphere, cube, rectangular prism, and cylinder. Include methods to determine the perimeter, surface area, volume where appropriate. Include data fields that keep track of the number geometric shapes created, as well as the number of each type of geometric shape created. In addition to using the concepts discussed in class, you are to use the following guide lines:
The driver (use class) should include the following:· Allow for up to 20 objects to be stored (array, array list, vector, linked list)· Allow the user the choice of creating a custom or default shape· Require the user to confirm the shape attributes· Output the number of each shape created in total along with the total number of geometric shapes created· Allow a user to search through the database and find a particular object based on a unique key· List all the objects in the database 展开
The driver (use class) should include the following:· Allow for up to 20 objects to be stored (array, array list, vector, linked list)· Allow the user the choice of creating a custom or default shape· Require the user to confirm the shape attributes· Output the number of each shape created in total along with the total number of geometric shapes created· Allow a user to search through the database and find a particular object based on a unique key· List all the objects in the database 展开
2个回答
展开全部
对于每个几何图形而言,都有一些共同的属性,如名字、面积等,而其计算面积的方法却各不相同。为了简化开发,请编写程序,定义一个超类来实现输入名字的方法,并使用抽象方法来计算面积。
思路分析:
所谓超类就是抽象父类,该抽象类中有两个方法,分别用来获取图形的名称和图形的面积。要获得图形的名称,通过类的getClass().getSimpleName()方法可以实现;要获得图形的面积,因为计算面积的方法各不相同,所以该方法是个抽象方法。
定义一个子类表示圆形,圆形的半径通过构造方法获得,圆形的面积通过重写超类中的抽象方法获得,其中圆周率可以用Math.PI表示。
其他类同步骤2,半径、长、宽等参数通过构造方法获得,这样才省事嘛。
//www.puguoan.cn www.Garfields.cc
public abstract class Shape {
public String getName() {//获得图形的名称
return this.getClass().getSimpleName();
}
public abstract double getArea();//获得图形的面积
}
public class Circle extends Shape {
private double radius;
public Circle(double radius) {//获得圆形的半径
this.radius = radius;
}
@Override
public double getArea() {//计算圆形的面积
return Math.PI * Math.pow(radius, 2);
}
}
public class Rectangle extends Shape {
private double length;
private double width;
public Rectangle(double length, double width) {//获得矩形的长和宽
this.length = length;
this.width = width;
}
@Override
public double getArea() {//计算矩形的面积
return length * width;
}
}
public class Test {
public static void main(String[] args) {
Circle circle = new Circle(1);//创建圆形对象并将半径设置成1
System.out.println("图形的名称是:" + circle.getName());
System.out.println("图形的面积是:" + circle.getArea());
Rectangle rectangle = new Rectangle(1, 1);//创建矩形对象并将长和宽设置成1
System.out.println("图形的名称是:" + rectangle.getName());
System.out.println("图形的面积是:" + rectangle.getArea());
}
}
追问
其他形状和表面积什么的按照这个来做就可以了吧?还有请问前两行//www.puguoan.cn www.Garfields.cc是什么
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询