Exercise with class Vector

  • ExVectorSample
    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 a Vector with three elements. *

    * The first element of the Vector is the object * s1, the second element of the Vector is the * object s2, and the third element of the Vector * is the object s3 *

    * * @param s1 a Student object * @param s2 a Student object * @param s3 a Student object * @return a Vector with the objects s1, * s2, and s3 */ 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 a Vector from an array of Student *

    * The elements in the Vector have the same order that in the * array *

    * * @param anArray an array of Student * @return a Vector with 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 a Vector object 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 which Student * data are read * @param DELIM a String with the delimiter used in the stream * referenced by br * @return a Vector with the Student data stored * in the stream br * @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 a Student with a specified number exist * in a Vector. *

    * 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 Vector *

    * * @param vector a Vector that contains objects of * the class Student * @param number the number of the student to be tested * @return true if vector contains a * Student object with the number; * otherwise return false */ public static boolean existStudent(Vector vector, int number) { return vector.contains(new Student(number)); } /** * Obtains the number of Student objects with a * grade greater o equal than the number specified * * @param vector a Vector that contains objects of * the class Student * @param n a number with which to compare * @return the number of Student objects with a * grade greater o equal than n */ 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 minor grade of the * Student objects stored in the specified Vector * * @param vector a non empty Vector that contains objects of * the class Student * @return a integer value of the minor grade */ 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 the Student objects * stored in the specified Vector * * @param vector a Vector that contains objects of * the class Student * @return a double with 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 a Student with a specified number from * a Vector. If the Vector does 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 Vector *

    * * @param vector a Vector that contains objects of * the class Student * @param number the number of the student to be tested * @return true if vector contained the * Student object with the number; * otherwise return false */ public static boolean removeStudent(Vector vector, int number) { return vector.remove(new Student(number)); } /** * Removes from a Vector the Student objects * with a grade less than the number specified * * @param vector a Vector that contains objects of * the class Student * @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 a java.awt.TextArea component the information of * the Student objects contained in the specified * Vector *

    * This method calls the method {@link Student#toString()} to display the * information of each Student. *

    *

    * The information of each Student is displayed in a diferent * line. No extra lines and white space is displayed *

    * * @param vector a Vector that contains objects of * the class Student * @param ta a java.awt.TextArea component 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 a java.awt.List component the information of the * Student objects contained in the specified * Vector. *

    * The component List is cleared at the begining, so the * component lst has the same size that the vector. *

    *

    * This method calls the method {@link Student#toString()} to display the * information of each Student. *

    * * @param vector a Vector that contains objects of * the class Student * @param lst a java.awt.List component where the elements * of vector will 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 field number of * the Student objects contained in the specified * Vector. *

    * This method calls the method {@link Arrays#sort(int[])} to sort the array. *

    * * @param vector a Vector that contains objects of * the class Student * @return the sorted array with the Student objects contained * in vector. */ 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