面向对象案例-学生信息

项目要求:

实体类:
	学生类:
		id, 姓名,年龄,性别,成绩
	需要使用数组保存学生信息
		Student[] allStu
	需要完成的方法
		1. 根据学生的ID,找到对应的学生对象
		2. 完成方法,添加新学生
		3. 完成方法,删除指定ID的学生
		4. 完成方法,展示数组中所有的学生信息
		5. 根据学生成绩,完成降序排序

项目分析

1.创建Student类,定义学生的各项信息
2.创建ArrayFunction类,定义学生信息的各项操作。
3.创建Demo主类

Student类分析实现

1.定义学生类,创建私有成员属性
   	private int id;
	private String name;
	private int age;
	private char gender;
	private float score;
    其中需要注意的是,id项由程序生成,非人工自行输入,只累加,不减少
2.定义有参无参构造方法
3.实现各项成员属性getter,setter方法

public class Student {
	private int id;
	private String name;
	private int age;
	private char gender;
	private float score;
	
	public Student() {}

	public Student(String name, int age, char gender, float score) {

		this.name = name;
		this.age = age;
		this.gender = gender;
		this.score = score;
	}


	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

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

	public int getAge() {
		return age;
	}

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

	public char getGender() {
		return gender;
	}

	public void setGender(char gender) {
		this.gender = gender;
	}

	public float getScore() {
		return score;
	}

	public void setScore(float score) {
		this.score = score;
	}
}

ArrayFunc类分析实现

public class ArrayFunc {
        //成员属性:
    	
	int count = 0;				// 累计学生数量累加
	int size = 0;				// 数组长度计数 == 5个
	Student[] allStu = new Student[5];	// 初始化先设置容量Capacity == 5个

	/**
	 * 根据传入的学生id查找对应的学生信息
	 * 
	 * @param id 学生 目标id
	 */
	public void find(int id) {
		int temp = -1;

		for (int i = 0; i < size; i++) {
			if (id == allStu[i].getId()) {
				temp = i;
				break;
			}
		}

		if (temp < 0) {
			System.out.println("你要查找的id不存在");
		} else {
			System.out.println("id是:" + allStu[id].getId() + ",姓名是:" + allStu[id].getName());
		}

	}

	/**
	 * 传入一个学生元素,插入到对应数组中
	 * 插入数组之前调用check函数判断数组是否满员,check返回true就直接插入,返回false则调用扩容;
	 * 
	 * @param student Student类型的元素
	 */
	public void insert(Student student) {
		boolean flag = this.check();

		if (flag) {
			allStu[size] = student;
			student.setId(count);
			size++;
			count++;
		}
	}

	/**
	 * 判断数组当前有效元素长度是否小于数组长度;
	 * 
	 * @return 确认当前有效元素长度小于数组长度后返回true,否则扩容后返回true
	 */
	public boolean check() {
		boolean flag = false;
		if (size < allStu.length - 1) {//数组提前扩容,后续展示空间不需考虑越界问题
			flag = true;
		} else {
			expand();
			flag = true;
		}
		return flag;
	}

	/**
	 * 数组扩容
	 * 
	 */
	public void expand() {
		Student[] newStu = new Student[allStu.length * 2];
		System.arraycopy(allStu, 0, newStu, 0, allStu.length);

		allStu = newStu;
	}

	/**
	 * 删除指定id的元素
	 * 
	 * @param delId 要删除的指定id
	 */
	public void del(int delId) {

		int delIndex = size + 1;
		boolean flag = true;

		for (int i = 0; i < size; i++) {
			if (delId == allStu[i].getId()) {
				delIndex = i;
				flag = false;
				size--;
				break;
			}
		}

		if (flag) {
			System.out.println("你要删除的id:" + delId + "不存在");
		}

		
		
		for (int i = delIndex; i <= size; i++) {
			allStu[i] = allStu[i + 1];
		}

	}

