tienen estas características y no se encuentran con contratos derivados de transacciones discretas,
G RÁFICO 1.3: U N ESQUEMA DE LOS ESTRATOS INSTITUCIONALES
The query elements() given in class Vector returns an object of type Enumeration. An Enumeration allows one to traverse through all of the elements of a Vector. The following code illustrates how one might do this. Assume that one has constructed a Vector v. We also assume that each element of the Vector can respond to the toString() method so it can be printed.
for (Enumeration e = v.elements(); e.hasMoreElements () ; ) {
System.out.println(e.nextElement()); }
Listing 2.11 presents a simple example that exercises some of the protocol of class Vector and utilizes the enumeration given above.
Listing 2.11 Class VectorTest
/** A test program that exercises some of the behavior of class Vector. */
import java.util.*;
public class VectorApp {
// Fields
private Vector v = new Vector(); // For internal use
private void displayVector() {
System.out.println();
TEAM
FLY
for (Enumeration enum = v.elements(); enum.hasMoreElements(); ) System.out.println (enum.nextElement());
}
private void buildVector() {
v.addElement (''John"); v.addElement ("Mary"); v.addElement ("Paul"); v.addElement ("Cindy");
System.out.println ("v.capacity() = " + v.capacity()); v.setElementAt ("Richard" , 1);
v.insertElementAt ("Mary" , 1); v.removeElementAt (0);
System.out.println ("v.lastElement() = " + v.lastElement()); System.out.println ("v.size() = " + v.size());
v.trimToSize();
System.out.println ("v.capacity() = " + v.capacity()); }
public static void main(String[] args) {
VectorApp app= new VectorApp(); app.buildVector();
app.displayVector(); }
}
The output of the program in Listing 2.11 is
v.capacity() = 10 v.lastElement() = Cindy v.size() = 4 v.capacity() = 4 Mary Richard Paul Cindy
Suppose we desire to construct a Vector of integer primitives (int). The formal type specified for a Vector is Object. This type serves as a placeholder for any reference type (any object type). This allows us to insert elements of type String
earlier since String is a bona fide reference type (it is a class). But what about inserting elements of type int or double or any other primitive type?
To insert a primitive type into a Vector we must wrap the type using one of the standard wrapper classes (Integer for int, Double for double, Float for float, Boolean for boolean, Character for char). Listing 2.12 demonstrates how we can accomplish this.
Listing 2.12 Using Wrapper Classes for Primitive Types in a Vector
/** A test program to show how wrapper objects may be used with Vector. */
import java.util.*;
public class VectorWrapperApp {
public static void main(String[] args) {
Vector v = new Vector();
v.addElement (new Integer (10)); v.addElement (new Boolean (true)); v.addElement (new Double ( -1.95)); v.addElement (''We are done");
for (Enumeration enum = v.element(); enum.hasMoreElements(); ) System.out.println (enum.nextElement());
// Suppose we wish to access the element in index 2 and output // five times its value
double value = ((Double) v.elementAt (2)).doubleValue(); System.out.println ("Five times the value at index 2 = " + 5.0 * value);
System.out.println ("index of -1.95 is " +
v.indexOf (new Double (-1.95))); }
}
Output from Listing 2.12
10 true -1.95 We are done
Five times the value at index 2 = -9.75 index of -1.95 is 2
Let us revisit the problem solved in Section 2.9. We wish to store the grades of students in a class and determine the following statistics: highest grade, lowest grade, and average grade. Suppose further that the grades are held in an ASCII file, one grade per line. The name of this file is grades.txt. We modify the solution given earlier.
Modified Design of Solution
1. Let us assume that we have loaded the raw data values into a Vector using wrapper objects of type Double (the raw data will be input as type String and converted to Double).
2. To compute the highest grade we assume that the first element in the Vector is the largest (this will probably not be true but it is just our initial assumption).
3. We iterate through the enumeration of grades and compare each of the grades found to the largest. If we find a grade (as we most probably shall) that is larger than the current largest grade, we replace the largest grade with the new grade and continue our iteration through the enumeration of grades. When we have completed this iteration, we should have the largest grade.
4. To compute the smallest grade we assume that the first element in the Vector is the smallest (again this will probably not be true).
5. We again iterate through the grades and compare each grade found to the smallest. If we find a grade that is smaller than the current smallest, we replace the current smallest with this new grade that is smaller. When we have completed this iteration we should have the smallest grade.
6. To compute the average grade we must compute the total of all the grades and then divide this total by the number of grades. We initialize a variable total of type double to zero.
7. We iterate through the scores and increment the variable sum. Upon the completion of the iteration we divide the sum by the number of scores and output the mean.
Listing 2.13 presents the solution using Vector.
Listing 2.13 Class Grades
/** A class that manages the computation of several grading statistics * using Vector.
*/
import java.io.*; import java.util.*; public class Grades { // Fields
private int numberGrades; private Vector v;
// Constructor
public Grades(String fileName) throws IOException {
v = new Vector();
BufferedReader diskInput = new BufferedReader( new InputStreamReader( new FileInputStream( new File (fileName)))); // Load internal vector v with grades
String line;
line = diskInput.readLine(); while (line != null) {
v.addElement (new Double(line)); numberGrades++; line = diskInput.readLine(); } } // Methods
public double maximumGrade () {
double largest = ((Double) v.firstElement()).doubleValue(); for (Enumeration enum = v.elements(); enum.hasMoreElements();){ double nextValue = ((Double) enum.nextElement()).doubleValue(); if ( nextValue > largest) largest = nextValue; } return largest; }
public double minimumGrade () {
double smallest = ((Double) v.firstElement()).doubleValue(); for (Enumeration enum = v.elements(); enum.hasMoreElements();){ double nextValue = ((Double) enum.nextElement()).doubleValue(); if ( nextValue < smallest) smallest = nextValue; } return smallest; }
public double averageGrade () {
double sum = 0.0;
for (Enumeration enum = v.elements(); enum.hasMoreElements();){ double nextValue =
((Double) enum.nextElement()).doubleValue(); sum += nextValue;
}
return sum / numberGrades; }
}
2.12— Summary
• An object is associated with a reference type – a class. It is more specifically an instance of a class.
• Before an object can be created it must be declared to be of a reference type.
• A class that implements the Cloneable interface may redefine the inherited clone method from Object. Its instances may then be sent the clone message.
• The predefined equality operator returns true if the object references are identical. This is true only if there exist two separate references to the same underlying storage as when aliasing occurs.
• Reference types include programmer-defined classes as well as the set of classes provided in the standard Java libraries.
• The primitive types are not associated with a class and do not need to be created. They may be initialized at their point of declaration.
• One of the most widely used Java classes is class String. This reference type is used to represent sequences of characters.
• A StringBuffer object is not immutable, unlike a String object. A string buffer implements a mutable sequence of characters.
• In Java, an array is an object and is represented by a reference type. Like all reference types an array must be created before it can be used.
• A Vector can hold any number of elements, is dynamic, and is indexable; that is, one can insert or access information at a specific index. A Vector is dynamic and automatically resizes itself if an insertion causes the number of elements to exceed the current capacity.
• An Enumeration allows one to traverse through all of the elements of a Vector.
2.13—