|
|
Welcome to the Java Forums.
You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:
- have access to post topics
- communicate privately with other members (PM)
- not see advertisements between posts
- have the possibility to earn one of our surprises if you are an active member
- access many other special features that will be introduced later.
Registration is fast, simple and absolutely free so please, join our community today!
If you have any problems with the registration process or your account login, please contact us.
|
|

11-26-2007, 08:23 PM
|
|
Member
|
|
Join Date: Oct 2007
Posts: 4
|
|
|
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.
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();
}
}
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;
}
}
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.
|
|

11-26-2007, 08:46 PM
|
 |
Senior Member
|
|
Join Date: Oct 2007
Posts: 123
|
|
I've always done linked lists in Java by setting up a node class. It looks like you already have one started. I didn't look through your whole program but it will probably be something like this.
//Assuming a doubly linked list is needed
public class data Node1 ()
{
Node1 next;
Node1 previous;
public Node1(Node1 next, Node1 previous){
this.next = next;
this.previous = previous;
}
//other constructors maybe needed.
}
Depending on how you set things up, you may need setter methods for next and previous. Setting up a linked list class might no be a bad idea either.
|
|

11-30-2007, 03:52 AM
|
|
Member
|
|
Join Date: Oct 2007
Posts: 4
|
|
|
tried to setup 2 node classes but not sure exactly what is in each node. had this problem with the vectors. Had the vectors setup but never knew what was in each index. Don't really understand how it all works. Linked list seems easier to understand in a way, like you should learn that first before vectors.
|
|

12-01-2007, 01:55 AM
|
|
Senior Member
|
|
Join Date: Jul 2007
Posts: 1,189
|
|
Since both classes extend AbstractList this is pretty easy:
import java.awt.*;
import java.awt.event.*;
import java.text.NumberFormat;
import java.util.*;
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));
setSize(800,600);
setLocation(100,100);
setVisible(true);
test();
}
/** To test our alogrithms without tedious data entry. */
private void test()
{
stu[0] = new Student("Mickey Mouse", 25, 3);
stu[0].addCourse(new Course("EE", 123, 3, "B"));
stu[0].addCourse(new Course("HE", 22, 4, "A"));
stu[0].addCourse(new Course("CE", 44, 5, "C"));
stu[1] = new Student("Donald Duck", 22, 2);
stu[1].addCourse(new Course("PE", 99, 2, "C"));
stu[1].addCourse(new Course("FP", 15, 3, "A"));
stu[2] = new Student("Elmer Fudd", 44, 2);
stu[2].addCourse(new Course("PH", 101, 4, "A"));
stu[2].addCourse(new Course("ES", 220, 5, "A"));
num_students = 3;
}
public class ButtonHandler implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
JButton button = (JButton)e.getSource();
if(button == add)
{
stu[num_students].getData();
num_students++;
}
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("");
Student[] sorted = sortByGPA();
for(int j = 0; j < sorted.length; j++)
{
textArea.append("\n");
textArea.append(sorted[j] + "\n");
printCourseData(sorted[j].getList());
textArea.append("------------");
}
}
private Student[] sortByGPA()
{
Student[] sorted = getCopy();
// Sort and return this copy of non-null elements.
for(int j = 0; j < sorted.length; j++)
{
float max = sorted[j].getGPA();
int maxIndex = j;
for(int k = j+1; k < sorted.length; k++)
{
if(sorted[k].getGPA() > max)
{
max = sorted[k].getGPA();
maxIndex = k;
}
}
if(maxIndex != j)
{
// found higher gpa: swap elements
Student temp = sorted[j];
sorted[j] = sorted[maxIndex];
sorted[maxIndex] = temp;
}
}
return sorted;
}
private Student[] getCopy()
{
// count number of non-null elements
int count = 0;
for(int j = 0; j < stu.length; j++)
{
if(stu[j].getName() == null)
break;
count++;
}
Student[] temp = new Student[count];
System.arraycopy(stu, 0, temp, 0, count);
return temp;
}
private void printCourseData(LinkedList<Course> list)
{
for(int j = 0; j < list.size(); j++)
{
Course course = list.get(j);
textArea.append(course + "\n");
}
}
public static void main(String[] args)
{
new GradebookRx();
}
}
class Student
{
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>();
private LinkedList<Course> list = new LinkedList<Course>();
NumberFormat nf;
public Student()
{
this(null, 0, 0);
}
public Student(String name, int age, int numCourses)
{
this.name = name;
this.age = age;
this.num_courses = numCourses;
nf = NumberFormat.getInstance();
nf.setMaximumFractionDigits(1);
nf.setMinimumFractionDigits(1);
updateGPA();
}
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();
addCourse(cr);
}
}
public void addCourse(Course course)
{
list.add(course);
updateGPA();
}
private void updateGPA()
{
int totalQualityPoints = 0;
int totalCredits = 0;
for(int j = 0; j < list.size(); j++)
{
Course c = list.get(j);
totalQualityPoints += c.getQP();
totalCredits += c.getCredits();
}
gpa = (float)totalQualityPoints/totalCredits;
}
public String toString()
{
return "Name:" + name + " age:" + age +
" num_courses:" + num_courses +
" gpa:" + nf.format(gpa);
}
public String getName()
{
return name;
}
public float getGPA()
{
return gpa;
}
public LinkedList<Course> getList()
{
return list;
}
}
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|