Java小实验-多态的应用

Java小实验-多态的应用

一、实验目的及要求

通过包的创建,类的继承在程序中的具体应用,进一步掌握多态的特点、技巧及使用等。同时熟练使用Java异常处理机制,处理程序中可能出现的异常。

二、实验环境及要求

Jdk1.8以上的开发环境,可使用任何编辑器。

三、实验内容及相应程序

  1. 设计一个圆类Circle,该类存在于mycircle包中,它具有以下的属性和方法:
    私有属性:圆半径r,圆心坐标x和y,
    方法:设置和获取所有私有属性r,x,y的方法,方法名自定;计算圆周长的方法perimeter();计算圆面积的方法area(),
    要求直接使用Math类中的PI;
  2. 设计一个圆柱体类Cylinder,该类存在于mycylinder包中,Cylinder继承Circle,它增加了以下属性和方法:
    私有属性:高度h,
    方法:设置和获取私有属性h的方法,方法名自定;计算表面积的方法area();计算体积的方法volume();
  3. 编写一个Java Application程序Compute.java,用来显示某一个圆柱体中心坐标位置(如图1中的k的位置),高度h,以及它的表面积(不包括两个底的面积)、体积和底面积。要求圆半径r,圆心坐标x和y,以及高度h的值均从命令行输入。
    提示及注意:
    圆柱体的体积=底面积高;
    圆柱体的表面积=底面周长
    高;
    每一个圆柱体的生成,都是在原有的圆的基础上,添加高度生成的。
  4. 编写测试类Computer.java程序,该类能够处理程序中可能出现的所有的异常 ,并有相应的提示信息。如命令行没有输入,则程序会向控制台输出“命令行没有输入,请输入正确的数值”这样的提示信息;

四、实现

源码

MyCircle.Circle.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package MyCircle;  

public class Circle {
private double x;
private double y;
private double r;

public Circle() {
this.x = 0;
this.y = 0;
this.r = 1;
}

public Circle(double x, double y, double r) {
this.x = x;
this.y = y;
this.r = CheckRadiusFormat(r);
}

public Circle SetX(double x) {
this.x = x;
return this;
}

public Circle SetY(double y) {
this.y = y;
return this;
}

public Circle SetR(double r) {
this.r = CheckRadiusFormat(r);
return this;
}

public double GetX() {
return x;
}

public double GetY() {
return y;
}

public double GetR() {
return r;
}

public double Perimeter() {
return Math.PI * this.r * this.r;
}

public double Area(){
return Math.PI * Math.pow(r, 2);
}

private double CheckRadiusFormat(double r) {
if (r < 0) {
throw new RuntimeException("半径不能小于0");
}
return r;
}

}

MyCylinder.Cylinder

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package MyCylinder;  

import MyCircle.Circle;

public class Cylinder extends Circle {
private double h;

public Cylinder() {
super();
}

public Cylinder(double x, double y, double r, double h) {
super(x, y, r);
this.h = CheckHeightFormat(h);
}

public Double GetH(){
return this.h;
}

public Cylinder SetHeight(double h) {
this.h = CheckHeightFormat(h);
return this;
}

public double Area(){
return super.Area() * 2 + super.GetR() * 2 * this.h;
}

public double SurfaceArea(){
return super.GetR() * 2 * this.h;
}
public double Volume(){
return super.Area() * this.h;
}

public double BottomArea(){
return super.Area();
}

private Double CheckHeightFormat(double h) {
if (h < 0) {
throw new RuntimeException("高度不能为0");
}
return h;
}
}

Compute.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
import MyCylinder.Cylinder;  

///程序入口类
public class Compute {
public static void main(String[] args){
final var vecArgs = Computer.CheckArgs(args);
var cylinder = new Cylinder(vecArgs.get(0),vecArgs.get(1),vecArgs.get(2),vecArgs.get(3));
System.out.printf("%s,高度为%f\n", FormatCoordinate(cylinder), cylinder.GetH());
System.out.println("表面积是:" + cylinder.SurfaceArea());
System.out.println("体积是:" + cylinder.Volume());
System.out.println("底面积是:" + cylinder.BottomArea());
}

public static String FormatCoordinate(Cylinder cylinder){
final var x = cylinder.GetX();
final var y = cylinder.GetY();
final var r = cylinder.GetR();
return String.format("(%f,%f,%f)", x,y,r);
}
}

Computer.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
import java.util.Vector;  

///测试类
public class Computer {
public static Vector<Integer> CheckArgs(String[] args){
if(args.length == 0){
throw new RuntimeException("命令行没有输入,请输入正确的数值");
}else if (args.length < 4){
throw new RuntimeException("太少的参数,需要至少四个");
}else if (args.length > 4){
throw new RuntimeException("太多的参数,需要至多四个");
}

var vec = new Vector<Integer>();

try {
for(String element : args){
vec.add(Integer.parseInt(element));
}
} catch (NumberFormatException e) {
throw new RuntimeException(e);
}

return vec;
}
}

运行效果

输入参数 5 6 3 7

1
2
3
4
(5.000000,6.000000,3.000000),高度为7.000000
表面积是:42.0
体积是:197.92033717615698
底面积是:28.274333882308138

Java小实验-多态的应用
http://cvrain.cloudvl.cn/2023/11/14/Java/java-example-1/
作者
ClaudeRainer
发布于
2023年11月14日
许可协议