Results 1 to 4 of 4
  1. #1
    cbrown08 is offline Member
    Join Date
    Oct 2007
    Posts
    4
    Rep Power
    0

    Default 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.

    Java 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();
        }
    }
    Java 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;
                   
        }
         
    }
    Java 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.

  2. #2
    ShoeNinja's Avatar
    ShoeNinja is offline Senior Member
    Join Date
    Oct 2007
    Posts
    124
    Rep Power
    0

    Default

    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.

    Java Code:
    //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.

  3. #3
    cbrown08 is offline Member
    Join Date
    Oct 2007
    Posts
    4
    Rep Power
    0

    Default

    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.

  4. #4
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    Since both classes extend AbstractList this is pretty easy:
    Java Code:
    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;
        }
    }

Similar Threads

  1. Circular Double Linked List
    By theonly in forum Advanced Java
    Replies: 3
    Last Post: 12-06-2009, 06:10 PM
  2. Linked List help
    By neobie in forum New To Java
    Replies: 8
    Last Post: 12-22-2007, 04:15 AM
  3. linked list nodes all refernce same item.
    By yllawwally in forum New To Java
    Replies: 0
    Last Post: 12-18-2007, 09:45 PM
  4. Linked List
    By rnavarro9 in forum New To Java
    Replies: 0
    Last Post: 11-29-2007, 04:42 AM
  5. Help with linked list
    By trill in forum New To Java
    Replies: 1
    Last Post: 08-07-2007, 08:29 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •