going from vectors to linked list?
After getting this program to work with some great help from a member here, I have come back with another question. After finnally getting this to work with vectors, now I have to change the vectors to linked lists! I've worked on this code but I believe I have changed it too much and now it doesnt run at all, so I will post the original working code with the vectors. So now, all of the vectors must be linked lists.
Code:
import java.awt.*;
import java.awt.event.*;
import java.util.Vector;
import javax.swing.*;
public class Gradebook 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 Gradebook()
{
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 data Node1 ()
{
}
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)
{
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)
{
textArea.append(stu[j] + "\n");
printCourseData(stu[j].getVector());
}
}
}
private void printCourseData(Vector<Course> v)
// private void printStudentData(Vector v)
{
for(int j = 0; j < v.size(); j++)
{
Course course = v.get(j);
textArea.append(course + "\n");
}
}
public static void main(String[] args)
{
new Gradebook();
}
}
Code:
import java.awt.*;
import java.awt.event.*;
import java.util.Vector;
import javax.swing.*;
class Course
{
private int id;
private String dept;
private int credits;
private String letter_grade;
private String stringid;
private String stringcredits;
private int QP;
private int GPA;
private int totalqp;
//private int totqp = getQP(totalqp);
//private int quality = getQP(credits, letter_grade);
//private int cred = getCredits();
//private String lgrade = getLettergrade();
//private int thegpa = getGPA(GPA);
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);
//return credits;
}
public void getLettergrade()
{
letter_grade = JOptionPane.showInputDialog(null,"Enter Letter Grade",
"Enter Letter Grade",
JOptionPane.QUESTION_MESSAGE);
}
public void getQP()
{
if(letter_grade.toUpperCase().equals("A"))
{
QP=4*credits;
totalqp = totalqp + QP;
GPA=(totalqp/credits);
}
if(letter_grade.toUpperCase().equals("B"))
{
QP=3*credits;
totalqp = totalqp + QP;
GPA=(totalqp/credits);
}
if(letter_grade.toUpperCase().equals("C"))
{
QP=2*credits;
totalqp = totalqp + QP;
GPA=(totalqp/credits);
}
if(letter_grade.toUpperCase().equals("D"))
{
QP=credits;
totalqp = totalqp + QP;
GPA=(totalqp/credits);
}
if(letter_grade.toUpperCase().equals("F"))
{
QP=0;
totalqp = totalqp + QP;
GPA=(totalqp/credits);
}
}
public String toString()
{
return "Deptartment:" + dept + "\n" + "Course ID:" + id + "\n" +
"Credits:" + credits + "\n" +"Grade:" + letter_grade + "\n" +
"QP:" + totalqp + "\n" + "GPA:" + GPA;
}
}
Code:
import java.awt.*;
import java.awt.event.*;
import java.util.Vector;
import javax.swing.*;
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<Course> v = new Vector<Course>();
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 + "\n" + "Age:" + age + "\n" +
"Number of courses:" + num_courses;
}
public String getName()
{
return name;
}
public Vector<Course> getVector()
{
return v;
}
}
The help is appreciated. Really, thanks alot.