学以致用——Java源码——使用多态输出平面及立体几何图形的面积和体积(Project: Shape Hierarchy)

2022-08-08,,,,

程序功能:

使用继承和多态的面向编程思想,动态的判断几何形状,打印平面图形面积及立体几何图形的面积和体积

这个习题让我从无到有创建了共10个类才完成,虽然简单,但是作为继承和多态的入门练习还是不错的!

测试结果:

逐个处理:

 

circle's area:201.06

square's area:64.00

triangle's area:24.00

sphere's area:804.25

sphere's volume:2144.66

cube's area:384.00

cube's volume:512.00

tetrahedron's area:110.85

tetrahedron's volume:60.34

 

 

多态处理:

 

图形 0是一个圆

面积:201.06

 

图形 1是一个正方形

面积:64.00

 

图形 2是一个三角形

面积:24.00

 

图形 3是一个球

面积:804.25

体积:2144.66

 

图形 4是一个立方体

面积:384.00

体积:512.00

 

图形 5是一个圆

面积:110.85

体积:60.34

 

 代码:

1. 测试类

//Java How to Program, Exercise 10.13: Project: Shape Hierarchy
//by pandenghuang@163.com
/**
 * 10.13 (Project: Shape Hierarchy) Implement the Shape hierarchy shown in Fig.
 * 9.3. Each Two-DimensionalShape should contain method getArea to calculate the
 * area of the two-dimensional shape. Each ThreeDimensionalShape should have
 * methods getArea and getVolume to calculate the surface area and volume,
 * respectively, of the three-dimensional shape. Create a program that uses an
 * array of Shape references to objects of each concrete class in the hierarchy.
 * The program should print a text description of the object to which each array
 * element refers. Also, in the loop that processes all the shapes in the array,
 * determine whether each shape is a TwoDimensionalShape or a
 * ThreeDimensionalShape. If it’s a TwoDimensionalShape, display its area. If
 * it’s a ThreeDimensionalShape, display its area and volume.
 * 
 * @author Pandenghuang@163.com
 * @Date Jan 7, 2019, 9:13:12 AM
 *
 */
public class ShapeHierarchyTest {
	
	
	public static void main(String[] args) {
		
		Circle circle = new Circle(8);
		Square square = new Square(8);
		Triangle triangle = new Triangle (6,8,10);
		
		Sphere sphere = new Sphere(8);
		Cube cube = new Cube(8);
		Tetrahedron tetrahedron = new Tetrahedron(8);
		
		
		
		System.out.printf("逐个处理:%n%n");
		System.out.printf("circle's area:%.2f%n", circle.getArea());
		System.out.printf("square's area:%.2f%n", square.getArea());
		System.out.printf("triangle's area:%.2f%n", triangle.getArea());
		
		System.out.printf("sphere's area:%.2f%n", sphere.getArea());
		System.out.printf("sphere's volume:%.2f%n", sphere.getVolume());
		System.out.printf("cube's area:%.2f%n", cube.getArea());
		System.out.printf("cube's volume:%.2f%n", cube.getVolume());
		System.out.printf("tetrahedron's area:%.2f%n", tetrahedron.getArea());
		System.out.printf("tetrahedron's volume:%.2f%n", tetrahedron.getVolume());
		
		Shape[] shapes = new Shape[6];
		shapes[0] = circle;
		shapes[1] = square;
		shapes[2] = triangle;
		shapes[3] = sphere;
		shapes[4] = cube;
		shapes[5] = tetrahedron;
		
		System.out.printf("%n%n多态处理:%n%n");
		 // get type name of each object in employees array
	      for (int j = 0; j < shapes.length; j++) {
	    	  
	    	 String[] classInfo =shapes[j].getClass().getName().split("\\.");
	    	 String className = classInfo[classInfo.length-1];
	    	 String classNameCN = "圆";
			 switch (className) {
			 case "Circle":
				 classNameCN = "圆";
				 break;
			 case "Square":
				 classNameCN = "正方形";
				 break;
			 case "Triangle":
				 classNameCN = "三角形";
				 break;
			 case "Sphere":
				 classNameCN = "球";
				 break;
			 case "Cube":
				 classNameCN = "立方体";
				 break;
			 case "Terahedron":
				 classNameCN = "正四面体";
				 break;
			 }
	         System.out.printf("图形 %d是一个%s%n", j, classNameCN); //打印对象的类型(类名)
	         
	         if (shapes[j] instanceof TwoDimensionalShape) 
	        	 System.out.printf("面积:%.2f%n%n", shapes[j].getArea());	//如果是二维图形,打印面积
	         else if (shapes[j] instanceof ThreeDimensionalShape) {
	        	 ThreeDimensionalShape threeDimensionalShape = (ThreeDimensionalShape)shapes[j];  //如果是三维图形,打印面积和体积
	        	 System.out.printf("面积:%.2f%n", threeDimensionalShape.getArea());
	        	 System.out.printf("体积:%.2f%n%n", threeDimensionalShape.getVolume());
	         }
	      }
		
	}

}

