编写一个完整的Java Application 程序.包含类Circle,Cylinder,ShapeTest

编写一个完整的Java Application 程序。包含接口ShapeArea,类Circle、Rectangle、Test,具体要求如下:~

// File: Test.java
//下面是具体的代码,要在命令提示符中运行,再者.记得给分哦!!_^_
//把下面的代码复制到同一个文件中,把名字改为:Test.java 然后编译,运行就可以了~
interface ShapeArea{
double getArea();
double getPerimeter();
}

class Rectangle implements ShapeArea{
double width;
double height;

public Rectangle(double w, double h){
this.width=w;
this.height=h;
}

public String toString(){
String s="width="+this.width+"
";
s+="Height="+this.height+"
";
s+="Perimeter="+this.getPerimeter()+"
";
s+="Area="+this.getArea()+"
";
return s;
}

public double getPerimeter(){
return 2*(width+height);
}

public double getArea(){
return height*width;
}
}

public class Test{
public static void main(String args[]){
if(args.length<2)
System.out.println("请在命令行输入两参数!");
double width=Integer.parseInt(args[0]);
double height=Integer.parseInt(args[1]);
Rectangle rec=new Rectangle(width,height);
System.out.println("该矩形的信息如下:");
System.out.println(rec);
}
}

public class Test {

public static void main(String[] args) {
ShapeArea rectangle = new Rectangle(3, 4);
ShapeArea circle = new Circle(2);

System.out.println(rectangle.toString());
System.out.println(circle.toString());
}

}

interface ShapeArea{
public double getArea();
public double getPerimeter();
}

class Rectangle implements ShapeArea{
private double height;
private double width;

public Rectangle(double w, double h){
this.width = w;
this.height = h;
}

public double getArea() {
return height * width;
}

public double getPerimeter() {
return 2 * (height + width);
}

public String toString(){
return "width=" + width + ",height=" + height + ",perimeter=" + getPerimeter() + ",area" + getArea();
}

}

class Circle implements ShapeArea{

private double r;

public Circle(double r){
this.r = r;
}

public double getArea() {
return Math.PI * r * r;
}

public double getPerimeter() {
return 2 * Math.PI * r;
}

public String toString(){
return "r =" + r + ", area = " + getArea() + ", perimeter = " + getPerimeter();
}

}

-----------------
width=3.0,height=4.0,perimeter=14.0,area12.0
r =2.0, area = 12.566370614359172, perimeter = 12.566370614359172

Circle类:

public class Circle {
protected double radius;
public Circle(double radius) {
this.radius = radius;
}
public Circle() {
radius = 0.0d;
}
public void setRadius(double r){
radius = r;
}
public double getRadius(){
return radius;
}
public double area(){
return Math.PI*(radius*radius);
}
public double perimeter(){
return Math.PI*(2*radius);
}
@Override
public String toString() {
return "Circle(r:"+radius+")";
}
}

Cylinder 类:

public class Cylinder extends Circle {
private double height;
public Cylinder(double radius, double height) {
this.radius = radius;
this.height = height;
}
public Cylinder() {
this.radius = 0.0d;
this.height = 0.0d;
}
public void setHeight(double height){
this.height = height;
}
public double getHeight(){
return height;
}
@Override
public double area() {
return super.perimeter()*height;
}
public double volume(){
return super.area()*height;
}
@Override
public String toString() {
return "Cylinder(r:"+radius+",h:"+height+")";
}
}

ShapeTest 类:

public class ShapeTest {
public ShapeTest() {
Circle circle = new Circle(10.0d);
System.out.println("面积:"+circle.area()+"周长:"+circle.perimeter());
Cylinder cylinder = new Cylinder(10.0d, 10.0d);
System.out.println("表面积:"+cylinder.area()+"体积:"+cylinder.volume());
}
}

看看书 这个不难吧

相关兴趣推荐

IT评价网,数码产品家用电器电子设备等点评来自于网友使用感受交流,不对其内容作任何保证

联系反馈
Copyright© IT评价网