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 aVector
with three elements. ** The first element of the
* * @param s1 aVector
is the object *s1
, the second element of theVector
is the * objects2
, and the third element of theVector
* is the objects3
*Student
object * @param s2 aStudent
object * @param s3 aStudent
object * @return aVector
with 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 aVector
from an array ofStudent
** The elements in the
* * @param anArray an array ofVector
have the same order that in the * array *Student
* @return aVector
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 aVector
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 whichStudent
* data are read * @param DELIM aString
with the delimiter used in the stream * referenced bybr
* @return aVector
with theStudent
data 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 aStudent
with 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
*Vector
that contains objects of * the classStudent
* @param number the number of the student to be tested * @returntrue
ifvector
contains a *Student
object with thenumber
; * otherwise returnfalse
*/ public static boolean existStudent(Vector vector, int number) { return vector.contains(new Student(number)); } /** * Obtains the number ofStudent
objects with a * grade greater o equal than the number specified * * @param vector aVector
that contains objects of * the classStudent
* @param n a number with which to compare * @return the number ofStudent
objects 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 minorgrade
of the *Student
objects stored in the specifiedVector
* * @param vector a non emptyVector
that 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 theStudent
objects * stored in the specifiedVector
* * @param vector aVector
that contains objects of * the classStudent
* @return adouble
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 aStudent
with a specified number from * aVector
. If theVector
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
* * @param vector aVector
*Vector
that contains objects of * the classStudent
* @param number the number of the student to be tested * @returntrue
ifvector
contained the *Student
object with thenumber
; * otherwise returnfalse
*/ public static boolean removeStudent(Vector vector, int number) { return vector.remove(new Student(number)); } /** * Removes from aVector
theStudent
objects * with a grade less than the number specified * * @param vector aVector
that 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.TextArea
component the information of * theStudent
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
* * @param vector aStudent
is displayed in a diferent * line. No extra lines and white space is displayed *Vector
that contains objects of * the classStudent
* @param ta ajava.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 ajava.awt.List
component the information of the *Student
objects contained in the specified *Vector
. ** The component
*List
is cleared at the begining, so the * componentlst
has the same size that thevector
. ** This method calls the method {@link Student#toString()} to display the * information of each
* * @param vector aStudent
. *Vector
that contains objects of * the classStudent
* @param lst ajava.awt.List
component where the elements * ofvector
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 fieldnumber
of * theStudent
objects contained in the specified *Vector
. ** This method calls the method {@link Arrays#sort(int[])} to sort the array. *
* * @param vector aVector
that contains objects of * the classStudent
* @return the sorted array with theStudent
objects 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 |