Android Road 14 - Java Basic Encapsulation Case

Reading Guide

1. Case Study of Encapsulation of Student and Professional Information
2. New Demand Statistics Professionals

Case Study of Student and Professional Information Encapsulation

Subject class

package com.hala.modle;

public class Subject {

    private String SubjectName;
    private String SubjectNum;
    private int SubjectLife;


    public Subject(){

    }


    public Subject(String subjectName,String subjectNum,int subjectLife){
        this.setSubjectName(subjectName);
        this.setSubjectNum(subjectNum);
        this.setSubjectLife(subjectLife);

    }

    public String getSubjectName() {
        return SubjectName;
    }
    public void setSubjectName(String subjectName) {
        SubjectName = subjectName;
    }


    public String getSubjectNum() {
        return SubjectNum;
    }
    public void setSubjectNum(String subjectNum) {
        SubjectNum = subjectNum;
    }


    public int getSubjectLife() {
        return SubjectLife;
    }
    public void setSubjectLife(int subjectLife) {
        if(subjectLife<=0)
            return ;
        else
           SubjectLife = subjectLife;
    }


    public String info() {
        String str = "Professional information is as follows:\n Professional Name:" + this.getSubjectName() + "\n Professional Number:" + this.getSubjectNum() + "\n Professional years:"
                + this.getSubjectLife();
        return str;
    }

}

Student class

package com.hala.modle;

public class Student {
    private String num;
    private String name;
    private String sex;
    private int age;
    private Subject studentSubject;




    public Subject getStudentSubject() {
        if(this.studentSubject==null)
            this.studentSubject=new Subject();
        //The importance of parameterless construction is shown here
        return studentSubject;
    }


    public void setStudentSubject(Subject studentSubject) {
        this.studentSubject = studentSubject;
    }


    public Student() {

    }


    public Student(String num, String name, String sex, int age) {
        this.setAge(age);
        this.setName(name);
        this.setNum(num);
        this.setSex(sex);
    }

    public Student(String num, String name, String sex, int age,Subject studentSubject) {
        this.setAge(age);
        this.setName(name);
        this.setNum(num);
        this.setSex(sex);
        this.setStudentSubject(studentSubject);
    }


    public String getNum() {
        return num;
    }
    public void setNum(String num) {
        this.num = num;
    }
    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;
    }

    /**
     * 
     * @param age Age
     * Age > 0 and < 100
     * 
     */
    public void setAge(int age) {
        if(age<=0||age>=100)
            age=18;
        this.age = age;
    }

    /**
     * Introduction of students
     * @return Name, school number, gender, age
     */
    public String introduction(){
        String str = "The student information is as follows:\n Student Name:" + this.getName() + "\n Student ID:" + this.getNum() + "\n Student gender:"
                + this.getSex()+"\n Student age:"+this.getAge();
            return str;
    }



    /**
     * Introduction of students
     * @param SubjectName
     * @param SubjectLife
     * @return Name, school number, gender, age, major, length of study
     * Solution 1; What to Pass
     */
    public String introduction(String SubjectName,String SubjectLife){
        String str = "The student information is as follows:\n Student Name:" + this.getName() + "\n Student ID:" + this.getNum() + "\n Student gender:"
                + this.getSex()+"\n Student age:"+this.getAge()+"\n Major:"+SubjectName+"\n Years of study:"+SubjectLife;
            return str;
    }



    /**
     * Introduction of students
     * @param mySubject
     * @return Name, school number, gender, age, major, length of study, major number
     * Solution 2; Pass Subject in as a method parameter
     */
    public String introduction(Subject mySubject) {
        String str = "The student information is as follows:\n Student Name:" + this.getName() + "\n Student ID:" + this.getNum() + "\n Student gender:" + this.getSex()
                + "\n Student age:" + this.getAge() + "\n Learning Specialties:" + mySubject.getSubjectName() + "\n Years of study:"
                + mySubject.getSubjectLife() + "\n Professional Number" + mySubject.getSubjectNum();
        return str;
    }


    /**
     * Introduction of students
     * @return Name, school number, gender, age, major, length of study, major number
     * Solution 3: Treat Subject as a property of Student
     */
    public String introduction1s() {
        String str = "The student information is as follows:\n Student Name:" + this.getName() + "\n Student ID:" + this.getNum() + "\n Student gender:" + this.getSex()
                + "\n Student age:" + this.getAge() + "\n Learning Specialties:" + this.getStudentSubject().getSubjectName() + "\n Years of study:"
                + this.getStudentSubject().getSubjectLife() + "\n Professional Number" + this.getStudentSubject().getSubjectNum();
        return str;
    }

}

