内部类的使用

实验题目:

        3个学生(小红、小明、小牛) 参加Java,SQL,J2EE的考试,上述课程类型分为考查课,考试课,考查课。
        考试课最终成绩计算方法:出勤 10%+作业 10%+实验10%+期末 70%

        考查课最终成绩计算方法:出勤 20%+作业 10%+实验10%+期末 60%

        实践课最终成绩为五级制:优(100-90)良(89-80)中(79-70)差(69-60)

        请实现,在传入各科成绩后,输出总分的效果。

方法一:

实验源代码:

import java.util.ArrayList;
import java.util.List;

public class Student {
    //这里定义了一个Student类,包含了姓名、性别和一个Subject类型的List。
    String name;//姓名
    String sex;//性别
    List<Subject> subject = new ArrayList<Subject>();
    
    //这是一个无参构造函数。
    public Student() {
        // TODO 自动生成的构造函数存根
    }
    
    //这是一个有参构造函数,用于初始化姓名和性别。
    public Student (String name, String sex) {
        this.name = name;
        this.sex = sex;
    }
    
    //这是主函数,用于测试代码。创建了两个Student对象,分别为s1和s2,然后调用了inputScore方法和printInfo方法。
    public static void main(String[] args) {
        // TODO 自动生成的方法存根
        Student s1 = new Student("小红","女");
        s1.inputScore("Java", "考察", 90, 85, 75, 80);
        s1.inputScore("SQL ", "考试", 80, 90, 82, 75);
        s1.inputScore("J2EE", "考察", 78, 70, 70, 71);
        s1.printInfo();
        System.out.println();
        System.out.println("***************************");
        System.out.println();
        
        Student s2 = new Student("小明","男");
        s2.inputScore("Java", "考察", 86, 67, 71, 70);
        s2.inputScore("SQL ", "考试", 77, 70, 85, 66);
        s2.inputScore("J2EE", "考察", 88, 74, 68, 80);
        s2.printInfo();
        System.out.println();
        System.out.println("***************************");
        System.out.println();
        
        Student s3 = new Student("小牛","男");
        s3.inputScore("Java", "考察", 85, 66, 78, 70);
        s3.inputScore("SQL ", "考试", 87, 79, 75, 76);
        s3.inputScore("J2EE", "考察", 98, 84, 69, 60);
        s3.printInfo();
        
    }
    
    // 成员内部类 课程类
    //这是一个成员内部类Subject,包含了课程名、课程类型、SubjectType类型的对象、出勤、作业、实验和期末成绩。
    //setType方法用于设置SubjectType对象的typeName和比例。
    class Subject {
        String subjectName;//课程名
        String type;//课程类型
        SubjectType subType = new SubjectType();//静态内部类对象
        int atten;//出勤
        int assign;//作业
        int lab;//实验
        int finalExam;//期末
        
        //第一行代码将一个变量subType的属性typeName设置为另一个变量type的值。
        //第二行代码调用subType的一个方法setRatio(),该方法会根据subType的typeName属性设置subType的ratio属性的值。具体实现细节需要查看setRatio()方法的代码。
        void setType(){
            subType.typeName = type;
            subType.setRatio();
        }
        
    }
    
    // 静态内部类 课程性质
    //这是一个静态内部类SubjectType,包含了课程类型、出勤比例、作业比例、实验比例和期末比例。
    //setRatio方法用于根据课程类型设置出勤比例和期末比例。
    static class SubjectType {
        String typeName;//类型
        double attenRatio;//出勤比例
        static final double assignRatio = 0.1;//作业比例,静态final
        static final double labRatio = 0.1;//实验比例,静态final
        double finalRatio;//期末比例

        void setRatio() {
            //设置比例的方法
            if ("考察".equals(typeName)) {
                attenRatio = 0.2;
                finalRatio = 0.6;
            }else if ("考试".equals(typeName)) {
                attenRatio = 0.1;
                finalRatio = 0.7;
            }
        }
    }
    
    //导入学生成绩方法,用于向subject列表中添加Subject对象。
    public void inputScore(String name,String type,int atten,int assign,int lab,int finalExam){
        Student s = new Student();
        Student.Subject sub = s.new Subject();
        sub.subjectName = name;
        sub.type = type;
        sub.setType();
        sub.atten = atten;
        sub.assign = assign;
        sub.lab = lab;
        sub.finalExam = finalExam;
        subject.add(sub);
        
    }
    //打印方法

    void printInfo(){
        class calcScore{ //局部内部类
            //用于计算总分
            int calcTotal(Subject sb){  
                double a = sb.atten * sb.subType.attenRatio;
                double b = sb.assign * SubjectType.assignRatio;
                double c = sb.lab * SubjectType.labRatio;
                double d = sb.finalExam * sb.subType.finalRatio;
                return (int)(a+b+c+d);
            }    
        }
        calcScore cs = new calcScore();
        System.out.println("姓名:"+name+"    性别:"+sex);
        System.out.println("课程      "+ "性质  "+ "出勤  "+ "         作业  "+ "        实验  "+  "        期末  "+ "        总分 ");
        System.out.println("===============================");
        for(int i=0;i<subject.size();i++) {
            Subject sb = ((ArrayList<Subject>) subject).get(i);
            System.out.println(sb.subjectName+" "+sb.type+"   "+sb.atten+"     "+sb.assign+"     "+sb.lab+"     "+sb.finalExam+"     "+cs.calcTotal(sb));
        }
     }

    

}
运行结果图:

 

