Java_SSD3_实验3《对象和类》

一 ,实验目的

  1. 设计类,并画出UML类图
  2. 实现UML中的类
  3. 使用类开发应用程序
     

二、实验内容

   1、(P305, 9.1)【矩形类Rectangle】遵照9.2节中Circle类的例子,设计一个名为Rectangle的类表示矩形。这个类包括:

  1. 两个名为width和height的double型数据域,它们分别表示矩形的宽和高。width和height的默认值都为1。
  2. 创建默认矩形的无参构造方法。
  3. 创建width和height为指定值的矩形的构造方法。
  4. 一个名为getArea()的方法返回这个矩形的面积。
  5. 一个名为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的类表示一个风扇。这个类包括:

  1. 三个名为SLOW、MEDIUM和FAST而值为1、2、3的常量表示风扇的速度。
  2. 一个名为speed的int类型私有数据域表示风扇的速度(默认值为SLOW)。
  3. 一个名为on的boolean类型私有数据域表示风扇是否打开(默认值为false)。
  4. 一个名为radius的double类型私有数据域表示风扇的半径(默认值为5)。
  5. 一个名为color的String类型数据域表示风扇的颜色(默认值为blue)。
  6. 这四个数据域的访问器和修改器。
  7. 一个创建默认风扇的无参构造方法。
  8. 一个名为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源代码如下:

 

  1. public class Fan {  
  2.     public final static int SLOW = 1;  
  3.     final static int MEDIUM = 2;  
  4.     final static int FAST = 3;  
  5.     private int speed = SLOW;  
  6.     private boolean on = false;  
  7.     private double radius = 5;  
  8.      
  9.     public boolean isOn() {  
  10.         return on;  
  11.     }  
  12.     public void setOn(boolean on) {  
  13.         this.on = on;  
  14.     }  
  15.     public double getRadius() {  
  16.         return radius;  
  17.     }  
  18.     public void setRadius(double radius) {  
  19.         this.radius = radius;  
  20.     }  
  21.   
  22.     public String getColor() {  
  23.         return color;  
  24.     }  
  25.   
  26.     public void setColor(String color) {  
  27.         this.color = color;  
  28.     }  
  29.   
  30.     public  String color = "blue";  
  31.     Fan(){}  
  32.   
  33.     @Override  
  34.     public String toString() {  
  35.         if(this.on)  
  36.         return "fan{" +  
  37.                 "speed=" + speed +  
  38.                 ", radius=" + radius +  
  39.                 ", color='" + color + '\'' +  
  40.                 '}';  
  41.         else  
  42.             return "fan is off"+" and fan{"+  
  43.                     "speed=" + speed +  
  44.                     ",radius=" + radius +  
  45.                     ", color='" + color + '\'' +  
  46.                     '}';  
  47.     }  
  48.   
  49.     public int getSpeed() {  
  50.         return speed;  
  51.     }  
  52.   
  53.     public void setSpeed(int speed) {  
  54.         this.speed = speed;  
  55.     }  
  56.   
  57.     public static void main(String[] args) {  
  58.         Fan fan1 = new Fan();  
  59.         fan1.setColor("yellow");  
  60.         fan1.setSpeed(Fan.FAST);  
  61.         fan1.setOn(true);  
  62.   
  63.         Fan fan2 = new Fan();  
  64.         fan2.setColor("blue");  
  65.         fan2.setSpeed(Fan.MEDIUM);  
  66.         fan2.setOn(false);  
  67.   
  68.         System.out.println("显示fan1");  
  69.         System.out.println(fan1.toString());  
  70.         System.out.println("显示fan2");  
  71.          System.out.println(fan2.toString());  
  72.   
  73.     }  
  74. }  