SubjectTest class

package com.hala.test;

import com.hala.modle.*;

public class SubjectTest {

    public static void main(String[] args) {

        Subject sub1=new Subject("Computer Science and Application","J0001",4);

        System.out.println(sub1.info());


    }

}

StudentTest class

package com.hala.test;

import com.hala.modle.Student;
import com.hala.modle.Subject;

public class StudentTest {

    public static void main(String[] args) {

        Student stu1=new Student("s01","Zhang San","male",18);
        System.out.println(stu1.introduction());
        System.out.println("+++++++++++++++++++++++++++++++++");
        Student stu2=new Student("s02","Li Si","female",19);
        System.out.println(stu2.introduction("Computer Science and Technology", "4 year"));
        System.out.println("+++++++++++++++++++++++++++++++++");
        Subject sub1=new Subject("Computer Science and Application","J0001",4);
        Student stu3=new Student("s03","King Five","female",18);
        System.out.println(stu3.introduction(sub1));
        System.out.println("+++++++++++++++++++++++++++++++++");
        Student stu4=new Student("s04","Zhao Six","male",20,sub1);
        System.out.println(stu4.introduction1s());

    }

}

Output Results
The student information is as follows:
Student Name: Zhang San
Student number: s01
Student gender: male
Student age: 18
+++++++++++++++++++++++++++++++++
The student information is as follows:
Student Name: Li Si
Student number: s02
Student gender: female
Student age: 19
My major is computer science and technology
Years of study: 4 years
+++++++++++++++++++++++++++++++++
The student information is as follows:
Student Name: Wang Wu
Student number: s03
Student gender: female
Student age: 18
Learning Major: Computer Science and Application
Years of study: 4
Professional Number J0001
+++++++++++++++++++++++++++++++++
The student information is as follows:
Student Name: Zhao Six
Student number: s04
Student gender: male
Student age: 20
Learning Major: Computer Science and Application
Years of study: 4
Professional Number J0001

New Demand Statistics Professionals

Subject class

package com.hala.modle;

public class Subject {

    private String SubjectName;
    private String SubjectNum;
    private int SubjectLife;
    private int studentNum;

    private Student[] myStudent; 


    public Subject(){

    }


    public Subject(String subjectName,String subjectNum,int subjectLife){
        this.setSubjectName(subjectName);
        this.setSubjectNum(subjectNum);
        this.setSubjectLife(subjectLife);

    }

    public Subject(String subjectName,String subjectNum,int subjectLife,Student[] myStudent){
        this.setSubjectName(subjectName);
        this.setSubjectNum(subjectNum);
        this.setSubjectLife(subjectLife);
        this.setMyStudent(myStudent);
    }



    public String getSubjectName() {
        return SubjectName;
    }
    public void setSubjectName(String subjectName) {
        SubjectName = subjectName;
    }


    public String getSubjectNum() {
        return SubjectNum;
    }
    public void setSubjectNum(String subjectNum) {
        SubjectNum = subjectNum;
    }


    public int getSubjectLife() {
        return SubjectLife;
    }
    public void setSubjectLife(int subjectLife) {
        if(subjectLife<=0)
            return ;
        else
           SubjectLife = subjectLife;
    }

    /**
     * Get student information for the optional course. If the array holding the student information is not initialized, initialize the array with a length of 200 first
     * @return Array holding student information
     */
    public Student[] getMyStudent() {
        if(this.myStudent==null)
            this.myStudent=new Student[200];
        //If this sentence is not set, null pointer exceptions are caused, and null pointer exceptions are often caused by uninstantiated objects
        return myStudent;
    }


    public void setMyStudent(Student[] myStudent) {
        this.myStudent = myStudent;
    }





    public int getStudentNum() {
        return studentNum;
    }


    public void setStudentNum(int studentNum) {
        this.studentNum = studentNum;
    }


    public String info() {
        String str = "Professional information is as follows:\n Professional Name:" + this.getSubjectName() + "\n Professional Number:" + this.getSubjectNum() + "\n Professional years:"
                + this.getSubjectLife();
        return str;
    }


    /**
     * 1.Implement storing student information management in an array
     * 2.Reset the number of students
     * @param Student Information Management
     */
    public void addStudent(Student stu){
        for(int i=0;i<this.getMyStudent().length;i++){
            if(this.getMyStudent()[i]==null){
                stu.setStudentSubject(this);
            //This indicates that referencing a data type as a parameter is equivalent to referencing
                this.getMyStudent()[i]=stu;
            //Two sentences in place in one step establish the connection from students to disciplines, and also from disciplines to students.
            //The top one-step approach chooses the third solution to the problem
                this.studentNum=i+1;
                break;
            }
        }
    }

}

