java 3-类

实验报告

目的

掌握类的定义,构造方法,初始化;

理解类与对象,并能够加以区分;

实验内容与设计思想

设计思路

(1) Person类的定义与使用

(2) 类的简单使用

(3) 三角形与圆形

(4) 初始化块

(5) 二次方程式求解

(6) 矩阵四则运算

(7) 学生成绩管理

主要代码结构 {#主要代码结构 .unnumbered}

实验使用环境

软件
java version “18.0.2”
EclipseIDE 2022-06

平台
WIN10

实验步骤和调试过程

4.1 Person类的定义与使用 {#person类的定义与使用 .unnumbered}

需求

(1) 定义一个有关人的Person类,内含姓名name、性别sex、年龄age属性,所有的变量必须是私有的(private)。

(2) 设计一无参构造方法初始化Person的age属性为0、sex属性为0(或者其他,视定义的数据类型而定)、name属性为空字符串(“”)。

(3) 使用这些构造方法对Person类进行初始化。

(4) 其他类只能通过该类的setter、getter方法修改与获取属性。

(5) 改写toString()方法,显示其所有属性。另外定义测试类TestPerson,对Person类进行测试。

代码

package exp1;

public class Person {
    private String name;
    private String sex;
    private int age;

    public Person() {
        this.name = "";
        this.sex = "";
        this.age = 0;
    }

    public Person(String name, String sex, int age)
    {
        this.name = name;
        this.sex = sex;
        this.age = age;
    }

    public void setName(String name)
    {
        this.name = name;
    }

    public void setSex(String sex)
    {
        this.sex = sex;
    }

    public void setAge(int age)
    {
        this.age = age;
    }

    public String getName()
    {
        return this.name;
    }

    public String getSex()
    {
        return this.sex;
    }

    public int getAge(){
        return this.age;
    }

    @Override
    public String toString()
    {
        return "Person{" +
                "name='" + name + '\'' +
                ", sex='" + sex + '\'' +
                ", age=" + age +
                '}';
    }
}
package exp1;

public class TestPerson
{
    public static void main(String[] args)
    {
        Person person = new Person();
        System.out.println(person.toString());

        person.setName("纪惠婷");
        person.setSex("女");
        person.setAge(20);
        System.out.println(person.toString());

        System.out.println(person.getName());
        System.out.println(person.getSex());
        System.out.println(person.getAge());

        Person person2 = new Person("test", "男", 1);
        System.out.println(person2.toString());

    }
}

实验结果

思考

private修饰的属性,访问起来到底有何限制?如果Person p = new
Person();可以直接使用p.name访问p的name属性吗?

public具有最大的访问权限,可以访问任何一个在classpath下的类、接口、异常等。它往往用于对外的情况,也就是对象或类对外的一种接口的形式。
protected主要的作用就是用来保护子类的。它的含义在于子类可以用它修饰的成员,其他的不可以,它相当于传递给子类的一种继承的东西
default有时候也称为friendly,它是针对本包访问而设计的,任何处于本包下的类、接口、异常等,都可以相互访问,即使是父类没有用protected修饰的成员也可以。
private访问权限仅限于类的内部,是一种封装的体现,例如,大多数成员变量都是修饰符为private的,它们不希望被其他任何外部的类访问。
  • public:可以被所有其他类所访问

  • private:只能被自己访问和修改

  • protected:自身、子类及同一个包中类可以访问

  • default:同一包中的类可以访问,声明时没有加修饰符,认为是friendly。

属性值同一个类同一个包子类外部包
public
protexted 
default  
private   

可以直接使用p.name访问p的name属性吗?如果name属性是private,则只能在同一类中访问。如果是default,可以在同一个包访问,如果是protected,自身、子类及同一个包中类可以访问。如果是public,则可以被其他所有类访问。

4.2 类的简单使用 {#类的简单使用 .unnumbered}

需求

(1) 定义一个类LeapYear,其中有一个非静态方法public boolean
isLeapYear(int year),用于判断年份year是否为闰年。

(2) 编写测试类,在程序主入口main()方法中创建一个LeapYear对象,调用isLeapYear方法并输出(测试数据自定,也可以由键盘输入)。

代码

package exp2;

public class LeapYear {
    public boolean isLeapYear(int year)
    {
        return year % 4 == 0 && year % 100 != 0 || year % 400 == 0;
    }
}
package exp2;

import static java.lang.Math.random;

public class TestLeapYear
{
    public static void main(String[] args)
    {
        LeapYear leapYear = new LeapYear();
        int[] test = {2021, 2022, 2023, 2024};
        for (int year : test)
        {
            if (leapYear.isLeapYear(year))
                System.out.println(year+"年是闰年");
            else
                System.out.println(year+"年不是闰年");
        }
    }
}

实验结果

思考

思考:静态方法与非静态方法使用起来有什么区别?

调用对象不同 引用变量不同静态方法是使用static关键字修饰的方法,属于类的,不属于对象; 非静态方法是不使用static关键字修饰的普通方法,属于对象,不属于类。
调用方法不同静态方法可以直接调用,类名调用和对象调用; 非静态方法只能通过对象调用。 静态方法可以直接调用,类名调用和对象调用。(类名.方法名/对象名.方法名) 但是非静态方法只能通过对象调用。(对象名.方法名)
生命周期不同静态方法的生命周期跟相应的类一样长,静态方法和静态变量会随着类的定义而被分配和装载入内存中。一直到线程结束,静态属性和方法才会被销毁。 (即静态方法属于类) 非静态方法的生命周期和类的实例化对象一样长,只有当类实例化了一个对象,非静态方法才会被创建,而当这个对象被销毁时,非静态方法也马上被销毁。(即非静态方法属于对象)
4.3三角形与圆形 {#三角形与圆形 .unnumbered}

需求

(1)编写长方形类Rectangle、三角形类Triangle、圆形类Circle,各有各的属性。

(2)在三个类中分别定义求周长的方法public double
getPerimeter()、求面积的方法getArea()。

(3)新建测试类TestShape,针对每个类建立两个对象(共六个对象,成员值自定或随机),并将这些对象的所有面积与周长相加统计输出。

代码

package exp3;

import static java.lang.Math.random;

public class Circle
{
    private double r;

    //构造
    public Circle()
    {
        r = random();
    }

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

    //getter && setter
    public double getR()
    {
        return r;
    }

    public void setR(double r)
    {
        this.r = r;
    }

    //周长
    public double getPerimeter()
    {
        return 2 * Math.PI * r;
    }

    //面积
    public double getArea()
    {
        return Math.PI * r * r;
    }
}
package exp3;

import static java.lang.Math.random;

public class Rectangle
{
    private double width, height;

    //构造
    public Rectangle()
    {
        width = random();
        height = random();
    }

    public Rectangle(double a, double b)
    {
        width = a;
        height = b;
    }

    //getter && setter
    public double getWidth()
    {
        return width;
    }

    public void setWidth(double width)
    {
        this.width = width;
    }

    public double getHeight()
    {
        return height;
    }

    public void setHeight(double height)
    {
        this.height = height;
    }

    //周长
    public double getPerimeter()
    {
        return 2 * (width + height);
    }

    //面积
    public double getArea()
    {
        return width * height;
    }
}
package exp3;

import java.util.Arrays;

import static java.lang.Math.random;
import static java.lang.Math.sqrt;

public class Triangle
{
    private double a, b, c;

    //构造
    public Triangle()
    {
        a = random();
        b = random();
        c = random();
    }

    public Triangle(double a, double b, double c)
    {
        this.a = a;
        this.b = b;
        this.c = c;
    }


    //getter && setter
    public double getA()
    {
        return a;
    }

    public void setA(double a)
    {
        this.a = a;
    }

    public double getB()
    {
        return b;
    }

    public void setB(double b)
    {
        this.b = b;
    }

    public double getC()
    {
        return c;
    }

    public void setC(double c)
    {
        this.c = c;
    }

    //周长
    public double getPerimeter()
    {
        return a + b + c;
    }

    //面积
    public double getArea()
    {
        double p = (a + b + c) / 2;

        double[] length = {a, b, c};
        Arrays.sort(length);
        if (length[0] + length[1] <= length[2])
            return 0;
        return sqrt(p * (p - a) * (p - b) * (p - c));
    }
}
package exp3;

import java.util.ArrayList;
import java.util.Objects;

public class Test
{
    public static void main(String[] args)
    {
        double perimeter = 0, area = 0;
        Rectangle rectangle1 = new Rectangle();
        Rectangle rectangle2 = new Rectangle();
        Triangle triangle1 = new Triangle();
        Triangle triangle2 = new Triangle();
        Circle circle1 = new Circle();
        Circle circle2 = new Circle();

        perimeter += rectangle1.getPerimeter() + rectangle2.getPerimeter();
        perimeter += triangle1.getPerimeter() + triangle2.getPerimeter();
        perimeter += circle1.getPerimeter() + circle2.getPerimeter();

        area += rectangle1.getArea() + rectangle2.getArea();
        area += triangle1.getArea() + triangle1.getArea();
        area += circle1.getArea() + circle2.getArea();

        System.out.println("周长:" + perimeter);
        System.out.println("面积:" + area);
    }
}

实验结果

思考

对面积、周长进行相加统计的代码是否过于复杂?有无改进空间?

可以在每个形状类中添加static double sumArea = 0和static double
sumPerimeter = 0记录目前为止的所有的周长与面积和,然后添加方法public
static double addArea(double area)和public static double
addPerimeter(double
perimeter)对将每个形状的周长和面积加到sumArea中。但是这样不规范。

可以建立Shape类作为父类,而Rectangle、Triangle、Circle作为子类。这样就可以使用数组或者ArrayList,将三个类型的对象全部存入。而求周长和面积的时候只要通过循环叠加即可得到周长和与面积和。这样做的好处是:用一个for循环即可计算得到周长和与面积和,方便简约。对于多于6个的对象,也会更加方便。

4.4初始化块 {#初始化块 .unnumbered}

需求

(1) 为题1中的Person类添加两个属性:身份证号int
id(不要求与真正的身份证类似,这里的id只是一个简单的整型值,每个Person对象均不同即可。如,可以从1开始编号,第一个Person对象的id为1,第二个为2,以此类推),静态变量(类变量)static
int count(用于记录身份证号自动增长的历史)。

(2) 在构造方法中对id进行初始化,并使得每个生成对象的id均不同。

(3) 为Person类创建两个构造方法:第一个为包含两个参数的构造方法,可根据提供的"name"、"age"参数,对姓名、年龄属性进行初始化;第二个构造方法包含三个参数name,age,sex,使用构造方法重载技术,调用第一个构造方法,并额外对sex属性进行初始化。

(4) 覆盖Person类中的toString方法,输出Person的基本信息。选择适当的测试语句,测试本题中的改动。

代码

package exp4;

class PersonAdd
{
    private String name;
    private String sex;
    private int age;
    private int id;
    static private int count = 0;

    public PersonAdd()
    {
        this.name = "";
        this.sex = "";
        this.age = 0;
        this.id = ++count;
    }

    public PersonAdd(String name, int age)
    {
        this();
        this.name = name;
        this.age = age;
    }

    public PersonAdd(String name, int age, String sex)
    {
        this(name, age);
        this.sex = sex;
    }

    public String getName()
    {
        return name;
    }

    public void setName(String name)
    {
        this.name = name;
    }

    public String getSex()
    {
        return sex;
    }

    public void setSex(String sex)
    {
        this.sex = sex;
    }

    public int getAge()
    {
        return age;
    }

    public void setAge(int age)
    {
        this.age = age;
    }

    public int getId()
    {
        return id;
    }


    public static int getCount()
    {
        return count;
    }


    @Override
    public String toString()
    {
        return "PersonAdd{" +
                "name='" + name + '\'' +
                ", sex='" + sex + '\'' +
                ", age=" + age +
                ", id=" + id +
                '}';
    }
}
package exp4;


public class TestPersonAdd
{
    public static void main(String[] args)
    {
        PersonAdd PersonAdd = new PersonAdd();
        System.out.println(PersonAdd.toString());

        PersonAdd.setName("纪惠婷");
        PersonAdd.setSex("女");
        PersonAdd.setAge(21);
        System.out.println(PersonAdd.toString());

        System.out.println(PersonAdd.getName());
        System.out.println(PersonAdd.getSex());
        System.out.println(PersonAdd.getAge());
        System.out.println(PersonAdd.getId());

        PersonAdd PersonAdd2 = new PersonAdd("test", 1);
        System.out.println(PersonAdd2.toString());
    }
}

实验结果

4.5二次方程式求解 {#二次方程式求解 .unnumbered}

需求

为二次方程ax2+bx+c=0设计一个名为QuadraticEquation的类。这个类包括:

(1)代表三个系数的私有数据成员a,b和c;

(2)一个参数为a,b,c的构造方法;

(3)一个名为getDiscriminant()的方法,返回判别式:b2-4ac;

(4)名为getRoot1()和getRoot2()的方法,返回二次方程的两个根:r1=(-b+(b2-4ac)1/2)/2a,r2=(-b-(b2-4ac)1/2)/2a。这两个方法只有在判别式为非负数时才有用。如果判别式为负,则返回0。

实现这个类。编写一个测试程序,提示用户输入a,b和c的值,然后显示判别式的值。如果判别式为正数,显示两个根;如果判别式为0,则显示一个根,否则显示"The
equation has no roots."字样。

代码

package exp5;

import static java.lang.Math.sqrt;

public class QuadraticEquation {
    private double a, b, c;

    public QuadraticEquation(double a, double b, double c) {
        this.a = a;
        this.b = b;
        this.c = c;
    }

    public double getA() {
        return a;
    }

    public void setA(double a) {
        this.a = a;
    }

    public double getB() {
        return b;
    }

    public void setB(double b) {
        this.b = b;
    }

    public double getC() {
        return c;
    }

    public void setC(double c) {
        this.c = c;
    }

    public double getDiscriminant() {
        return b * b - 4 * a * c;
    }

    public double getRoot1() {
        if(getDiscriminant()<0)
            return 0;
        return (-b + sqrt(b * b - 4 * a * c)) / 2 * a;
    }

    public double getRoot2() {
        if(getDiscriminant()<0)
            return 0;
        return (-b - sqrt(b * b - 4 * a * c)) / 2 * a;
    }

}
package exp5;

import java.util.Scanner;

public class TestQuadraticEquation
{

    static public void main(String[] args)
    {
        System.out.println("Equation, please enter the three coefficients:");
        try (Scanner scanner = new Scanner(System.in))
        {
            double a = scanner.nextDouble();
            double b = scanner.nextDouble();
            double c = scanner.nextDouble();
            QuadraticEquation quadraticEquation = new QuadraticEquation(a, b, c);

            if (quadraticEquation.getDiscriminant() > 0)
                System.out.println("two roots:" + quadraticEquation.getRoot1() + " " + quadraticEquation.getRoot2());
            else if (quadraticEquation.getDiscriminant() == 0)
                System.out.println("one root:" + quadraticEquation.getRoot1());
            else
                System.out.println("The equation has no roots.");
        }
    }
}

实验结果

4.6矩阵四则运算 {#矩阵四则运算 .unnumbered}

需求

定义矩阵类Matrix,包括:

(1) 代表矩阵的行数rows(或m)、列数cols(或n),以及二维数组data;

(2) 一个参数为rows,cols的构造方法,实现初始化操作,并将矩阵元素全部置为0;

(3) public void setElement(int row, int col, double
value);方法,用于设置第row行,第col列元素的值;

(4) public Matrix add(Matrix
m);方法,实现当前矩阵与m矩阵相加,并返回新的矩阵;若无法相加,则返回null;

(5) public Matrix minus(Matrix
m);方法,实现当前矩阵减去m矩阵,并返回新的矩阵;若无法相减,则返回null;

(6) public Matrix multiple(Matrix
m);方法,实现当前矩阵乘以m矩阵,并返回新的矩阵;若无法相乘,则返回null;

(7) public Matrix transposition();方法,实现矩阵转置,并返回新的矩阵;

(8) public void display();方法,打印当前矩阵。

(9) 实现该类。编写一个测试程序,随机生成矩阵元素或者由程序中用常量设置(可不必由键盘输入),测试上述四则运算,打印运算结果。

代码

package exp6;

public class Matrix
{
    private int rows;
    private int cols;
    private double[][] data;

    //构造
    public Matrix(int rows, int cols)
    {
        this.rows = rows;
        this.cols = cols;
        this.data = new double[rows][cols];
        for (int i = 0; i < rows; i++)
            for (int j = 0; j < cols; j++)
                this.data[i][j] = 0;
    }

    //getter && setter
    public int getCols()
    {
        return cols;
    }

    public double getData(int i, int j)
    {
        return data[i][j];
    }

    public void setData(int i, int j, double data)
    {
        this.data[i][j] = data;
    }

    public int getRows()
    {
        return rows;
    }

    public void setElement(int row, int col, double value)
    {
        if (row < 0 || row >= this.rows || col >= this.cols || col < 0)
            return;
        this.data[row][col] = value;
    }

    public Matrix add(Matrix m)
    {
        if (m == null || m.rows != rows || m.cols != cols)
            return null;
        Matrix new_matrix = new Matrix(this.rows, this.cols);
        for (int i = 0; i < rows; i++)
            for (int j = 0; j < cols; j++)
                new_matrix.data[i][j] = m.data[i][j] + this.data[i][j];
        return new_matrix;
    }


    public Matrix minus(Matrix m)
    {
        if (m == null || m.rows != rows || m.cols != cols)
            return null;
        Matrix new_matrix = new Matrix(this.rows, this.cols);
        for (int i = 0; i < rows; i++)
            for (int j = 0; j < cols; j++)
                new_matrix.data[i][j] = this.data[i][j] - m.data[i][j];
        return new_matrix;
    }

    public Matrix multiple(Matrix m)
    {
        if (m == null || m.rows != cols || m.cols != rows)
            return null;
        Matrix new_matrix = new Matrix(this.rows, m.cols);
        for (int i = 0; i < rows; i++)
            for (int j = 0; j < m.cols; j++)
                for (int k = 0; k < cols; k++)
                    new_matrix.data[i][j] += this.data[i][k] * m.data[k][j];
        return new_matrix;
    }

    public Matrix transposition()
    {
        Matrix new_matrix = new Matrix(this.cols, this.rows);
        for (int i = 0; i < cols; i++)
            for (int j = 0; j < rows; j++)
                new_matrix.data[i][j] = data[j][i];
        return new_matrix;
    }
}
package exp6;

import static java.lang.Math.random;


public class TestMatrix
{
    public static void makeMatrix(Matrix matrix)

    {
        for (int i = 0; i < matrix.getRows(); i++)
        {
            for (int j = 0; j < matrix.getCols(); j++)
            {
                matrix.setElement(i, j, random());
            }
        }
    }

    public static void display(Matrix x)
    {
        for (int i = 0; i < x.getRows(); i++)
        {
            for (int j = 0; j < x.getCols(); j++)
                System.out.printf("%.2f   ", x.getData(i, j));
            System.out.println();
        }
    }

    public static void main(String[] args)
    {
        Matrix a = new Matrix(4, 4);
        Matrix b = new Matrix(4, 4);

        makeMatrix(a);
        System.out.println("a");
        display(a);

        makeMatrix(b);
        System.out.println("b");
        display(b);

        System.out.println("add:");
        Matrix addMatrix = a.add(b);
        display(addMatrix);

        System.out.println("minus");
        Matrix minusMatrix = a.minus(b);
        display(minusMatrix);

        System.out.println("multiple");
        Matrix multipleMatrix = a.multiple(b);
        display(multipleMatrix);

        System.out.println("transposition");
        Matrix transposeMatrix = a.transposition();
        display(transposeMatrix);
    }
}

实验结果

a
0.01   0.03   0.33   0.46   
0.56   0.13   0.31   0.61   
0.38   0.31   0.13   0.69   
0.25   0.67   0.70   0.84   
b
0.13   0.73   0.26   0.50   
0.35   0.22   0.08   0.74   
0.06   0.40   0.68   0.03   
0.95   0.30   0.38   0.59   
add:
0.14   0.76   0.59   0.95   
0.91   0.35   0.39   1.35   
0.44   0.71   0.81   0.71   
1.20   0.98   1.08   1.43   
minus
-0.12   -0.70   0.07   -0.04   
0.21   -0.10   0.23   -0.13   
0.32   -0.09   -0.55   0.66   
-0.69   0.37   0.32   0.24   
multiple
0.46   0.29   0.40   0.31   
0.71   0.75   0.60   0.75   
0.81   0.61   0.47   0.83   
1.10   0.87   0.91   1.14   
transposition
0.01   0.56   0.38   0.25   
0.03   0.13   0.31   0.67   
0.33   0.31   0.13   0.70   
0.46   0.61   0.69   0.84   
4.7学生成绩管理 {#学生成绩管理 .unnumbered}

需求

(1) 创建Student类,包含name,math,java,ds,avg,total等属性,并编写器setter/getter方法。并编写两个构造方法,一个构造方法包含除avg,total外的所有属性,另一个构造方法包含所有属性。编写相应的toString()方法。

(2) 创建一Students.txt文件。

(3) 使用上一次的编写的public static String[]
readStudentsFromFile(String
fileName),把文件中的信息读取到String数组中。

(4) 编写public static Student[] makeStudentFromString(String[]
students),实现将字符串转化成学生对象。

(5) 编写一个方法对返回的Student数组中的所有学生的平均成绩进行排序,并输出。

代码

package exp7;

public class Student
{
    private String name;
    private int math, java, ds, total, avg;

    public Student(String name, String math, String java, String ds)
    {
        this.name = name;
        this.math = Integer.parseInt(math);
        this.java = Integer.parseInt(java);
        this.ds = Integer.parseInt(ds);
        this.total = this.math + this.java + this.ds;
        this.avg = total / 3;
    }

    public Student(String name, String math, String java, String ds, int avg, int total)
    {
        this(name, math, java, ds);
        this.avg = avg;
        this.total = total;
    }

    public int getMath()
    {
        return math;
    }

    public int getJava()
    {
        return java;
    }

    public int getDs()
    {
        return ds;
    }

    public int getTotal()
    {
        return total;
    }

    public int getAvg()
    {
        return avg;
    }

    public void setName(String name)
    {
        this.name = name;
    }

    public void setMath(int math)
    {
        this.math = math;
    }

    public void setJava(int java)
    {
        this.java = java;
    }

    public void setDs(int ds)
    {
        this.ds = ds;
    }

    public void setTotal(int total)
    {
        this.total = total;
    }

    public void setAvg(int avg)
    {
        this.avg = avg;
    }

    @Override
    public String toString()
    {
        return "Student{" +
                "name='" + name + '\'' +
                ", math=" + math +
                ", java=" + java +
                ", ds=" + ds +
                ", total=" + total +
                ", avg=" + avg +
                '}';
    }
}
package exp7;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.Scanner;


public class TestStudent
{
    //写入文件
    public static void CreateStudent(int n, String filename) throws FileNotFoundException
    {
        String[] students = new String[n];
        for (int i = 0; i < n; i++)
        {
            String name = "名字" + i;
            int math = (int) (Math.random() * 101), java = (int) (Math.random() * 101), ds = (int) (Math.random() * 101);
            String str = name + " " + math + " " + java + " " + ds + " 0 0";
            students[i] = str;
        }
        PrintWriter out = new PrintWriter(filename);
        for (String each : students)
            out.println(each);
        out.close();
    }

    //读取文件
    public static Student[] readStudentsFromFile(String fileName) throws FileNotFoundException
    {
        ArrayList<String> data = new ArrayList<>();
        Scanner in = new Scanner(new File(fileName));
        while (in.hasNextLine())
        {
            String line = in.nextLine();
            data.add(line);
        }
        in.close();

        Student[] students = new Student[data.size()];
        for (int i = 0; i < data.size(); i++)
        {
            Scanner lineScanner = new Scanner(data.get(i));
            lineScanner.useDelimiter(" ");
            String name = lineScanner.next();
            String math = lineScanner.next();
            String java = lineScanner.next();
            String ds = lineScanner.next();
            students[i] = new Student(name, math, java, ds);
        }
        return students;
    }

    //按成绩排序
    public static void sortStudentsByGrade(Student[] students)
    {
        Arrays.sort(students, new Comparator<Student>()
        {
            @Override
            public int compare(Student o1, Student o2)
            {
                return o1.getAvg() - o2.getAvg();
            }
        });
    }

    public static void main(String[] args) throws FileNotFoundException
    {
        String fileName = "Students.txt";
        CreateStudent(10, fileName);

        System.out.println("Read from file:");
        Student[] students = readStudentsFromFile(fileName);
        for (Student student : students)
            System.out.println(student.toString());

        System.out.println("After sort by grade:");
        sortStudentsByGrade(students);
        for (Student student : students)
            System.out.println(student.toString());
    }
}

实验结果

Read from file:
Student{name='名字0', math=20, java=62, ds=32, total=114, avg=38}
Student{name='名字1', math=44, java=6, ds=56, total=106, avg=35}
Student{name='名字2', math=26, java=93, ds=29, total=148, avg=49}
Student{name='名字3', math=59, java=67, ds=53, total=179, avg=59}
Student{name='名字4', math=41, java=67, ds=75, total=183, avg=61}
Student{name='名字5', math=7, java=75, ds=91, total=173, avg=57}
Student{name='名字6', math=79, java=31, ds=12, total=122, avg=40}
Student{name='名字7', math=82, java=21, ds=73, total=176, avg=58}
Student{name='名字8', math=83, java=41, ds=71, total=195, avg=65}
Student{name='名字9', math=57, java=1, ds=27, total=85, avg=28}
After sort by grade:
Student{name='名字9', math=57, java=1, ds=27, total=85, avg=28}
Student{name='名字1', math=44, java=6, ds=56, total=106, avg=35}
Student{name='名字0', math=20, java=62, ds=32, total=114, avg=38}
Student{name='名字6', math=79, java=31, ds=12, total=122, avg=40}
Student{name='名字2', math=26, java=93, ds=29, total=148, avg=49}
Student{name='名字5', math=7, java=75, ds=91, total=173, avg=57}
Student{name='名字7', math=82, java=21, ds=73, total=176, avg=58}
Student{name='名字3', math=59, java=67, ds=53, total=179, avg=59}
Student{name='名字4', math=41, java=67, ds=75, total=183, avg=61}
Student{name='名字8', math=83, java=41, ds=71, total=195, avg=65}

实验小结

1.实验中遇到的问题及解决过程 {#实验中遇到的问题及解决过程 .unnumbered}

没有什么问题。

2. 实验中产生的错误及原因分析

student构造方法里面调用另一个student构造方法的方法不能是:new
student(…);

而应该是 this.student(…)

3.实验体会和收获 {#实验体会和收获 .unnumbered}

掌握了类的定义,构造方法;

理解类与对象,并能够加以区分;

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值