# Encapsulate the array of Student type in object-oriented

Posted by DarkPrince2005 on Tue, 02 Jun 2020 17:56:55 +0200

#### requirement

The definition class Student contains three attributes: Student number(int), grade state(int), grade
score(int). Create 20 student objects with student numbers from 1 to 20. Grades and grades are determined by random numbers.
Question 1: print out the student information of grade 3 (state value is 3).
***

I've written two versions, one of which is not encapsulation, the other is encapsulation

Implementation code without encapsulation:

package com.luokai.exer;
/*
* The definition class Student contains three attributes: Student number(int), grade state(int), grade
score(int).  Create 20 student objects with student numbers from 1 to 20. Grades and grades are determined by random numbers.
Question 1: print out the student information of grade 3 (state value is 3).
***
Tips:
1) Generate random number: Math.random(), return value type double;
2) Rounding: Math.round(double d), return the value type long.
*/

public class StudentTest {
/**
* @Description
* @author luokai Email:luokai524@163.com
* @date 2020 12:42:09 PM, June 2, 2010
* @param args
*/
public static void main(String[] args) {
//Declare an array of Student type
Student[] studs = new Student[20];//String[] arr = new String[10];
for(int i = 0;i < studs.length;i++) {
//Assign values to array elements
studs[i] = new Student();
//Assign a value to the property of the Student object
//Student number [1,20]
studs[i].number = (i + 1);
studs[i].state = (int)(Math.random() * (6 - 1 + 1) + 1);
//Score [0100]
studs[i].score = (int)(Math.random() * (100 - 0 + 1));
}

//Traversing elements of all objects
for(int i = 0;i < studs.length;i++) {
//Method 1
//System.out.println ***
//Method 2
//studs[i].showStudent();

//Question 1: print out the student information of grade 3 (state value is 3).
if(studs[i].state == 3) {
studs[i].showStudent();
}
}
System.out.println();

//***
for(int i = 0;i < studs.length - 1;i++) {
for(int j = 0;j < studs.length - 1 - i;j++) {
if(studs[j].score > studs[j + 1].score) {
//If you need to reorder, exchange Student objects
Student temp = studs[j];
studs[j] = studs[j + 1];
studs[j + 1] = temp;
}

}
}
//Traversing objects after bubble sorting
for(int i = 0;i < studs.length;i++) {
studs[i].showStudent();
}

}

}

class Student{
int number;
int state;
int score;

//Traversing elements of all objects
public void showStudent() {
System.out.println("Student ID:" + number + " Grade:" +state+ " Results:" + score);
}
}

Implementation code for encapsulation:

package com.luokai.exer;
/*
* The definition class Student contains three attributes: Student number(int), grade state(int), grade
score(int).  Create 20 student objects with student numbers from 1 to 20. Grades and grades are determined by random numbers.
Question 1: print out the student information of grade 3 (state value is 3).
***
Tips:
1) Generate random number: Math.random(), return value type double;
2) Rounding: Math.round(double d), return the value type long.

*This code is correct StudentTest.java The function of manipulating array is encapsulated in array
*/

public class StudentTestPlus {
/**
* @Description
* @author luokai Email:luokai524@163.com
* @date 2020 12:42:09 PM, June 2, 2010
* @param args
*/
public static void main(String[] args) {
//Declare an array of Student type
StudentPlus[] studs = new StudentPlus[20];//String[] arr = new String[10];
for(int i = 0;i < studs.length;i++) {
//Assign values to array elements
studs[i] = new StudentPlus();
//Assign a value to the property of the Student object
//Student number [1,20]
studs[i].number = (i + 1);
studs[i].state = (int)(Math.random() * (6 - 1 + 1) + 1);
//Score [0100]
studs[i].score = (int)(Math.random() * (100 - 0 + 1));
}

StudentTestPlus test = new StudentTestPlus();
//Traverse StudentPlus array object
test.show(studs);
System.out.println("*********************");
test.searchState(studs, 3);
System.out.println("*********************");
//Rank results
test.sort(studs);

}

/**
*
* @Description  Traversal of Student [] array
* @author luokai Email:luokai524@163.com
* @date 2020 8:14:44 PM, June 2, 2006
* @param studs
*/
public void show(StudentPlus[] studs) {
for(int i = 0;i < studs.length;i++) {
studs[i].showStudent();
}
System.out.println();
}

/**
*
* @Description ***
* @author luokai Email:luokai524@163.com
* @date 2020 ***
* @param studs
* @param state
*/
public void searchState(StudentPlus[] studs,int state) {
//Traversing elements of all objects
for(int i = 0;i < studs.length;i++) {
if(studs[i].state == state) {
studs[i].showStudent();
}
}
System.out.println();
}

/**
*
* @Description ***
* @author luokai Email:luokai524@163.com
* @date 2020 1:19:07 PM, June 2, 2010
* @param studs
*/
public void sort(StudentPlus[] studs) {
for(int i = 0;i < studs.length - 1;i++) {
for(int j = 0;j < studs.length - 1 - i;j++) {
if(studs[j].score > studs[j + 1].score) {
//If you need to reorder, exchange Student objects
StudentPlus temp = studs[j];
studs[j] = studs[j + 1];
studs[j + 1] = temp;
}

}
}

//Traversing the student array after bubble sorting
for(int i = 0;i < studs.length;i++) {
studs[i].showStudent();
}
System.out.println();
}

}

class StudentPlus{
int number;
int state;
int score;

//Traversing elements of all objects
public void showStudent() {
System.out.println("Student ID:" + number + " Grade:" +state+ " Results:" + score);
}
}

Topics: Java