一 ,实验目的
- 设计类,并画出UML类图
- 实现UML中的类
- 使用类开发应用程序
二、实验内容
1、(P305, 9.1)【矩形类Rectangle】遵照9.2节中Circle类的例子,设计一个名为Rectangle的类表示矩形。这个类包括:
- 两个名为width和height的double型数据域,它们分别表示矩形的宽和高。width和height的默认值都为1。
- 创建默认矩形的无参构造方法。
- 创建width和height为指定值的矩形的构造方法。
- 一个名为getArea()的方法返回这个矩形的面积。
- 一个名为getPerimeter()的方法返回矩形周长。
画出该类的UML图并实现这个类。编写一个测试程序,创建两个Rectangle对象:一个矩形的宽为4高为40,另一个矩形的宽为3.5高为35.9。依次显示每个矩形的宽、高、面积和周长。
1.1该程序的UML图如下:
1.2对程序的测试结果如下:
由测试结果可知,该程序完全符合实验要求。
1.3心得体会
本程序中刚开始对默认值有点迷,因为在c++中类的成员变量是不能赋初始值的,但是java是允许的。这显示的java的方便和强大,对于这种细节方面的东西以后一定要不懂就查。
1.4源代码如下:
public class Rectangle {
double width = 1;
double heigth = 1;
Rectangle( ){
width = 1;
heigth = 1;
}
Rectangle(double width,double heigth){
this.heigth = heigth;
this.width = width;
}
public double getArea(){
return heigth * width;
}
public double getPerimeter(){
return (heigth + width) * 2;
}
public static void main(String[] args) {
Rectangle rec1 = new Rectangle(4,40);
Rectangle rec2 = new Rectangle(3.5,35.9);
System.out.println("矩形1的宽为"+rec1.width+" 长为"+rec1.heigth+" 面积为:"+rec1.getArea()+" 周长为"+rec1.getPerimeter());
System.out.println("矩形2的宽为"+rec2.width+" 长为"+rec2.heigth+" 面积为:"+rec2.getArea()+" 周长为"+rec2.getPerimeter());
}
}
2. (P307, 9.8)【风扇类Fan】设计一个名为Fan的类表示一个风扇。这个类包括:
- 三个名为SLOW、MEDIUM和FAST而值为1、2、3的常量表示风扇的速度。
- 一个名为speed的int类型私有数据域表示风扇的速度(默认值为SLOW)。
- 一个名为on的boolean类型私有数据域表示风扇是否打开(默认值为false)。
- 一个名为radius的double类型私有数据域表示风扇的半径(默认值为5)。
- 一个名为color的String类型数据域表示风扇的颜色(默认值为blue)。
- 这四个数据域的访问器和修改器。
- 一个创建默认风扇的无参构造方法。
- 一个名为toString()的方法返回描述风扇的字符串。如果风扇是打开的,那么该方法在一个组合的字符串中返回风扇的速度、颜色和半径。如果风扇没有打开,该方法返回一个由“fan is off”和风扇颜色、半径组成的字符串。
画出该类的UML图。实现这个类。编写一个测试程序,创建两个Fan对象。将第一个对象设置为最大速度、半径为10、颜色为yellow、状态为打开。将第二个对象设置为中等速度、半径为5、颜色为blue、状态为关闭。通过调用它们的toString方法显示这些对象。
2.1该程序的UML图如下:
2.2对程序的测试结果如下:
由测试结果可知,该程序完全符合实验要求。
2.3心得体会
这个实验的UML类图有点把握不住,首先是常量在UML图中要怎么表示,常量是静态变量的一种,所以也要加下划线。另外发现IDEA是可以自动生成UML图的,可以根据那个图来画UML图就比较方便了。
2.4源代码如下:
- public class Fan {
- public final static int SLOW = 1;
- final static int MEDIUM = 2;
- final static int FAST = 3;
- private int speed = SLOW;
- private boolean on = false;
- private double radius = 5;
- public boolean isOn() {
- return on;
- }
- public void setOn(boolean on) {
- this.on = on;
- }
- public double getRadius() {
- return radius;
- }
- public void setRadius(double radius) {
- this.radius = radius;
- }
- public String getColor() {
- return color;
- }
- public void setColor(String color) {
- this.color = color;
- }
- public String color = "blue";
- Fan(){}
- @Override
- public String toString() {
- if(this.on)
- return "fan{" +
- "speed=" + speed +
- ", radius=" + radius +
- ", color='" + color + '\'' +
- '}';
- else
- return "fan is off"+" and fan{"+
- "speed=" + speed +
- ",radius=" + radius +
- ", color='" + color + '\'' +
- '}';
- }
- public int getSpeed() {
- return speed;
- }
- public void setSpeed(int speed) {
- this.speed = speed;
- }
- public static void main(String[] args) {
- Fan fan1 = new Fan();
- fan1.setColor("yellow");
- fan1.setSpeed(Fan.FAST);
- fan1.setOn(true);
- Fan fan2 = new Fan();
- fan2.setColor("blue");
- fan2.setSpeed(Fan.MEDIUM);
- fan2.setOn(false);
- System.out.println("显示fan1");
- System.out.println(fan1.toString());
- System.out.println("显示fan2");
- System.out.println(fan2.toString());
- }
- }
3.(P308,9.10*)【二次方程式】为二次方程式ax2+bx+c=0设计一个名为QuadraticEquation的类。这个类包括:
- 代表三个系数的私有数据域a、b、c。
- 一个参数为a、b、c的构造方法。
- a、b、c的三个get方法。
- 一个名为getDiscriminant()的方法返回判别式,b2-4ac。
- 一个名为getRoot1()和getRoot2()的方法返回等式的两个根。
这些方法只有在判别式为非负数时才有用。如果判别式为负,方法返回0。
画出该类的UML图。实现这个类。编写一个测试程序,提示用户输入a、b、c的值,然后显示判别式的结果。如果判别式为正数,显示两个根;如果判别式为0,显示一个根;否则,显示“The equation has no roots”。
3.1该程序的UML图如下:
3.2对程序的测试结果如下:
1)当方程有两根时:
2)当方程没有根时:
3)当方程只有一个根时:
4)当二次项系数为零时,会提示用户输入错误:
由测试结果可知,对于所有情况,该程序都完全符合实验要求。
3.3心得体会
这次差一点就漏了二次项系数为零时的情况的,起初输入零时,程序运行直接异常,于是马上修改,最后得到正确的程序。
3.4源代码如下:
- public class QuadraticEquation {
- private double a;
- private double c;
- private double b;
- public QuadraticEquation(double a, double c, double b) {
- this.a = a;
- this.c = c;
- this.b = b;
- }
- public double getDiscriminant(){
- return getB() * getB() - 4*getA()*getC();
- }
- public double getA() {
- return a;
- }
- public double getRoot1(){
- double t =getDiscriminant();
- if(t < 0)
- return 0;
- else
- return (-b + Math.sqrt(t)) / (2*a);
- }
- public double getRoot2(){
- double t =getDiscriminant();
- if(t < 0)
- return 0;
- else
- return (-b - Math.sqrt(t)) / (2*a);
- }
- public double getC() {
- return c;
- }
- public double getB() {
- return b;
- }
- public void setABC(double a,double b,double c) {
- this.a = a;
- this.c= c;
- this.b = b;
- }
- public static void main(String[] args) {
- Scanner input =new Scanner(System.in);
- System.out.print("请以此输入二次方程的二次项系数,一次项系数和常数项系数:");
- double a =input.nextDouble();
- double b =input.nextDouble();
- double c =input.nextDouble();
- if(a == 0)
- {
- System.out.print("二次项系数不为零,请重新输入:");
- a =input.nextDouble();
- }
- QuadraticEquation equ =new QuadraticEquation(a,b,c);
- double t = equ.getDiscriminant();
- if(t > 0)
- System.out.println("该方程有两根,分别为:"+equ.getRoot1()+" "+equ.getRoot2());
- else if(t == 0)
- System.out.println("该方程有一个根,为:"+equ.getRoot2());
- else
- System.out.println("该方程没有根");
- }
- }
4.(P308, 9.13**)【位置类】设计一个名为Location的类,定位二维数组中的最大值及其位置。这个类包括公共的数据域row、column和maxValue,二维数组中的最大值及其下标用double型的maxValue以及int型的row和column存储。
编写下面的方法,返回一个二维数组中最大值的位置。
public static Location locateLargetst(double[][] a)
返回值是一个Location的实例。编写一个测试程序,提示用户输入一个二维数组,然后显示这个数组中的最大元素及下标。运行实例如下:
输入二维数组的行数和列数: 3 4
输入数组:
23.5 35 2 10
4.5 3 45 3.5
35 44 5.5 9.6
最大元素及其下标是: 45 在(1,2)
4.1对程序的测试结果如下:
1)对于题目给定测试数据,程序运行结果为:
2)当在极端情况,即二维数组只含一个数时:
3)当二维数组中包含多个最大数据时,程序返回列标和行标之后最小的那个:
由测试结果可知,该程序完全符合实验要求。
4.2心得体会
通过这个程序,暴露并改变了我的一个错误观念:一个Java文件中是可以包含多个类的,但是public的那个类必须和文件名同名。以前并未注意到这个细节。
4.3源代码如下:
- public class Location {
- public int row;
- public int column;
- public double maxValue;
- public Location(int row, int column, double maxValue) {
- this.row = row;
- this.column = column;
- this.maxValue = maxValue;
- }
- public static Location locateLargetst(double[][] a){
- int r = 0, c = 0;
- double max = a[0][0];
- for (int i = 0; i < a.length; i++) {
- for (int j = 0; j < a[i].length; j++) {
- if(max < a[i][j])
- {
- max = a[i][j];
- r =i;
- c = j;
- }
- }
- }
- Location loc1 = new Location(r,c,max);
- return loc1;
- }
- public static void main(String[] args) {
- Scanner input = new Scanner(System.in);
- System.out.println("输入二维数组的行数和列数:");
- int r =input.nextInt();
- int c =input.nextInt();
- double[][] a = new double [r][c];
- System.out.println("输入数组:");
- for (int i = 0; i < r; i++) {
- for (int j = 0; j < c; j++) {
- a[i][j] = input.nextDouble();
- }
- }
- Location loc1 = locateLargetst(a);
- System.out.println("最大元素的为:"+loc1.maxValue+" 坐标为:("+loc1.row+","+loc1.column+")");
- }
- }