Student class

package com.hala.modle;

public class Student {
    private String num;
    private String name;
    private String sex;
    private int age;
    private Subject studentSubject;




    public Subject getStudentSubject() {
        if(this.studentSubject==null)
            this.studentSubject=new Subject();
        //The importance of parameterless construction is shown here
        return studentSubject;
    }


    public void setStudentSubject(Subject studentSubject) {
        this.studentSubject = studentSubject;
    }


    public Student() {

    }


    public Student(String num, String name, String sex, int age) {
        this.setAge(age);
        this.setName(name);
        this.setNum(num);
        this.setSex(sex);
    }

    public Student(String num, String name, String sex, int age,Subject studentSubject) {
        this.setAge(age);
        this.setName(name);
        this.setNum(num);
        this.setSex(sex);
        this.setStudentSubject(studentSubject);
    }


    public String getNum() {
        return num;
    }
    public void setNum(String num) {
        this.num = num;
    }
    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;
    }

    /**
     * 
     * @param age Age
     * Age > 0 and < 100
     * 
     */
    public void setAge(int age) {
        if(age<=0||age>=100)
            age=18;
        this.age = age;
    }

    /**
     * Introduction of students
     * @return Name, school number, gender, age
     */
    public String introduction(){
        String str = "The student information is as follows:\n Student Name:" + this.getName() + "\n Student ID:" + this.getNum() + "\n Student gender:"
                + this.getSex()+"\n Student age:"+this.getAge();
            return str;
    }



    /**
     * Introduction of students
     * @param SubjectName
     * @param SubjectLife
     * @return Name, school number, gender, age, major, length of study
     * Solution 1; What to Pass
     */
    public String introduction(String SubjectName,String SubjectLife){
        String str = "The student information is as follows:\n Student Name:" + this.getName() + "\n Student ID:" + this.getNum() + "\n Student gender:"
                + this.getSex()+"\n Student age:"+this.getAge()+"\n Major:"+SubjectName+"\n Years of study:"+SubjectLife;
            return str;
    }



    /**
     * Introduction of students
     * @param mySubject
     * @return Name, school number, gender, age, major, length of study, major number
     * Solution 2; Pass Subject in as a method parameter
     */
    public String introduction(Subject mySubject) {
        String str = "The student information is as follows:\n Student Name:" + this.getName() + "\n Student ID:" + this.getNum() + "\n Student gender:" + this.getSex()
                + "\n Student age:" + this.getAge() + "\n Learning Specialties:" + mySubject.getSubjectName() + "\n Years of study:"
                + mySubject.getSubjectLife() + "\n Professional Number" + mySubject.getSubjectNum();
        return str;
    }


    /**
     * Introduction of students
     * @return Name, school number, gender, age, major, length of study, major number
     * Solution 3: Treat Subject as a property of Student
     */
    public String introduction1s() {
        String str = "The student information is as follows:\n Student Name:" + this.getName() + "\n Student ID:" + this.getNum() + "\n Student gender:" + this.getSex()
                + "\n Student age:" + this.getAge() + "\n Learning Specialties:" + this.getStudentSubject().getSubjectName() + "\n Years of study:"
                + this.getStudentSubject().getSubjectLife() + "\n Professional Number" + this.getStudentSubject().getSubjectNum();
        return str;
    }

}

SubjectTest class

package com.hala.test;

import com.hala.modle.*;

public class SubjectTest {

    public static void main(String[] args) {
        Subject sub1=new Subject("Computer Science and Application","J0001",4);
        Student stu1=new Student("s01","Zhang San","male",18);
        Student stu2=new Student("s02","Li Si","female",19);
        Student stu3=new Student("s03","King Five","female",18);
        Student stu4=new Student("s04","Zhao Six","male",20);


        sub1.addStudent(stu1);
        sub1.addStudent(stu2);
        sub1.addStudent(stu3);
        sub1.addStudent(stu4);
        System.out.println(sub1.getStudentNum());
        System.out.println(stu1.introduction1s());
//Because the Subject class is one step in place, you can output information about students and their disciplines.
    }

}

Output Results
4
The student information is as follows:
Student Name: Zhang San
Student number: s01
Student gender: male
Student age: 18
Learning Major: Computer Science and Application
Years of study: 4
Professional Number J0001

Two ways to prevent null pointer exceptions

Posted by FijiSmithy on Sat, 27 Jun 2020 09:44:22 -0700