Java教程

java班级类拓展 两门成绩 子类小组

本文主要是介绍java班级类拓展 两门成绩 子类小组,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

java班级类拓展 两门成绩 子类小组

一、基础性题目

将实验一的学生成绩题目进行扩展,要求:

创建一个班级类的子类,兴趣小组类;
class InGroup extends Class
新增成员变量:兴趣小组名称,活动时间(星期几表示),活动地点;
String groupName;
String activityTime;
String place;
新增成员方法:设置组名,设置活动时间,设置活动地点,显示组名+组员列表+活动时间+活动地点,显示小组平均成绩,最高分,最低分及分数分布统计;
public String getGroupName() {
    return groupName;
}
// 使用setter,getter
public void setGroupName(String groupName) {
    this.groupName = groupName;
}
重写平均成绩方法(去掉一个最高,去掉一个最低,取平均)。
String getAverage() {
        sum[j] = sum[j] - maxScore - minScore;
    return sum[j] / (s.length - 2);
}

课程拓展到多门课程。

class Student {
    String name;
    Grade[] grade;

    Student(String name, Grade[] grade) {
        this.name = name;
        this.grade = grade;
    }
}

//课程拓展到多门课程。
class Grade {
    int courseNo;
    String courseName;
    double courseScore;

    Grade(int courseNo, String courseName, double courseScore) {
        this.courseNo = courseNo;
        this.courseName = courseName;
        this.courseScore = courseScore;
    }
}

classDiagram

Student String name Grade[] grade Grade int courseNo String courseName double courseScore Class Student[] s String getAverage String getMax String getMin String gradeDistribution InGroup +String groupName +String activityTime +String place setter() and getter() String getAverage String memberlist String showDetails String showGrade

综上所述,程序源代码为

class Student {
    String name;
    Grade[] grade;

    Student(String name, Grade[] grade) {
        this.name = name;
        this.grade = grade;
    }
}

//课程拓展到多门课程。
class Grade {
    int courseNo;
    String courseName;
    double courseScore;

    Grade(int courseNo, String courseName, double courseScore) {
        this.courseNo = courseNo;
        this.courseName = courseName;
        this.courseScore = courseScore;
    }
}

class Class {
    Student[] s;

    Class(Student[] s) {
        this.s = s;
    }


    String getAverage() {
        double[] sum = {0, 0};
        for (int j = 0; j < 2; j++) {
            for (int i = 0; i < s.length; i++) {
                sum[j] += s[i].grade[j].courseScore;
            }
        }
        return "课程号:" + s[0].grade[0].courseNo + " 课程名:" + s[0].grade[0].courseName +
                "的平均成绩为:" + sum[0] / s.length +
                "\n\t课程号:" + s[0].grade[1].courseNo + " 课程名:" + s[0].grade[1].courseName +
                "的平均成绩为:" + sum[1] / s.length;
    }

    String getMax() {
        double[] maxScore = {0, 0};
        for (int j = 0; j < 2; j++) {
            for (int i = 0; i < s.length; i++) {
                if (s[i].grade[0].courseScore > maxScore[j]) {
                    maxScore[j] = s[i].grade[j].courseScore;
                }
            }
        }
        return "课程号:" + s[0].grade[0].courseNo + " 课程名:" + s[0].grade[0].courseName +
                "的最高分:" + maxScore[0] +
                "\n\t课程号:" + s[0].grade[1].courseNo + " 课程名:" + s[0].grade[1].courseName +
                "的最高分:" + maxScore[1];
    }

    public String getMin() {
        double[] minScore = {100, 100};
        for (int j = 0; j < 2; j++) {
            for (int i = 0; i < s.length; i++) {
                if (s[i].grade[1].courseScore < minScore[0]) {
                    minScore[0] = s[i].grade[j].courseScore;
                }
            }
        }
        return "课程号:" + s[0].grade[0].courseNo + " 课程名:" + s[0].grade[0].courseName +
                "的最高分:" + minScore[0] +
                "\n\t课程号:" + s[0].grade[1].courseNo + " 课程名:" + s[0].grade[1].courseName +
                "的最高分:" + minScore[1];
    }

    String gradeDistribution() {
        int[] excellence = {0, 0};
        int[] good = {0, 0};
        int[] pass = {0, 0};
        int[] failure = {0, 0};
        for (int j = 0; j < 2; j++) {
            for (int i = 0; i < s.length; i++) {
                int grade = (int) (s[i].grade[j].courseScore / 10);
                switch (grade) {
                    case 10:
                    case 9:
                        excellence[j]++;
                        break;
                    case 8:
                    case 7:
                        good[j]++;
                        break;
                    case 6:
                        pass[j]++;
                        break;
                    default:
                        failure[j]++;
                        break;
                }
            }
        }
        return "课程号:" + s[0].grade[0].courseNo + " 课程名:" + s[0].grade[0].courseName +
                "分数分布——优秀:" + excellence[0] + " 良好:" + good[0] + " 及格:" + pass[0] + " 不及格:" + failure[0] +
                "\n\t课程号:" + s[0].grade[1].courseNo + " 课程名:" + s[0].grade[1].courseName +
                "分数分布——优秀:" + excellence[1] + " 良好:" + good[1] + " 及格:" + pass[1] + " 不及格:" + failure[1];
    }
}

class InGroup extends Class {
    String groupName;
    String activityTime;
    String place;

    InGroup(Student[] s) {
        super(s);
    }


    public String getGroupName() {
        return groupName;
    }

    public void setGroupName(String groupName) {
        this.groupName = groupName;
    }

    public String getActivityTime() {
        return activityTime;
    }

