/** * 作业说明: 创建圆形、三角形、方形 三个形状类,具有高宽等属性和能够计算周长、面积的成员方法 * 姓名:王晓旺 * 学号:2014011739 * 班级:5班 */ //Shape.java package com.works1.www; public abstract class Shape { public abstract double getArea(); public Shape() { // TODO Auto-generated constructor stub } } // Circle.java package com.works1.www; public class Circle extends Shape { private double r; final double pi = 3.14; public Circle(double r) { // TODO Auto-generated constructor stub if (r < 0) { System.out.println("圆的r不能为负数"); } else { this.r = r; } } public double getPer() { double L; L = 2 * pi * r; return L; } public double getArea() { double S; S = pi * r * r; return S; } } // Triangle.java package com.works1.www; public class Triangle extends Shape{ private double a, b, c; public Triangle(double a, double b, double c) { // TODO Auto-generated constructor stub if (a < 0 || b < 0 || c < 0 || a + b <= c || a + c <= b || b + c <= a) { System.out.println("输入的数值不满足构成三角形的要求!"); } else { this.a = a; this.b = b; this.c = c; } } public double getPer() { double L; L = a + b + c; return L; } public double getArea() { double p, s; p = (a + b + c) / 2; s = Math.sqrt(p * (p - a) * (p - b) * (p - c)); return s; } } //Rectangle.java package com.works1.www; public class Rectangle extends Shape { private double a, b; public Rectangle(double a, double b) { // TODO Auto-generated constructor stub if (a < 0 || b < 0) { System.out.println("输入的条件不符合题意!"); } else { this.a = a; this.b = b; } } public double getPer() { double L; L = (a + b) * 2; return L; } public double getArea() { double S; S = (a * b); return S; } } // Exp0103Test.java package com.works1.www; public class Exp0103Test{ public static void main(String[] args) { // TODO Auto-generated constructor stub Circle X = new Circle(8); Triangle Y = new Triangle(3, 4, 5); Rectangle Z = new Rectangle(6, 7); System.out.println("圆的周长:" + X.getPer()); System.out.println("圆的面积:" + X.getArea()); System.out.println("三角形的周长:" + Y.getPer()); System.out.println("三角形的面积:" + Y.getArea()); System.out.println("方形的面积:" + Z.getPer()); System.out.println("方形的面积:" + Z.getArea()); } }
作业07 在作业06基础上,抽象出一个abstract类Shape。
10
分
任务尚未发布或者你没有权限查看任务内容。
任务讨论