package myprojects.vectorexample;
import java.awt.*;
import java.awt.event.*;
import java.util.Vector;
public class VectorExample extends Frame {
public VectorExample() {
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
dispose();
System.exit(0);
}
});
}
public static void main(String args[]) {
System.out.println("Starting VectorExample...");
VectorExample mainFrame = new VectorExample();
mainFrame.setSize(400, 400);
mainFrame.setTitle("VectorExample");
mainFrame.setVisible(true);
// Create a vehicle of weight 20KG and length 1.5M
Vehicle v1= new Vehicle("Peugot",20.0, 1.5);
// Create a vehicle of weight 1000.0KG and length 3.2M
Vehicle v2 = new Vehicle("Ford", 1000.0, 3.2);
// Create a vehicle of 30000kg and 6 metres length
Vehicle v3 = new Vehicle("Chieftan",30000.0,6.0);
// Declare a Vector (v)
Vector v = new Vector();
// Add three vehicles
v.addElement(v1);
v.addElement(v2);
v.addElement(v3);
System.out.println("The number of vehicles is "+v.size());
// The number of vehicles in the vector can be found by using the
// size() method.
for(int i=0; i<v.size(); i++) {
// Declare a vehicle reference 'veh'
Vehicle veh;
// retrieve the element and 'cast' it using (Vehicle)
veh = (Vehicle)v.elementAt(i);
System.out.println("Vector entry "+i);
// Invoke the method to display the vehicle data
veh.displayData();
}
// Remove the first entry and print out the list again
v.removeElementAt(0);
for(int i=0; i<v.size(); i++) {
// Declare a vehicle reference 'veh'
Vehicle veh;
// retrieve the element and 'cast' it using (Vehicle)
veh = (Vehicle)v.elementAt(i);
System.out.println("Vector entry "+i);
// Invoke the method to display the vehicle data
veh.displayData();
}
}
}
|