⼀套完整的java程序_编写⼀个完整的JavaApplication程序展开全部
/*
* --测试类 (4)Test类作为主类要完成测试功能 ① ⽣成Rectangle对象 ② 调⽤对
62616964757a686964616fe4b893e5b19e31333335346137象的toString⽅法,输出对象的描述信息
*/
public class Test {
public static void main(String args[]) {
Rectangel rectangel = new Rectangel(1,2);
System.out.println(rectangel);
Triangle triangle = new Triangle(3,4,5);
System.out.println(triangle);
Circle circle = new Circle(1);
System.out.println(circle);
}
}
/*
* --⽗类 (1)⽗类Shape: name:String类型,形状的名称 color:String类型,形状的颜⾊
*/
class Shape {
private String name;
private String color;
}
/
*
* --接⼝ (2)IshapeCaculate double getArea():求⼀个形状的⾯积 double getPerimeter
* ():求⼀个形状的周长
*/
interface IshapeCaculate {
public double getArea();
public double getPerimeter();
}
/*
* --⼦类 (3.1)类 Rectangel: 实现接⼝IshapeCaculate,并有以下属性和⽅法: ① 属性 width:
* double类型,表⽰矩形的长 height: double类型,表⽰矩形的⾼ ② ⽅法 Rectangle(double w, double
* h):构造函数 toString()⽅法 :输出矩形的描述信息,如“矩形 width=1.0,height=2.0, perimeter=6.0,
* area=2.0”
*/
class Rectangel extends Shape implements IshapeCaculate {
private double width;
private double height;
public Rectangel(double w, double h) {
this.width = w;
this.height = h;
}
@Override
public double getArea() {
return width * height;
}
@Override
public double getPerimeter() {
return 2 * (width + height);
}
public String toString() {
return "矩形 width=" + width + ",height=" + height + ", perimeter=" + getPerimeter() + ",area=" + getArea();
}
}
/*
* (3.2)类Triangle: 实现接⼝IshapeCaculate,并有以下属性和⽅法: ① 属性 x,y,z:double型,表⽰三⾓形的三条边 s: * 周长的1/2(注:求三⾓形⾯积公式为sqrt(s(s-x)(s-y)(s-z)) ,s=(x+y+z)/2
* ,开⽅可⽤Math.sqrt(double)⽅法) ② ⽅法 Triangle(double x, double y, double
* z):构造函数,给三条边和s赋初值。 toString():输出矩形的描述信息,如“三⾓形 three
* sides:3.0,4.0,5.0,perimeter=12.0,area=6.0” --测试类 (4)Test类作为主类要完成测试功能 ①
* ⽣成Rectangle对象 ② 调⽤对象的toString⽅法,输出对象的描述信息
*
*/
class Triangle extends Shape implements IshapeCaculate {
private double x;
private double y;
private double z;
private double s;
public Triangle(double x, double y, double z) {
this.x = x;
this.y = y;
this.z = z;
s = getPerimeter() / 2;
}
@Override
public double getArea() {
return Math.sqrt(s * (s - x) * (s - y) * (s - z));
}
@Override
public double getPerimeter() {
return x + y + z;
}
public String toString() {
return "三⾓形 three sides:" + x + "," + y + "," + z + ",perimeter=" + getPerimeter() + ",area=" + getArea(); }
}
/*
* (3.3)类Circle实现接⼝IshapeCaculate,并有以下属性和⽅法: ① 属性 radius: double类型,圆的半径 ② ⽅法* Circle(double radius):构造函数 toString()⽅法 :输出矩形的描述信息,如“圆形 radius
=1.0,
* perimeter=6.28, area=3.14”
*/
class Circle extends Shape implements IshapeCaculate {
private double radius;
public Circle(double radius) {
this.radius = radius;
}
java的tostring方法@Override
public double getArea() {
return Math.PI * Math.pow(radius, 2);//圆周率*半径的平⽅
}
@Override
public double getPerimeter() {
return 2 * 3.1415926 * radius;
}
public String toString() {
return "圆形 radius=" + radius + ",perimeter=" + getPerimeter() + ", area=" + getArea(); }
}
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论