对面向对象中Student类型的数组进行封装处理

要求

定义类Student,包含三个属性:学号number(int),年级state(int),成绩
score(int)。 创建20个学生对象,学号为1到20,年级和成绩都由随机数确定。
问题一:打印出3年级(state值为3)的学生信息。
问题二:使用冒泡排序按学生成绩排序,并遍历所有学生信息

我写了两个版本,其中一个是不进行封装处理,另一个进行封装处理

不进行封装的实现代码:

package com.luokai.exer;
/*
 * 定义类Student,包含三个属性:学号number(int),年级state(int),成绩
score(int)。 创建20个学生对象,学号为1到20,年级和成绩都由随机数确定。
问题一:打印出3年级(state值为3)的学生信息。
问题二:使用冒泡排序按学生成绩排序,并遍历所有学生信息
提示:
1) 生成随机数:Math.random(),返回值类型double;
2) 四舍五入取整:Math.round(double d),返回值类型long。
 */

public class StudentTest {
	/**
	 * @Description
	 * @author luokai Email:luokai524@163.com
	 * @date 2020年6月2日下午12:42:09
	 * @param args
	 */
	public static void main(String[] args) {
		//声明Student类型的数组
		Student[] studs = new Student[20];//String[] arr = new String[10];
		for(int i = 0;i < studs.length;i++) {
			//给数组元素赋值
			studs[i] = new Student();
			//给Student对象的属性赋值
			//学号[1,20]
			studs[i].number = (i + 1);
			//年级[1,6]
			studs[i].state = (int)(Math.random() * (6 - 1 + 1) + 1);
			//成绩[0,100]
			studs[i].score = (int)(Math.random() * (100 - 0 + 1));		
		}
		
		//遍历所有对象的元素
		for(int i = 0;i < studs.length;i++) {
			//方法一
			//System.out.println("学号:" + studs[i].number + " 年级:" +studs[i].state+ " 成绩:" + studs[i].score);
			//方法二
			//studs[i].showStudent();
			
			//问题一:打印出3年级(state值为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) {
					//如果需要换序,交换Student对象
					Student temp = studs[j];
					studs[j] = studs[j + 1];
					studs[j + 1] = temp;
				}
				
			}
		}
		//遍历冒泡排序后的对象
		for(int i = 0;i < studs.length;i++) {
			studs[i].showStudent();
		}
		
	}
	
}

class Student{
	int number;
	int state;
	int score;	
	
	//遍历所有对象的元素
	public void showStudent() {		
			System.out.println("学号:" + number + " 年级:" +state+ " 成绩:" + score);		
	}
}

进行封装的实现代码:

package com.luokai.exer;
/*
 * 定义类Student,包含三个属性:学号number(int),年级state(int),成绩
score(int)。 创建20个学生对象,学号为1到20,年级和成绩都由随机数确定。
问题一:打印出3年级(state值为3)的学生信息。
问题二:使用冒泡排序按学生成绩排序,并遍历所有学生信息
提示:
1) 生成随机数:Math.random(),返回值类型double;
2) 四舍五入取整:Math.round(double d),返回值类型long。

 *此代码是对StudentTest.java的改进,将操作数组的功能封装在数组中
 */

public class StudentTestPlus {
	/**
	 * @Description
	 * @author luokai Email:luokai524@163.com
	 * @date 2020年6月2日下午12:42:09
	 * @param args
	 */
	public static void main(String[] args) {
		//声明Student类型的数组
		StudentPlus[] studs = new StudentPlus[20];//String[] arr = new String[10];
		for(int i = 0;i < studs.length;i++) {
			//给数组元素赋值
			studs[i] = new StudentPlus();
			//给Student对象的属性赋值
			//学号[1,20]
			studs[i].number = (i + 1);
			//年级[1,6]
			studs[i].state = (int)(Math.random() * (6 - 1 + 1) + 1);
			//成绩[0,100]
			studs[i].score = (int)(Math.random() * (100 - 0 + 1));		
		}
		
		StudentTestPlus test = new StudentTestPlus();
		//遍历StudentPlus数组对象		
		test.show(studs);
		System.out.println("*********************");
		//查询年级的学生
		test.searchState(studs, 3);
		System.out.println("*********************");
		//对成绩进行排序
		test.sort(studs);
		
		
		
				
	}
	
	/**
	 * 
	 * @Description  遍历Student[]数组的操作
	 * @author luokai Email:luokai524@163.com
	 * @date 2020年6月2日下午8:14:44
	 * @param studs
	 */
	public void show(StudentPlus[] studs) {
		for(int i = 0;i < studs.length;i++) {				
			studs[i].showStudent();					
			}
		System.out.println();
		}
	
	/**
	 * 		
	 * @Description 查找StudentPlus数组中某年级的所有学生
	 * @author luokai Email:luokai524@163.com
	 * @date 2020年6月2日下午1:16:31
	 * @param studs
	 * @param state
	 */
	public void searchState(StudentPlus[] studs,int state) {
		//遍历所有对象的元素
		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年6月2日下午1:19:07
	 * @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) {
					//如果需要换序,交换Student对象
					StudentPlus temp = studs[j];
					studs[j] = studs[j + 1];
					studs[j + 1] = temp;
				}
				
			}
		}
		
		//遍历冒泡排序后的学生数组
		for(int i = 0;i < studs.length;i++) {
			studs[i].showStudent();
		}
		System.out.println();
	}
	
}

class StudentPlus{
	int number;
	int state;
	int score;	
	
	//遍历所有对象的元素
	public void showStudent() {		
			System.out.println("学号:" + number + " 年级:" +state+ " 成绩:" + score);		
	}
}

  • 4
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值