View Single Post
  #1 (permalink)  
Old 10-31-2007, 07:07 AM
cbrown08 cbrown08 is offline
Member
 
Join Date: Oct 2007
Posts: 4
cbrown08 is on a distinguished road
Understanding Vectors
I do not quite understand vectors regardless of how many pages I read on them, I have been assigned this program in my java class, I have written the following code so far, some of it was given to me, such as the v.add(cr) in the student class, however I have no clue as to what exactly is in my vector, where it is, and how to pull the variables that I need from it, which is what I ultimately need to do. If someone could please shed some light and point me in the right direction I would appreciate it. I have three classes, gradebook(supplies a gui with 3 buttons, one of which adds a student who has characteristics and up to 5 courses, one button which is supposed to print everything about the students(which is nowhere near done yet, don't worry about that), and one just to exit. Here's my code.

Code:
import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.io.*; public class Gradebook extends JFrame { private Button add; private Button print; private Button exit; float[] gpa_array=new float[5]; private int num_students=0; Student[] stu=new Student[5]; public Gradebook() { super("Student/Course GUI"); for(int a=0;a<5;a++) stu[a]=new Student(); ButtonHandler handler = new ButtonHandler(); Container cat = getContentPane(); cat.setLayout(new FlowLayout()); add = new Button("Add Student"); add.addActionListener(handler); cat.add(add); print = new Button("Print"); print.addActionListener(handler); cat.add(print); exit = new Button("Exit"); exit.addActionListener(handler); cat.add(exit); setSize(800,600); setVisible(true); } public class ButtonHandler implements ActionListener { public void actionPerformed(ActionEvent e) { if(e.getSource()== add) { num_students++; stu[num_students].getData(); } else if(e.getSource() == print) System.out.println("Printing all courses and students"); else if(e.getSource() == exit) System.exit(0); } } public static void main(String[] args) { Gradebook app = new Gradebook(); } } import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.io.*; import java.util.Vector; public 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; 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); } } } import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.io.*; public 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() { if(letter_grade=="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; } }
Here is the exact assignment just for any clarification purposes:
3 classes
Course
Student
Gradebook(main)

The Main class will have 3 Buttons to control the following Events:

1...Add student to system (initial data--name, gender, and course load (1 to
5 courses))
A Vector in the student class will be initially set to 5 courses for
each of 5 possible students
An array in the Gradebook class will hold references to each of the 5
student objects

2...Print all student data (along with gpa) High to low based upon gpa.

3...Exit

Last edited by cbrown08 : 10-31-2007 at 10:59 AM.
Reply With Quote
Sponsored Links