• java作业之学生类


    题目、编写一个Java应用程序。设计一个学生类Students,包括属性有:序号,学号,姓名,性别,专业,三门课程成绩(数学,计算机,英语);包括方法有:求成绩总分,成绩平均分,除序号外各个属性的赋值方法,各个属性值的获取方法等等。说明:每创建一个Students对象,则序号值增1;第一个学生对象的序号值为1

    编写一个主类StudentsDemo,在主方法中创建5个Students类的对象,按以下要求输出相关信息:

    (1)各课程最高/最低分同学的序号、学号、姓名和该门课程分数

    (2)总分最高/最低同学的序号、学号、姓名和所有课程分数

    (3)输入一个学号或者姓名,若存在这样的学号或姓名则输出该学生的所有信息,否则输出信息不存在。

    package StudentsDemo;
    import java.util.Scanner;
    public class StudentsDemo {
    
      public static void main(String[] args) {
        //创建学生信息
        Student[] a = new Student[5];//以数组的形式存储5个学生类
        int i = 0;
        for (i = 0; i < 5; i++) {
          a[i] = new Student();
          a[i].math = (int) (Math.random() * 100);//产生一个0到1之间的随机数*100
          a[i].english = (int) (Math.random() * 100);//随机数录入成绩
          a[i].comp = (int) (Math.random() * 100);
          a[i].gender = "男";
          a[i].major = "计算机类";
    //      System.out.println(a[i].nunber);  验证编号
        }
        a[0].stu_num = "125";
        a[1].stu_num = "244";
        a[2].stu_num = "334";
        a[3].stu_num = "442";
        a[4].stu_num = "542";//学号
    
        a[0].name = "张三";
        a[1].name = "里面";
        a[2].name = "黎明";
        a[3].name = "李华";
        a[4].name = "丽华";//起名
    
        int max1 = 0, min1 = 0;
        int max2 = 0, min2 = 0;
        int max3 = 0, min3 = 0;
        int MAX = 0;
        for (i = 0; i < 5; i++) {
          if (a[i].gettotal() > a[MAX].gettotal()) {
            MAX = i;
          }
          if (a[i].math > a[max1].math) {
            max1 = i;
          }
    
          if (a[i].math < a[min1].math) {
            min1 = i;
          }
    
          if (a[i].english > a[max2].english) {
            max2 = i;
          }
    
          if (a[i].english < a[max2].english) {
            min2 = i;
          }
    
          if (a[i].comp > a[max3].comp) {
            max3 = i;//寻找分高的人
          }
    
          if (a[i].comp < a[min3].comp) {
            min3 = i;
          }
        }
        Put(a, "英语", max2);
        Put(a, "数学", max1);
        Put(a, "计算机", max3);
    
        //输出学习信息
        Put2(a, "英语", min2);
        Put2(a, "数学", min1);
        Put2(a, "计算机", min3);
    
        Put(a, "", MAX);
        System.out.println("英语分 " + a[MAX].getEnglish());
        System.out.println("数学分为  " + a[MAX].getMath());
        System.out.println("计算机分数为  " + a[MAX].getComp());
        //输出总分最高
    
        //查找学生
        int cont = -1;
        Scanner in = new Scanner(System.in);
        System.out.println("请输入学号或姓名");
        String search = in.nextLine();
        for (i = 0; i < 5; i++) {//equals比较字符串是否相等
          if (a[i].name.equals(search) || a[i].stu_num.equals(search)) {
            cont = i;
            break;
          }
        }
    
        if (cont == -1) {
          System.out.println("信息不存在");
        } else {
          System.out.println("姓名是" + a[cont].name);
          System.out.println("序号是" + a[cont].nunber);
          System.out.println("学号是" + a[cont].stu_num);
          System.out.println("性别是" + a[cont].gender);
          System.out.println("专业是" + a[cont].major);
          System.out.println("英语成绩是" + a[cont].getEnglish());
          System.out.println("数学成绩是" + a[cont].math);
          System.out.println("计算机成绩是" + a[cont].comp);
          System.out.println("总分是" + a[cont].gettotal());
          System.out.println("平均分是" + a[cont].getaver());
    
        }
    
    
      }
    
      static void Put(Student[] ss, String course, int num) {
        System.out.println(course + "成绩最高的是" + ss[num].nunber + "号学生");
        System.out.println("学号是" + ss[num].stu_num);
        System.out.println("姓名是" + ss[num].name);
        if (course.equals("english")) {//分号不能少
          System.out.println(course + "成绩是" + ss[num].english);
        } else if (course.equals("comp")) {
          System.out.println(course + "成绩是" + ss[num].comp);
        } else if (course.equals("math")) {
          System.out.println(course + "成绩是" + ss[num].math);
        }
      }
    
      static void Put2(Student[] ss, String course, int num) {
        System.out.println(course + "成绩最低的是" + ss[num].nunber + "号学生");
        System.out.println("学号是" + ss[num].stu_num);
        System.out.println("姓名是" + ss[num].name);
        if (course.equals("english")) {
          System.out.println(course + "成绩是" + ss[num].english);
        } else if (course.equals("comp")) {
          System.out.println(course + "成绩是" + ss[num].comp);
        } else if (course.equals("math")) {
          System.out.println(course + "成绩是" + ss[num].math);
        }
      }
    }
    package StudentsDemo;
    public class Student {
    
          static int NUM = 1;
          int nunber;
          String stu_num;
          String name;
          String gender;
          double math;
          double comp;
          double english;
          String major;
    
          Student() {//构造方法
            nunber = NUM++;
          }
    
          public double gettotal() {//获取总分
            return this.math + this.english + this.comp;
          }
    
          public double getaver() {
            return ((this.comp + this.english + this.math) * 1.0) / 3;
          }
    
          public String getName() {
            return name;
          }
    
          public void setName(String name) {
            this.name = name;
          }
    
          public double getComp() {
            return comp;
          }
    
          public void setComp(double comp) {
            this.comp = comp;
          }
    
          public double getEnglish() {
            return english;
          }
    
          public void setEnglish(double english) {
            this.english = english;
          }
    
          public double getMath() {
            return math;
          }
    
          public void setMath(double math) {
            this.math = math;
          }
    
          public String getGender() {
            return gender;
          }
    
          public void setGender(String gender) {
            this.gender = gender;
          }
    
          public String getStu_num() {
            return stu_num;
          }
    
          public void setStu_num(String stu_num) {
            this.stu_num = stu_num;
          }
    
          public String getMajor() {
            return major;
          }
    
          public void setMajor(String major) {
            this.major = major;
          }
    
        }
    //另一种方法
    public class Student {    
        static int id=0;//学生序号
        //用类变量保留相应科目最高(低)分的ID,便于对象共享
        static int mathScoreMaxID=0;
        static int mathScoreMinID=0;
        static int englishScoreMaxID=0;
        static int englishScoreMinID=0;
        static int computerScoreMaxID=0;
        static int computerScoreMinID=0;    
        static int totalScoreMaxID=0;
        static int totalScoreMinID=0;
        //用类变量保留相应科目最高(低)分,便于对象共享
        private static float mathScoreMax=0;//数学科目最高分
        private static float englishScoreMax=0;
        private static float computerScoreMax=0;
        private static float mathScoreMin=100;
        private static float englishScoreMin=100;
        private static float computerScoreMin=100;
        private static float totalScoreMax=0;
        private static float totalScoreMin=0;
        //将成员变量定义为私有的,体现面向对象的封装性
        private String studentNumber;//学生学号
        private String studentName;
        private String studentSex;
        private String studentMajor;
        private float mathScore;
        private float englishScore;
        private float computerScore;
        private float totalScore;
        private float averageScore;
        //构造方法,创建对象时调用
        Student(String number,String name,String sex,String major){
            id++;//每创建一个Students对象,则序号值增1;
            studentNumber=number;
            studentName=name;
            studentSex=sex;
            studentMajor=major;                
        }
    //给每个成员变量(各个科目成绩)赋值
        void setAllScore(int id,float math,float english,float computer){
            setMathScore(id,math);
            setEnglishScore(id,english);
            setComputerScore(id,computer);
            setTotalScore();
            setAverageScore();        
        }
    //给数学成绩赋值,用类变量保存最大(小)值以及相应序号
        void setMathScore(int id,float math){
            mathScore=math;
            if(math>mathScoreMax){//获得数学分最高的学生序号
                mathScoreMax=math;
                mathScoreMaxID=id;
            }
            if(math<mathScoreMin){//获得数学分最低的学生序号
                mathScoreMin=math;
                mathScoreMinID=id;
            }
        }    
        void setEnglishScore(int id ,float english ){
            englishScore=english;
            if(english>englishScoreMax){//获得英语分最高的学生序号
                englishScoreMax=english;
                englishScoreMaxID=id;
            }
            if(english<englishScoreMin){//获得英语分最低的学生序号
                englishScoreMin=english;
                englishScoreMinID=id;
            }
        }    
        void setComputerScore(int id,float computer){
            computerScore=computer;        
            if(computer>computerScoreMax){//获得计算机分最高的学生序号
                computerScoreMax=computer;
                computerScoreMaxID=id;
            }
            if(computer<computerScoreMin){//获得计算机分最低的学生序号
                computerScoreMin=computer;
                computerScoreMinID=id;
            }
        }
        void setTotalScore(){
            totalScore=mathScore+englishScore+computerScore;
            if(totalScore>totalScoreMax){
                totalScoreMax=totalScore;
                totalScoreMaxID=id;
            }
            if(totalScore<totalScoreMin){
                totalScoreMin=totalScore;
                totalScoreMinID=id;
            }        
        }
        void setAverageScore(){
            averageScore=totalScore/3;            
        }
        //学号、姓名查找方法
        boolean findStudentNumber(String number){
            return studentNumber.equals(number);
        }
        boolean findStudentName(String name){
            return studentName.equals(name);
        }    
    //以下方法主要由于外部访问内部私有变量
        float getMathScore(){
            return mathScore;
        }
        float getEnglishScore(){
            return englishScore;
        }
        float getComputerScore(){
            return computerScore;
        }    
        String getStudentNumber(){
            return studentNumber;
        }
        String getStudentName(){
            return studentName;
        }
        String getStudentSex(){
            return studentSex;
        }
        String getStudentMajor(){
            return studentMajor;
        }    
    }
  • 相关阅读:
    PAT (Basic Level) Practise 1013 数素数
    PAT (Basic Level) Practise 1014 福尔摩斯的约会
    codeforces 814B.An express train to reveries 解题报告
    KMP算法
    rsync工具
    codeforces 777C.Alyona and Spreadsheet 解题报告
    codeforces 798C.Mike and gcd problem 解题报告
    nginx + tomcat多实例
    MongoDB副本集
    指针的艺术(转载)
  • 原文地址:https://www.cnblogs.com/zhuimingzhenbai/p/12607814.html
Copyright © 2020-2023  润新知