方法二:

实验源代码:

import java.util.ArrayList;
import java.util.List;

public class ExamScore {
    private List<Student> students;//学生列表
    
    public ExamScore(){
        students = new ArrayList<>();//初始化学生列表
    }
    
    public void addStudent(Student student){//添加学生
        students.add(student);
    }
    public void printScore(){//打印成绩单
        for(Student student : students){///遍历学生列表
            System.out.println(student);//打印学生信息
        }
    }
    
    public static class Student{
        private String name;
        private String gender;
        private List<Course> courses;
        
        public Student(String name,String gender){//构造函数
                this.name=name;//初始化姓名
                this.gender =gender;//初始化性别
                courses = new ArrayList<>();//初始化课程列表
    }
        public void addCourse(Course course){//添加课程
            courses.add(course);
        }
        
        public double getTotalScore(){//计算总分
            int totalScore = 0;//初始化总分
            for(Course course: courses){//遍历课程列表
                totalScore += course.getFinalScore();//累加每门课程的期未成绩
            
            }
            return totalScore;//返回总分
        }
        public String toString(){//转换为字符串
            StringBuilder sb= new StringBuilder();//创建字符串构建器
            sb.append("姓名:").append(name).append("\t性别:").append(gender).append("\n");
            sb.append("课程\t性质\t出勤\t作业\t实验\t期末\t总分\n");
            sb.append("====\t====\t====\t====\t====\t====\t====\n");
            for(Course course:courses){//遍历课程列表
                sb.append(course).append("\n");
            }
            sb.append("****************\n");
            sb.append("总分:").append(getTotalScore()).append("\n");
            return sb.toString();
            
        }
    }
    public static class Course {
        private String name ;//课程名称
        private String type ;
        private int attendanceScore ;
        private int homeworkScore ;
        private int experimentScore ;
        private int finalScore;
        
        //进行初始化
        public Course (String name,String type,int attendanceScore,int homeworkScore,int experimentScore,int finalScore){
            this.name = name;
            this.type = type;
            this.attendanceScore = attendanceScore;
            this.homeworkScore = homeworkScore;
            this.experimentScore = experimentScore;
            this.finalScore = finalScore;
        }
        public double getFinalScore() {
            if(type.equals("考试")){
                    return attendanceScore * 0.1 + homeworkScore * 0.1 + experimentScore * 0.1 + finalScore * 0.7;
            }else if(type.equals("考查")){
                    return attendanceScore * 0.2 + homeworkScore * 0.1 + experimentScore * 01 + finalScore * 0.6;
            }else{
                if(finalScore >= 90){
                    return 5;
                }else if(finalScore >= 80){
                    return 4;
                }else if(finalScore >= 70){
                    return 3;
                }else if(finalScore >= 60){
                    return 2;
                }else{
                    return 1;
                }
                
            }
        }
        public String toString(){
            return name +"\t" +type + "\t" +attendanceScore + "\t" +homeworkScore + "\t" +experimentScore + "\t" +finalScore+ "\t" +getFinalScore() ;
    }
    }
}

public class Test{
    public static void main(String[] args) {
        ExamScore examScore = new ExamScore();
        
        ExamScore.Student student1 = new ExamScore.Student("小红","女");
        student1.addCourse(new ExamScore.Course("Java","考查",90,85,75,80));
        student1.addCourse(new ExamScore.Course("SQL","考试",80,90,82,75));
        student1.addCourse(new ExamScore.Course("J2EE","考查",78,70,70,71));
        examScore.addStudent(student1);//
        
        ExamScore examScore2 = new ExamScore();
        
        ExamScore.Student student2 = new ExamScore.Student("小明","男");
        student2.addCourse(new ExamScore.Course("Java","考查",80,95,85,80));
        student2.addCourse(new ExamScore.Course("SQL","考试",85,80,92,85));
        student2.addCourse(new ExamScore.Course("J2EE","考查",79,80,90,81));
        examScore2.addStudent(student2);
    
        ExamScore examScore3 = new ExamScore();
        ExamScore.Student student3 = new ExamScore.Student("小牛","男");
        student3.addCourse(new ExamScore.Course("Java","考查",80,89,75,90));
        student3.addCourse(new ExamScore.Course("SQL","考试",88,91,72,79));
        student3.addCourse(new ExamScore.Course("J2EE","考查",88,80,90,81));
        examScore3.addStudent(student3);
        
        examScore.printScore();
        examScore2.printScore();
        examScore3.printScore();
    }
}
运行结果图:

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值