View Single Post
  #2 (permalink)  
Old 10-31-2007, 11:59 AM
hardwired hardwired is offline
Senior Member
 
Join Date: Jul 2007
Posts: 1,266
hardwired is on a distinguished road
Vector is a legacy class and has been retrofitted to work in the Collections framework that came out in j2se 1.2
Look up the Vector class api in the javadocs to see what methods and fields are available for you to use. Here's a link into the javadocs for version 1.5 1.5 javadocs. Scroll down to the Vector class in the lower left frame, select the link and the Vector class api loads into the main frame.
Since you are using Vector you may also be using an older java version. Type "java -version" at the prompt to find out. If you are using j2se 1.5 or later you can use the generic declarations shown (but this is not required). If you are using an older version (1.4 of earlier) you can forget about generics but will have to make a cast (shown below) when taking each Course out of the vector. You can make the cast in generics, too if you want but it is not required.
This is currently set up for generics so you'll have to comment/uncomment the appropriate (marked) options to get this to compile if you are using an earlier version.
Code:
import java.awt.*; import java.awt.event.*; import java.util.Vector; import javax.swing.*; public class GradebookRx extends JFrame { private JButton add; private JButton print; private JButton exit; float[] gpa_array=new float[5]; private int num_students=0; Student[] stu=new Student[5]; JTextArea textArea = new JTextArea(); public GradebookRx() { super("Student/Course GUI"); setDefaultCloseOperation(EXIT_ON_CLOSE); for(int a=0;a<5;a++) stu[a]=new Student(); // Initialize buttons ButtonHandler handler = new ButtonHandler(); JPanel north = new JPanel(); add = new JButton("Add Student"); add.addActionListener(handler); north.add(add); print = new JButton("Print"); print.addActionListener(handler); north.add(print); exit = new JButton("Exit"); exit.addActionListener(handler); north.add(exit); Container cat = getContentPane(); cat.add(north, BorderLayout.NORTH); cat.add(new JScrollPane(textArea)); // default center section setSize(800,600); setLocation(100,100); setVisible(true); } public class ButtonHandler implements ActionListener { public void actionPerformed(ActionEvent e) { JButton button = (JButton)e.getSource(); if(button == add) { num_students++; stu[num_students].getData(); } else if(button == print) { System.out.println("Printing all courses and students"); printAllData(); } else if(button == exit) System.exit(0); } } private void printAllData() { textArea.setText(""); for(int j = 0; j < stu.length; j++) { if(stu[j].getName() != null) // no empty student elements { textArea.append(stu[j] + "\n"); printCourseData(stu[j].getVector()); } } } private void printCourseData(Vector<Course> v) // generic method signature // private void printStudentData(Vector v) // non-generic signature { for(int j = 0; j < v.size(); j++) { Course course = v.get(j); // generic form //Course course = (Course)v.get(j); // non-generic cast textArea.append(course + "\n"); } } public static void main(String[] args) { new GradebookRx(); } } class Student { private String name; private String stringage; private int age; private float gpa; private int num_courses; private String stringnum_courses; private int totalqp; // generic declaration for j2se 1.5+ private Vector<Course> v = new Vector<Course>(); // non-generic declaration for j2se 1.4- //private Vector v = new Vector(); public Student() { } public void getData() { name = JOptionPane.showInputDialog(null,"Enter Student's Name", "Enter Student's Name", JOptionPane.QUESTION_MESSAGE); stringage = JOptionPane.showInputDialog(null,"Enter Student's Age", "Enter Student's Age", JOptionPane.QUESTION_MESSAGE); age = Integer.parseInt(stringage); stringnum_courses = JOptionPane.showInputDialog(null,"Enter Number of Courses", "Enter Number of Courses", JOptionPane.QUESTION_MESSAGE); num_courses = Integer.parseInt(stringnum_courses); for(int a=0;a<num_courses;a++) { Course cr = new Course(); v.add(cr); } } public String toString() { return "Name:" + name + " age:" + age + " num_courses:" + num_courses; } public String getName() { return name; } public Vector<Course> getVector() // generic method signature //public Vector getVector() // non-generic signature { return v; } } class Course { // instance variables - replace the example below with your own private int id; private String dept; private int credits; private String letter_grade; private String stringid; private String stringcredits; private int QP; public Course() { getDept(); getId(); getCredits(); getLettergrade(); getQP(); } public void getDept() { dept = JOptionPane.showInputDialog(null,"Enter Department","Enter Department", JOptionPane.QUESTION_MESSAGE); } public void getId() { stringid = JOptionPane.showInputDialog(null,"Enter Course ID","Enter Course ID", JOptionPane.QUESTION_MESSAGE); id = Integer.parseInt(stringid); } public void getCredits() { stringcredits = JOptionPane.showInputDialog(null,"Enter Credits","Enter Credits", JOptionPane.QUESTION_MESSAGE); credits = Integer.parseInt(stringcredits); } public void getLettergrade() { letter_grade = JOptionPane.showInputDialog(null,"Enter Letter Grade", "Enter Letter Grade", JOptionPane.QUESTION_MESSAGE); } public void getQP() { // The "==" operator is not so useful for testing Strings. // Use the "equals" method for testing String equality. // "toUpperCase" is another option: not necessary. if(letter_grade.toUpperCase().equals("A")) QP=4*credits; if(letter_grade=="B") QP=3*credits; if(letter_grade=="C") QP=2*credits; if(letter_grade=="D") QP=credits; if(letter_grade=="F") QP=0; } public String toString() { return "Dept:" + dept + " id:" + id + " credits:" + credits + " grade:" + letter_grade + " QP:" + QP; } }
Reply With Quote