package com.edu.www; /** * 作业说明:创建圆形、三角形、方形 三个形状类,具有高宽等属性和能够计算周长、面积的成员方法 * 作者:闫中宇 * 班级:一班 * 学号:2014011546 */ public class Test { public static void main(String[] args) { // TODO Auto-generated method stub Circle c = new Circle(5); Rectangle r=new Rectangle(1,2); Triangle t = new Triangle(2,2,3); c.display(); r.display(); t.display(); } } 方类 package com.edu.www; public class Rectangle { private double Length;//长 private double Width;//宽 private double area;//面积 private double perimeter;//周长 public Rectangle(double Length, double Width) { if(Width <= 0 && Length <= 0){ System.out.println("方形的边不能为负数!"); }else{ this.Length = Length; this.Width = Width; this.area = Length * Width; this.perimeter = 2*(Width + Length); } } public Rectangle(int length2, int width2) { // TODO Auto-generated constructor stub } public double getLength() { return Length; } public double getWidth() { return Width; } public double getArea() { return area; } public double getPerimeter() { return perimeter; } public void display(){ System.out.println("方形的面积:"+getArea()); System.out.println("方形的周长:"+getPerimeter()); } } 三角类 package com.edu.www; import java.text.DecimalFormat; public class Triangle { private double a; private double b; private double c; private double area; private double perimeter; private DecimalFormat df = new DecimalFormat("0.00"); public Triangle(double a, double b, double c) { double p; if(judgeSetup(a,b,c)){ this.a = a; this.b = b; this.c = c; p = (a+b+c)/2; this.area = Math.sqrt( (p*(p-a)*(p-b)*(p-c))); this.perimeter = a+b+c; } else if(a<0 || b<0 || c<0){ System.out.println("三角形任意一边不能为负数!"); } else{ System.out.println("三角形三边无法构成三角形!"); } } public boolean judgeSetup(double a,double b,double c){ double max,min; max = (a>b)?a:b; max = (max>c)?max:c; min = (a<b)?a:b; min = (min<c)?min:c; if( (max-min) < (a+b+c-max-min)&&(a+b+c-max) > max ){ return true; } else{ return false; } } public double getA() { return a; } public double getB() { return b; } public double getC() { return c; } public double getArea() { return area; } public double getPerimeter() { return perimeter; } public void display(){ System.out.println("三角形的面积:"+df.format( getArea() )); System.out.println("三角形的周长:"+getPerimeter()); } } 圆类 package com.edu.www; import java.text.DecimalFormat; public class Circle { private double radius; private double area; private double perimeter; private DecimalFormat df = new DecimalFormat("0.00"); public Circle(double radius) { if(radius<=0){ System.out.println("圆的半径不可为负数"); }else{ this.radius = radius; this.area = this.radius * this.radius * 3.14; this.perimeter = 2 * 3.14 *radius; } } public double getRadius() { return radius; } public void setRadius(double a) { this.radius = a; } public double getArea() { return area; } public double getPerimeter() { return perimeter; } public void display(){ System.out.println("圆的面积是:"+getArea()); System.out.println("园的周长是:"+df.format( getPerimeter() )); } }
创建圆形、三角形、方形 三个形状类,具有高宽等属性和能够计算周长、面积的成员方法
10
分
任务尚未发布或者你没有权限查看任务内容。
任务讨论