2. 实体类

1)Shape类(基类,图形)

/**
 * 
 * @author Pandenghuang@163.com
 * @Date Jan 7, 2019, 1:44:02 AM
 *
 */
public abstract class Shape {
	public abstract double getArea();
}

2)TwoDimensionalShape类 (平面图形)

public abstract class TwoDimensionalShape extends Shape {

}

3)ThreeDimensionalShape类 (立体图形)

public abstract class ThreeDimensionalShape extends Shape{
	
	public abstract double getVolume(); 

}

4)Circle类 (圆)

public class Circle extends TwoDimensionalShape {
	
	private double radius;
	
	Circle(double radius){
		this.radius = radius;
	}
	
	@Override
	public double getArea() {
		return Math.PI*Math.pow(radius,2);
	}

}

5)Square类 (正方形)

public class Square extends TwoDimensionalShape{

	private double sideLength;
	
	Square(double sideLength){
		this.sideLength = sideLength;
	}
	
	@Override
	public double getArea() {
		return Math.pow(sideLength,2);
	}
}

6)Triangle类 (三角形)

public class Triangle extends TwoDimensionalShape {
	private double a;
	private double b;
	private double c;
	
	public Triangle (double a, double b, double c) {
		this.a = a;
		this.b = b;
		this.c = c;
		
	}
	
	@Override
	public double getArea() {
		double p = (a + b + c)/2.0;
		return Math.sqrt(p*(p-a)*(p-b)*(p-c));
	}
	

}

7) Sphere类 (球)

public class Sphere extends ThreeDimensionalShape {
	
	private double radius;
	
	Sphere(double radius){
		this.radius = radius;
	}
	
	@Override
	public double getArea() {
		return 4*Math.PI*Math.pow(radius,2);
	}

	@Override
	public double getVolume() {
		return 4.0/3.0*Math.PI*Math.pow(radius,3);
	}
}

8)Cube类 (立方体)

public class Cube extends ThreeDimensionalShape {
	
	private double length;
	
	Cube(double length){
		this.length = length;
	}
	
	@Override
	public double getArea() {
		return 6*Math.pow(length,2);
	}

	@Override
	public double getVolume() {
		return Math.pow(length,3);
	}
}

9)Tetrahedron类(正四面体)

public class Tetrahedron extends ThreeDimensionalShape {
	
	private double length;
	
	Tetrahedron(double length){
		this.length = length;
	}
	
	@Override
	public double getArea() {
		return Math.sqrt(3)*Math.pow(length,2);
	}

	@Override
	public double getVolume() {
		return Math.sqrt(2)/12*Math.pow(length,3);
	}
}

 

本文地址:https://blog.csdn.net/hpdlzu80100/article/details/85985958

《学以致用——Java源码——使用多态输出平面及立体几何图形的面积和体积(Project: Shape Hierarchy).doc》

下载本文的Word格式文档,以方便收藏与打印。