    public void setActivityTime(String activityTime) {
        this.activityTime = activityTime;
    }

    public String getPlace() {
        return place;
    }

    public void setPlace(String place) {
        this.place = place;
    }

    // 重写平均成绩方法(去掉一个最高,去掉一个最低,取平均)。
    @Override
    String getAverage() {

        double[] sum = {0, 0};
        for (int j = 0; j < 2; j++) {
            for (int i = 0; i < s.length; i++) {
                sum[j] += s[i].grade[j].courseScore;
            }
            double maxScore = 0;
            for (int i = 0; i < s.length; i++) {
                if (s[i].grade[0].courseScore > maxScore) {
                    maxScore = s[i].grade[j].courseScore;
                }
            }
            double minScore = 100;
            for (int i = 0; i < s.length; i++) {
                if (s[i].grade[0].courseScore > minScore) {
                    minScore = s[i].grade[j].courseScore;
                }
            }
            sum[j] = sum[j] - maxScore - minScore;
        }

        return "课程号:" + s[0].grade[0].courseNo + " 课程名:" + s[0].grade[0].courseName +
                "的平均成绩为:" + sum[0] / (s.length - 2) +
                "\n\t课程号:" + s[0].grade[1].courseNo + " 课程名:" + s[0].grade[1].courseName +
                "的平均成绩为:" + sum[1] / (s.length - 2);
    }

    String memberlist() {
        StringBuilder namelist = new StringBuilder(" 小组成员:");
        for (Student i : s) {
            namelist.append(i.name + ' ');
        }
        String menmberList = namelist.toString();
        return menmberList;
    }

    String showDetails() {
        return "组名:" + groupName + memberlist() + "\n\t活动时间:" + activityTime +
                " 活动地点:" + place;
    }

    String showGrade() {
        return "小组平均成绩:" + getAverage() + "\n最高分:" + getMax() +
                "\n最低分:" + getMin() + "\n分数分布统计:" + gradeDistribution();
    }
}

public class E2T1 {
    public static void main(String[] args) {
        int stuNum = 4;
        String[] name = {"wei", "zhao", "wang", "zhang"};
        int courseNum = 2;
        int[] courseNo = {1, 2};
        String courseName[] = {"Chinese", "Math"};
        double[][] courseScore = {{100, 90}, {81, 79}, {69, 86}, {81, 72}};

        Student[] stu = new Student[stuNum];

        for (int i = 0; i < stuNum; i++) {
            Grade grade[] = new Grade[courseNum];
            for (int j = 0; j < courseNum; j++) {
                grade[j] = new Grade(courseNo[j], courseName[j], courseScore[i][j]);
            }
            stu[i] = new Student(name[i], grade);
        }
        
        InGroup g = new InGroup(stu);
        g.setGroupName("第一组");
        g.setActivityTime("周五");
        g.setPlace("图书馆");
        System.out.println("小组详情:\n" + g.showDetails());
        System.out.println("\n小组成绩:\n" + g.showGrade());
    }
}
小组详情:
组名:第一组 小组成员:wei zhao wang zhang 
    活动时间:周五 活动地点:图书馆

小组成绩:
小组平均成绩:课程号:1 课程名:Chinese的平均成绩为:65.5
    课程号:2 课程名:Math的平均成绩为:68.5
最高分:课程号:1 课程名:Chinese的最高分:100.0
    课程号:2 课程名:Math的最高分:90.0
最低分:课程号:1 课程名:Chinese的最高分:72.0
    课程号:2 课程名:Math的最高分:100.0
分数分布统计:课程号:1 课程名:Chinese分数分布——优秀:1 良好:2 及格:1 不及格:0
    课程号:2 课程名:Math分数分布——优秀:1 良好:3 及格:0 不及格:0

二、开放性题目(反编译)

class Circle {
    double r;

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

    double getArea() {
        return Math.PI * r * r;
    }
}

public class E2T2 {
    public static void main(String[] args) {
        Circle c = new Circle(1.0);
        System.out.println(c);
    }
}

程序直接打印对象c,输出的结果是其所属类名@地址。

(1)请通过反编译工具,找出原因。

提示:所有的Java类都继承于java.lang.Object。

通过反编译工具反编译E2T2.class,Cricle.class。得到如下代码

class Circle {
  double r;
  
  Circle(double paramDouble) {
    this.r = paramDouble;
  }
  
  double getArea() {
    return Math.PI * this.r * this.r;
  }
}
public class E2T2 {
  public static void main(String[] paramArrayOfString) {
    Circle circle = new Circle(1.0D);
    System.out.println(circle);
  }
}

使用了String[] paramArrayOfString做默认参数,而传入数据则是double 1.0,顾需要使用toString转格式

查阅java.lang.Object源代码,发现其中的toString()方法

public String toString() {
    return getClass().getName() + "@" + Integer.toHexString(hashCode());
}

程序应该是调用父类的toString()完成的输出

(2)修改程序,让它打印对象c时,输出Circle类的对象c的面积吗?且保留2位小数。

已经知道程序的输出所调用的方法后,我们可以重写toString()方法完成输出,再使用String.format()保留小数

class Circle {
    double r;

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

    String getArea() {
        return String.format("%.2f", Math.PI * r * r);
    }

    @Override //重写的方法
    public String toString() {
        return getClass().getName() + "@" + Integer.toHexString(hashCode()) +"\n圆的面积为:"+  getArea();
    }
}

public class E2T2 {
    public static void main(String[] args) {
        Circle c = new Circle(1.0);
        System.out.println(c);
    }
}

程序输出结果为:

Circle@1b6d3586
圆的面积为:3.14
这篇关于java班级类拓展 两门成绩 子类小组的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!