Java experiment – define a class Student that represents student information, the requirements are as follows:

Table of contents

1. Topic requirements

2. Problem-solving ideas

3. Specific code

4. Run screenshots


1. Topic requirements

Define a class Student that represents student information. The requirements are as follows:

(1) Member variables of class Student:

sNO represents the student number; sName represents the name; sSex represents the gender; sAge represents the age; sJava: represents the Java course score.

(2) Construction method of class Student with parameters:

In the constructor, the assignment of member variables is completed through formal parameters.

(3) Method members of class Student:

getNo(): get student number;

getName(): get the name;

getSex(): get gender;

getAge() gets age;

getJava(): Get Java course grades

setNo(): Set student number;

setName(): Set name;

setSex(): set gender;

setAge(); set age

setJava(): Set Java course grades

According to the definition of class Student, create five objects of this class, output the information of each student, calculate and output the average of the five students’ Java language scores, and calculate and output the maximum and minimum values ​​of their Java language scores.

2. Problem-solving ideas

First define the Student class, then define the member variables sNO, sName, sSex, sAge, sJava, then set up the parameterized constructor, set the set and get methods for each member variable, and finally construct the sample object through the constructor, and Perform some operations (sorting of Java scores).

3. Specific code

public class Student {
    private int sNo;//student number
    private String sName;//name
    private String sSex;//Gender
    private int sAge;//Age
    private int sJava;//Java results

    //Construction method
    public Student(int sNo, String sName, String sSex, int sAge, int sJava) {
        this.sNo=sNo;
        this.sName=sName;
        this.sSex=sSex;
        this.sAge=sAge;
        this.sJava=sJava;
    }

    public int getNo() {//Get student number
        return sNo;
    }

    public void setNo(int sNo) {//Set student number
        this.sNo = sNo;
    }

    public String getName() {//Get the name
        return sName;
    }

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

    public String getSex() {//Get gender
        return sSex;
    }

    public void setSex(String sSex) {//Set gender
        this.sSex = sSex;
    }

    public int getAge() {//Get age
        return sAge;
    }

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

    public int getJava() {//Get Java course grades
        return sJava;
    }

    public void setJava(int sJava) {//Set Java course scores
        this.sJava = sJava;
    }

    public void print(){
        System.out.printf("Student ID: %d\tName: %s\tGender: %s\tAge: %d\tJava course grade: %d\n", sNo, sName, sSex, sAge, sJava );
    }

}
public class StudentDemo1 {
    public static void main(String[] args) {
        //Define five student objects
        Student stu1=new Student(1,"Lettuce and shrimp","Male",18,85);
        Student stu2=new Student(2,"Kumako","woman",19,95);
        Student stu3=new Student(3,"Nezumi","Woman",20,97);
        Student stu4=new Student(4," fish boy "," male ",19,98);
        Student stu5=new Student(5,"Paparazzi","Male",21,90);

        //Print the relevant information of these five students
        stu1.print();
        stu2.print();
        stu3.print();
        stu4.print();
        stu5.print();

        //Save Java scores into array
        int[] java=new int[5];
        java[0]= stu1.getJava();
        java[1]= stu2.getJava();
        java[2]= stu3.getJava();
        java[3]= stu4.getJava();
        java[4]= stu5.getJava();

        int sumJava=java[0];//The sum of five students’ Java scores
        int maxJava = java[0];//Highest score
        int minJava=java[0];//Minimum score
        int n=5;
        for(int i=0;i<java.length-1;i++){
            sumJava+=java[i+1];
            if(maxJava<java[i]){
                maxJava=java[i];
            }
            if(minJava>java[i]){
                minJava=java[i];
            }
        }
        int avgJava=sumJava/n;//Average score
        System.out.println("The average score is: "+ avgJava);
        System.out.println("The highest score is: "+maxJava);
        System.out.println("The minimum score is: "+minJava);

    }
}

4. Run screenshots

Related Posts

Huawei Cloud IOT Android application development detailed tutorial

[Take you step by step to learn JavaSE] Part 3: Operators and program logic control

Why do many people with computer majors switch careers and quit computer science after graduation? [Revealing the truth]

IDEA’s high-quality plug-in Translation has updated its translation engine following the IDEA 2022.3 version.

[SpringBoot Series] How SpringBoot integrates SSMP

JAVA Exercise 73-III. Printing Binary Tree from Top to Bottom III

Alibaba Yu Jun: DingTalk should take the path of low-code practice

Design and implementation of epidemic drug purchase and delivery system based on Java+SpringBoot+vue+element

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

*