⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 practical quiz 7.studentarraylist.java

📁 ssd3的全部答案
💻 JAVA
字号:
import java.util.*;

/**
 * This class contains methods to process array lists of {@link Student}
 * objects.
 * 
 * @author Neil
 * @version 1.0.0
 * @see Student
 * @see ArrayList
 */
public class StudentArrayList {

	/**
	 * Returns an array list with three elements.
	 * 
	 * @param first
	 *            a <code>Student</code> object.
	 * @param second
	 *            a <code>Student</code> object.
	 * @param third
	 *            a <code>Student</code> object.
	 * @return an array list with the objects <code>first</code>,
	 *         <code>second</code>, and <code>third</code>
	 */
	public static ArrayList<Student> makeArrayList(Student first,
			Student second, Student third) {

		ArrayList<Student> result = new ArrayList<Student>();
		result.add(first);
		result.add(second);
		result.add(third);

		return result;
	}

	/**
	 * Returns an array list with the same elements of the specified array
	 * arranged in the same order.
	 * 
	 * @param array
	 *            an array with <code>Student</code> objects .
	 * @return an array list with the same elements of the specified array
	 *         arranged in the same order
	 */
	public static ArrayList<Student> makeArrayListFromArray(Student[] array) {

		ArrayList<Student> result = new ArrayList<Student>();

		for (int i = 0; i < array.length; i++) {
			result.add(array[i]);
		}

		return result;
	}

	/**
	 * Returns <code>true</code> if the specified array list contains a
	 * student whose id matches the specified ID.
	 * 
	 * @param arrayList
	 *            an array list of <code>Student</code> objects.
	 * @param id
	 *            a student ID.
	 * @return <code>true</code> if the specified array list contains a
	 *         student whose ID matches the specified ID; <code>false</code>
	 *         otherwise.
	 */
	public static boolean hasStudent(ArrayList<Student> arrayList, int id) {

		if (arrayList.size() == 0)
			return false;

		for (Student i : arrayList) {
			if (i.getId() == (id))
				return true;
		}

		return false;
	}

	/**
	 * Returns the number of students in the specified array list whose grade is
	 * greater than or equal to the specified grade.
	 * 
	 * @param arrayList
	 *            an array list of <code>Student</code> objects.
	 * @param grade
	 *            a grade.
	 * @return the number of students in the specified array list whose grade is
	 *         greater than or equal to the specified grade.
	 */
	public static int countGradeGreaterOrEqual(ArrayList<Student> arrayList,
			int grade) {

		if (arrayList.size() == 0)
			return 0;

		int count = 0;

		for (Student i : arrayList) {
			if (i.getGrade() >= grade)
				count++;
		}

		return count;
	}

	/**
	 * Returns the smallest grade of the students in the specified array list.
	 * <p>
	 * This method assumes that the array list is not empty.
	 * 
	 * @param arrayList
	 *            an array list of <code>Student</code> objects.
	 * @return the smallest grade of the students in the specified array list.
	 */
	public static int getMinGrade(ArrayList<Student> arrayList) {

		if (arrayList.size() == 0)
			return 0;

		int result = arrayList.get(0).getGrade();

		for (Student i : arrayList) {
			if (i.getGrade() <= result)
				result = i.getGrade();
		}

		return result;
	}

	/**
	 * Returns the average grade of the students in the specified array list.
	 * 
	 * @param arrayList
	 *            an array list of <code>Student</code> objects.
	 * @return the average grade of the students in the specified array list.
	 */
	public static double getGradeAverage(ArrayList<Student> arrayList) {

		if (arrayList.size() == 0)
			return 0.0;

		double result = 0.0;

		for (Student i : arrayList) {
			result += i.getGrade();
		}

		return result / arrayList.size();
	}

	/**
	 * Removes all students in the specified array list whose grade is less than
	 * the specified grade.
	 * 
	 * @param arrayList
	 *            an array list of <code>Student</code> objects.
	 * @param grade
	 *            a grade.
	 */
	public static void removeGradeLess(ArrayList<Student> arrayList, int grade) {

		for (Iterator<Student> i = arrayList.iterator(); i.hasNext();) {
			Student s = i.next();
			if (s.getGrade() < grade)
				i.remove();
		}
	}

	/**
	 * Returns the string representation of the objects in the specified array
	 * list.
	 * <p>
	 * A new line character ( \n ) should separate the string representations of
	 * the objects. For example:
	 * </p>
	 * 
	 * <pre>
	 * Student[328,Galileo Galilei,80]\nStudent[123,Albert Einstein,100]
	 * </pre>
	 * 
	 * <p>
	 * Note that the string does <i>not</i> end with a new line character ( \n )
	 * </p>
	 * 
	 * @param arrayList
	 *            an array list of <code>Student</code> objects.
	 * @return the string representation of the objects in the specified array
	 *         list.
	 */
	public static String displayAll(ArrayList<Student> arrayList) {

		if (arrayList.size() == 0)
			return "";

		String result = "";

		for (Iterator<Student> i = arrayList.iterator(); i.hasNext();) {
			Student s = i.next();
			result += "Student[" + s.getId() + "," + s.getName() + ","
					+ s.getGrade() + "]\n";
		}

		return result.substring(0, result.length() - 1);
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -