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

📄 studentarraylist.java

📁 ssd3的练习上的答案保证是100分的
💻 JAVA
字号:
import java.util.*;
import java.util.Iterator;
/*
 *老师我已经尽力了,最后的两个方法都不能很好的实现了,倒数第二个方法我尝试了四种不同的方法但是还是没有出现题目要求的结果
 *请您指正!!
 **/
/**
 * This class contains methods to process array lists of {@link Student}
 * objects.
 *
 * @author  autor name
 * @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)  {

		/* PLACE YOUR CODE HERE */
		ArrayList<Student> array=new ArrayList<Student>();
		array.add(first);
		array.add(second);
		array.add(third);

		return array; // REMOVE; USED SO THIS FILE COMPILES
	}

	/**
	 * 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) {

		/* PLACE YOUR CODE HERE */
		ArrayList<Student> arraylist=new ArrayList<Student>();
		for(int i=0;i<array.length;i++){
		
          arraylist.add(array[i]);
                                        }
                                        
		return arraylist; // REMOVE; USED SO THIS FILE COMPILES
	}

	/**
	 * 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)  {

		/* PLACE YOUR CODE HERE */
        for(int i=0;i<arrayList.size();i++){
            if(arrayList.get(i).getId()==id)return true;
        }
		return false; // REMOVE; USED SO THIS FILE COMPILES
	}

	/**
	 * 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)  {

		/* PLACE YOUR CODE HERE */
		int count=0;
		for(int i=0;i<arrayList.size();i++){
			if(arrayList.get(i).getGrade()>=grade) count++;
					}

		return count; // REMOVE; USED SO THIS FILE COMPILES
	}

	/**
	 * 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)  {

		/* PLACE YOUR CODE HERE */
		int a[]=new int[arrayList.size()];
		for(int i=0;i<arrayList.size();i++){
			 a[i]=arrayList.get(i).getGrade();
		}
		Arrays.sort(a);
               
		return a[0]; // REMOVE; USED SO THIS FILE COMPILES
	}

	/**
	 * 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)  {

		/* PLACE YOUR CODE HERE */
		double d=0.0;
		for(int i=0;i<arrayList.size();i++){
			d+=arrayList.get(i).getGrade();
		}
        double e=d/arrayList.size();
		return e; // REMOVE; USED SO THIS FILE COMPILES
	}

	/**
	 * 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)  {
			
		/* PLACE YOUR CODE HERE */
		//methode1:
	/*	int count=0;
		ArrayList<Student> arrayListS=arrayList;
		for(int i=0;i<arrayListS.size();i++){
			if(arrayListS.get(i).getGrade()<grade){
				 arrayListS.get(i).setGrade(0);
				 count++;
			}
		                                   }
	 ArrayList<Student> newarray=new ArrayList<Student>();
	 for(int i=0;i<arrayListS.size();i++){
	 	if(arrayList.get(i).getGrade()!=0) newarray.add(arrayList.get(i));
	 }
	 arrayList.clear();
	 arrayList=newarray;*/
	 //method2:
	/* ArrayList<Student> newarraylist=new ArrayList<Student>();
	 for(int i=0;i<arrayList.size();i++){
	 if(arrayList.get(i).getGrade()>grade) newarraylist.add(arrayList.get(i));
	 
	 }	
	 arrayList=newarraylist;
	}*/
	//method3:
	/*int count=0;
    ArrayList<Student> newarraylist=new ArrayList<Student>();
    for(int i=0;i<arrayList.size();i++){
	 if(arrayList.get(i).getGrade()>grade) {
	  ++count;
	 for(int j=0;j<count;j++){
	 
	 newarraylist.set(j,arrayList.get(i));
	                          }
	                                         }
	        }
	 arrayList.clear();
	 for(int i=0;i<newarraylist.size();i++){
	 
	 arrayList.add(newarraylist.get(i));
	      }
	 }	*/
	 Iterator<Student> iter=arrayList.iterator();
	while(iter.hasNext()){
		if(iter.next().getGrade()<grade)
		iter.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)  {

		/* PLACE YOUR CODE HERE */
			String s="";
			String m="";
	  if(arrayList.size()!=0) {
	    for(int i=0;i<arrayList.size()-1;i++){
	   	   s+=arrayList.get(i).toString()+"\n";
	   	                                }
	   	    m=s+arrayList.get(arrayList.size()-1).toString();     
	   	                                }
	   	   return m;                         
	}
	}

⌨️ 快捷键说明

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