public class Test { 17. 18. /** 19. * @param args 20. */ 21. public static void main(String[] args) { 22. // TODO Auto-generated method stub 23. double length, area; 24. Circle circle = null; 25. Trangle trangle; 26. Lader lader; 27. circle = new Circle(6); // 创建对象circle 28. trangle = new Trangle(3, 4, 5); // 创建对象trangle。 29. lader = new Lader(3, 4, 5); // 创建对象lader 30. length = circle.getLength(); // circle调用方法返回周长并赋值给length 31. System.out.println("圆的周长:" + length); 32. area = circle.getArea(); // circle调用方法返回面积并赋值给area 33. System.out.println("圆的面积:" + area); 34. length = trangle.getLength(); // trangle调用方法返回周长并赋值给length 35. System.out.println("三角形的周长:" + length); 36. area = trangle.getArea(); // trangle调用方法返回面积并赋值给area 37. System.out.println("三角形的面积:" + area); 38. area = lader.getArea(); // lader调用方法返回面积并赋值给area 39. System.out.println("梯形的面积:" + area); 40. trangle.setABC(12, 34, 1); // trangle调用方法设置三个边,要求将三个边修改为12,34,1。 41. area = trangle.getArea(); // trangle调用方法返回面积并赋值给area 42. System.out.println("三角形的面积:" + area); 43. length = trangle.getLength(); // trangle调用方法返回周长并赋值给length 44. System.out.println("三角形的周长:" + length); 45. 46. } 47. 48.}
Circle类:
01.public class Circle { 02. double radius, area; 03. 04. Circle(double r) { 05. radius = r; // 方法体 06. } 07. 08. double getArea() { 09. return Math.PI * radius * radius; // 方法体,要求计算出area返回 10. } 11. 12. double getLength() { 13. return 2 * Math.PI * radius; // getArea方法体的代码,要求计算出length返回 14. } 15. 16. void setRadius(double newRadius) { 17. radius = newRadius; 18. } 19. 20. double getRadius() { 21. return radius; 22. } 23. 24.}
Ladder类:
01.public class Lader { 02. double above, bottom, height, area; 03. 04. Lader(double a, double b, double h) { 05. // 方法体,将参数a,b,c分别赋值给above,bottom,height 06. above = a; 07. bottom = b; 08. height = h; 09. } 10. 11. double getArea() { 12. // 方法体,,要求计算出area返回 13. return ((above + bottom) * height) / 2; 14. } 15. 16.}
Triangle类:
01.public class Trangle { 02. double sideA, sideB, sideC, area, length; 03. boolean boo; 04. 05. public Trangle(double a, double b, double c) { 06. // 参数a,b,c分别赋值给sideA,sideB,sideC 07. this.sideA = a; 08. this.sideB = b; 09. this.sideC = c; 10. if (a + b > c && a + c > b && b + c > a && a != 0 && b != 0 && c != 0) // a,b,c构成三角形的条件表达式 11. { 12. boo = true; // 给boo赋值。 13. } else { 14. boo = false; // 给boo赋值。 15. } 16. } 17. 18. double getLength() { 19. length = sideA + sideB + sideC; // 方法体,要求计算出length的值并返回 20. return length; 21. } 22. 23. public double getArea() { 24. if (boo) { 25. double p = (sideA + sideB + sideC) / 2.0; 26. area = Math.sqrt(p * (p - sideA) * (p - sideB) * (p - sideC)); 27. return area; 28. } else { 29. System.out.println("不是一个三角形,不能计算面积"); 30. return 0; 31. } 32. } 33. 34. public void setABC(double a, double b, double c) { 35. // 参数a,b,c分别赋值给sideA,sideB,sideC 36. this.sideA = a; 37. this.sideB = b; 38. this.sideC = c; 39. if (a + b > c && a + c > b && b + c > a && a != 0 && b != 0 && c != 0) // a,b,c构成三角形的条件表达式 40. { 41. boo = true; // 给boo赋值。 42. } else { 43. boo = false; // 给boo赋值。 44. } 45. } 46. 47.}