3.(P308,9.10*)【二次方程式】为二次方程式ax2+bx+c=0设计一个名为QuadraticEquation的类。这个类包括:

  1. 代表三个系数的私有数据域a、b、c。
  2. 一个参数为a、b、c的构造方法。
  3. a、b、c的三个get方法。
  4. 一个名为getDiscriminant()的方法返回判别式,b2-4ac。
  5. 一个名为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源代码如下:

  1. public class QuadraticEquation {  
  2.     private double a;  
  3.     private double c;  
  4.     private double b;  
  5.   
  6.     public QuadraticEquation(double a, double c, double b) {  
  7.         this.a = a;  
  8.         this.c = c;  
  9.         this.b = b;  
  10.     }  
  11.   
  12.     public double getDiscriminant(){  
  13.         return getB() * getB() - 4*getA()*getC();  
  14.     }  
  15.     public double getA() {  
  16.         return a;  
  17.     }  
  18.     public double getRoot1(){  
  19.         double t =getDiscriminant();  
  20.         if(t < 0)  
  21.             return 0;  
  22.         else  
  23.             return (-b + Math.sqrt(t)) / (2*a);  
  24.     }  
  25.     public double getRoot2(){  
  26.         double t =getDiscriminant();  
  27.         if(t < 0)  
  28.             return 0;  
  29.         else  
  30.             return (-b - Math.sqrt(t)) / (2*a);  
  31.     }  
  32.   
  33.     public double getC() {  
  34.         return c;  
  35.     }  
  36.   
  37.     public double getB() {  
  38.         return b;  
  39.     }  
  40.   
  41.     public void setABC(double a,double b,double c) {  
  42.         this.a = a;  
  43.         this.c= c;  
  44.         this.b = b;  
  45.     }  
  46.   
  47.     public static void main(String[] args) {  
  48.         Scanner input =new Scanner(System.in);  
  49.         System.out.print("请以此输入二次方程的二次项系数,一次项系数和常数项系数:");  
  50.         double a =input.nextDouble();  
  51.         double b =input.nextDouble();  
  52.         double c =input.nextDouble();  
  53.         if(a == 0)  
  54.         {  
  55.             System.out.print("二次项系数不为零,请重新输入:");  
  56.             a =input.nextDouble();  
  57.         }  
  58.         QuadraticEquation equ =new QuadraticEquation(a,b,c);  
  59.   
  60.         double t = equ.getDiscriminant();  
  61.         if(t > 0)  
  62.             System.out.println("该方程有两根,分别为:"+equ.getRoot1()+" "+equ.getRoot2());  
  63.         else if(t == 0)  
  64.             System.out.println("该方程有一个根,为:"+equ.getRoot2());  
  65.         else  
  66.             System.out.println("该方程没有根");  
  67.   
  68.     }  
  69. }  

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源代码如下:

  1. public class Location {  
  2.     public int row;  
  3.     public int column;  
  4.     public double maxValue;  
  5.   
  6.     public Location(int row, int column, double maxValue) {  
  7.         this.row = row;  
  8.         this.column = column;  
  9.         this.maxValue = maxValue;  
  10.     }  
  11.   
  12.     public static Location locateLargetst(double[][] a){  
  13.   
  14.         int r = 0, c = 0;  
  15.         double max = a[0][0];  
  16.         for (int i = 0; i < a.length; i++) {  
  17.             for (int j = 0; j < a[i].length; j++) {  
  18.                 if(max < a[i][j])  
  19.                 {  
  20.                     max = a[i][j];  
  21.                     r =i;  
  22.                     c = j;  
  23.                 }  
  24.             }  
  25.         }  
  26.         Location loc1 = new Location(r,c,max);  
  27.         return loc1;  
  28.     }  
  29.   
  30.     public static void main(String[] args) {  
  31.         Scanner input = new Scanner(System.in);  
  32.         System.out.println("输入二维数组的行数和列数:");  
  33.         int r =input.nextInt();  
  34.         int c =input.nextInt();  
  35.         double[][] a = new double [r][c];  
  36.         System.out.println("输入数组:");  
  37.         for (int i = 0; i < r; i++) {  
  38.             for (int j = 0; j < c; j++) {  
  39.                 a[i][j] = input.nextDouble();  
  40.             }  
  41.         }  
  42.         Location loc1 = locateLargetst(a);  
  43.         System.out.println("最大元素的为:"+loc1.maxValue+"  坐标为:("+loc1.row+","+loc1.column+")");  
  44.   
  45.   
  46.     }  
  47.   
  48. }  

 

猜你喜欢

转载自blog.csdn.net/qq_45768060/article/details/105824106