	/**
	 * 根据学生分数排序算法
	 * 
	 */
	public void sort() {
		for (int i = 0; i < size - 1; i++) {
			for (int j = i + 1; j < size; j++) {
				if (allStu[i].getScore() < allStu[j].getScore()) {
					Student temp = allStu[i];
					allStu[i] = allStu[j];
					allStu[j] = temp;
				}
			}
		}
	}
	
	/**
	 * 展示数组
	 */
	public void show() {
		for (int i = 0; i < size; i++) {
			System.out.println("id是:" + allStu[i].getId() + ",姓名是:" + allStu[i].getName());
		}
	}

}

Demo主类实现

public class Demo {
	public static void main(String[] args) {

		// 生成Student 对象
		Student student = new Student("张0", 18, '男', 90F);//匿名对象

		ArrayFunc arrFunc = new ArrayFunc();

		arrFunc.insert(student);

		arrFunc.insert(new Student("张1", 26, '男', 91F));
		arrFunc.insert(new Student("张2", 26, '男', 92F));
		arrFunc.insert(new Student("张3", 26, '男', 93F));
		arrFunc.insert(new Student("张4", 26, '男', 94F));
		arrFunc.insert(new Student("张5", 26, '男', 95F));
		arrFunc.insert(new Student("张6", 26, '男', 96F));
		arrFunc.insert(new Student("张7", 26, '男', 97F));
		arrFunc.insert(new Student("张8", 26, '男', 98F));
		arrFunc.insert(new Student("张9", 26, '男', 99F));
		
		arrFunc.find(6);// 根据下标找到对应同学
		System.out.println("-----------------");
		
		arrFunc.show();// 展示所有的学生信息
		System.out.println("-----------------");

		arrFunc.sort();// 这里加入allStu为什么报错?
		arrFunc.show();
		System.out.println("-----------------");
		
		arrFunc.del(0);// 删除指定id学生
		arrFunc.show();
		System.out.println("-----------------");
		
		arrFunc.insert(new Student("张10", 26, '男', 99F));

		arrFunc.show();
		System.out.println("-----------------");	
		
                arrFunc.sort();
		arrFunc.show();
		System.out.println("-----------------");		
		
		
	}
}

代码结果

id是:6,姓名是:张6
-----------------
id是:0,姓名是:张0
id是:1,姓名是:张1
id是:2,姓名是:张2
id是:3,姓名是:张3
id是:4,姓名是:张4
id是:5,姓名是:张5
id是:6,姓名是:张6
id是:7,姓名是:张7
id是:8,姓名是:张8
id是:9,姓名是:张9
-----------------
id是:9,姓名是:张9
id是:8,姓名是:张8
id是:7,姓名是:张7
id是:6,姓名是:张6
id是:5,姓名是:张5
id是:4,姓名是:张4
id是:3,姓名是:张3
id是:2,姓名是:张2
id是:1,姓名是:张1
id是:0,姓名是:张0
-----------------
id是:9,姓名是:张9
id是:8,姓名是:张8
id是:7,姓名是:张7
id是:6,姓名是:张6
id是:5,姓名是:张5
id是:4,姓名是:张4
id是:3,姓名是:张3
id是:2,姓名是:张2
id是:1,姓名是:张1
-----------------
id是:9,姓名是:张9
id是:8,姓名是:张8
id是:7,姓名是:张7
id是:6,姓名是:张6
id是:5,姓名是:张5
id是:4,姓名是:张4
id是:3,姓名是:张3
id是:2,姓名是:张2
id是:1,姓名是:张1
id是:10,姓名是:张10
-----------------
id是:9,姓名是:张9
id是:10,姓名是:张10
id是:8,姓名是:张8
id是:7,姓名是:张7
id是:6,姓名是:张6
id是:5,姓名是:张5
id是:4,姓名是:张4
id是:3,姓名是:张3
id是:2,姓名是:张2
id是:1,姓名是:张1
-----------------

猜你喜欢

转载自www.cnblogs.com/raising/p/12814351.html