import java.util.*; import java.io.*; import student.*; /** * ExVectorSample ** This class implements some methods to practice the * use of the class {@link Vector}. *
* * @author Alfonso Rodriguez * @version 1.0.0 * @see Student * @see StudentFormatException * @see Vector */ public class ExVectorSample { /** * Creates aVectorwith three elements. ** The first element of the
* * @param s1 aVectoris the object *s1, the second element of theVectoris the * objects2, and the third element of theVector* is the objects3*Studentobject * @param s2 aStudentobject * @param s3 aStudentobject * @return aVectorwith the objectss1, *s2, ands3*/ public static Vector makeVector3(Student s1, Student s2, Student s3) { Vector v = new Vector(); v.add(s1); v.add(s2); v.add(s3); return v; } /** * Creates aVectorfrom an array ofStudent** The elements in the
* * @param anArray an array ofVectorhave the same order that in the * array *Student* @return aVectorwith the objects stored in the array *anArray*/ public static Vector makeVectorFromArray(Student anArray[]) { Vector v = new Vector(); for (int i = 0; i < anArray.length; i++) { v.add(anArray[i]); } return v; } /** * Creates aVectorobject from an input stream ** The data of each student is stored in one line in the input stream, * and each field is separated by a the delimiter specified in the parameter *
*DELIM, for example. ** 328_Galileo Galilei_80_ * 123_Albert Einstein_100_ * 926_Isaac Newton_90_ ** This method uses {@link StringTokenizer} to obtain each field from a line * * * @param br a {@link Reader} from whichStudent* data are read * @param DELIM aStringwith the delimiter used in the stream * referenced bybr* @return aVectorwith theStudentdata stored * in the streambr* @throws {@link IOException} if cannot read from the input stream * @throws {@link StudentFormatException} if receives a badly formed data */ public static Vector makeVectorFromReader(Reader r, final String DELIM) throws IOException, StudentFormatException { BufferedReader br = new BufferedReader(r); Vector v = new Vector(); StringTokenizer st; String line = ""; int number; String name; int grade; try { line = br.readLine(); while (line != null) { st = new StringTokenizer(line, DELIM); number = Integer.parseInt(st.nextToken()); name = st.nextToken(); grade = Integer.parseInt(st.nextToken()); if (st.hasMoreTokens()) { throw new StudentFormatException(line); } v.add(new Student(number, name, grade)); line = br.readLine(); } } catch (NoSuchElementException nsee) { throw new StudentFormatException(line); } catch (NumberFormatException nfe) { throw new StudentFormatException(line); } return v; } /** * Tests if aStudentwith a specified number exist * in aVector. ** This method creates a {@link Student} object with the * specified number and calls the method {@link Vector#contains(Object elem)} * to test if the object exists in the
* * @param vector aVector*Vectorthat contains objects of * the classStudent* @param number the number of the student to be tested * @returntrueifvectorcontains a *Studentobject with thenumber; * otherwise returnfalse*/ public static boolean existStudent(Vector vector, int number) { return vector.contains(new Student(number)); } /** * Obtains the number ofStudentobjects with a * grade greater o equal than the number specified * * @param vector aVectorthat contains objects of * the classStudent* @param n a number with which to compare * @return the number ofStudentobjects with a * grade greater o equal thann*/ public static int countGradeGreaterOEqual(Vector vector, int n) { int count = 0; for (Iterator i = vector.iterator(); i.hasNext(); ) { if (((Student) i.next()).getGrade() >= n) { count++; } } return count; } /** * Obtains the value of the minorgradeof the *Studentobjects stored in the specifiedVector* * @param vector a non emptyVectorthat contains objects of * the classStudent* @return a integer value of the minorgrade*/ public static int getMinGrade(Vector vector) { Iterator i = vector.iterator(); int min = ((Student) i.next()).getGrade(); Student s; while(i.hasNext()) { s = (Student) i.next(); if (s.getGrade() < min) { min = s.getGrade(); } } return min; } /** * Obtains the grade average of theStudentobjects * stored in the specifiedVector* * @param vector aVectorthat contains objects of * the classStudent* @return adoublewith the grade average */ public static double getGradeAverage(Vector vector) { double total = 0.0; if (vector.size() == 0) { return 0.0; } else { for (Iterator i = vector.iterator() ; i.hasNext(); ) { total += ((Student) i.next()).getGrade(); } return total / (double) vector.size(); } } /** * Removes aStudentwith a specified number from * aVector. If theVectordoes not contain * the element, it is unchanged. ** This method creates a {@link Student} object with the * specified number and calls the method {@link Vector#remove(Object elem)} * to remove the object from the
* * @param vector aVector*Vectorthat contains objects of * the classStudent* @param number the number of the student to be tested * @returntrueifvectorcontained the *Studentobject with thenumber; * otherwise returnfalse*/ public static boolean removeStudent(Vector vector, int number) { return vector.remove(new Student(number)); } /** * Removes from aVectortheStudentobjects * with a grade less than the number specified * * @param vector aVectorthat contains objects of * the classStudent* @param n a number with which to compare */ public static void removeGradeLess(Vector vector, int n) { for (Iterator i = vector.iterator() ; i.hasNext(); ) { if (((Student) i.next()).getGrade() < n) { i.remove(); } } } /** * Display in ajava.awt.TextAreacomponent the information of * theStudentobjects contained in the specified *Vector** This method calls the method {@link Student#toString()} to display the * information of each
*Student. ** The information of each
* * @param vector aStudentis displayed in a diferent * line. No extra lines and white space is displayed *Vectorthat contains objects of * the classStudent* @param ta ajava.awt.TextAreacomponent where the * information will be displayed */ public static void displayInTextArea(Vector vector, java.awt.TextArea ta) { int size = vector.size(); if (size > 0) { Iterator i = vector.iterator(); ta.setText(i.next().toString()); while(i.hasNext()) { ta.append("\n" + i.next().toString()); } } else { ta.setText(""); } } /** * Display in ajava.awt.Listcomponent the information of the *Studentobjects contained in the specified *Vector. ** The component
*Listis cleared at the begining, so the * componentlsthas the same size that thevector. ** This method calls the method {@link Student#toString()} to display the * information of each
* * @param vector aStudent. *Vectorthat contains objects of * the classStudent* @param lst ajava.awt.Listcomponent where the elements * ofvectorwill be displayed */ public static void displayInList(Vector vector, java.awt.List lst) { lst.removeAll(); for (Iterator i = vector.iterator() ; i.hasNext(); ) { lst.add(i.next().toString()); } } /** * Obtains a sorted array with values of the fieldnumberof * theStudentobjects contained in the specified *Vector. ** This method calls the method {@link Arrays#sort(int[])} to sort the array. *
* * @param vector aVectorthat contains objects of * the classStudent* @return the sorted array with theStudentobjects contained * invector. */ public static int[] getNumbers(Vector vector) { int nums[] = new int[vector.size()]; int i = 0; for (Iterator it = vector.iterator() ; it.hasNext();) { nums[i++] = ((Student) it.next()).getNumber(); } Arrays.sort(nums); return nums; } }
| Previou page | Next page |