Computer Science Canada

Class List GUI

Author:  eNc [ Sun Nov 20, 2005 4:04 pm ]
Post subject:  Class List GUI

Ok so in the following code I am trying to change all the JButtons labels. This is how it works. The JButton labels are corresponding to a class list. The class list can be sorted, randomized and searched. I would like to have the class list randomized and displayed when the "Ownage" button is pressed. I have tried to do so in the following code, but I only can get the default class list to appear in the GUI. Please help me to see what I am doing wrong.

code:

import java.io.*;
import java.util.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

//start class student
class student
{
        //declaring instance fields
        String firstName;       
        String lastName;
        int age;
       
        //student constructor
        public student(String l, String f, int a)
        {
                lastName = l;
                firstName = f;
                age = a;
        }//end student constructor
       
        //Part A - creates createList() method
        //creates class list and store into array studentArray
        //source from studentList.txt
        public static student[] createList()
        {
                String first, last = null;
                int age = 0;
               
                student [] studentArray = new student[33];
               
      try
      {
        FileReader fr = new FileReader("C:\\java\\programs\\chanto\\studentList.txt");
        BufferedReader br = new BufferedReader(fr);   
        String line;
        for(int i = 0; i<=studentArray.length; i++)
        {
         line =br.readLine();
                StringTokenizer st = new StringTokenizer(line,",", false);
                first = st.nextToken();
                last = st.nextToken();
                age = Integer.parseInt(st.nextToken());
                                studentArray[i] = new student(first, last, age);
         }
         br.close();
                }
      catch (Exception y){}
                return studentArray;
        }//end createList() method
       
        // Part B - creates printstudentArray method
        // display students' names and ages to screen (First Name, Last Name then "age" then Age)
        public static void printstudentArray(student[] sArray)
        {
                        for (int i = 0; i < sArray.length; i++)
                                Output.println ( sArray[i].firstName + " " + sArray[i].lastName + ", age " + sArray[i].age);
        }//end printstudentArray method        
       
        // Part C - creates random method
        //rearranges the students in the list into random order
        public static student[] random (student[] studentArray)
        {
                student temp = new student("","",0);
                for(int i = 0; i<studentArray.length; i++)
                {
                        int randNum = (int)(Math.random()*studentArray.length);
                        temp = studentArray[i];
                        studentArray[i] = studentArray[randNum];
                        studentArray[randNum] = temp;
                }              
                return studentArray;
        }//end random method
       
        //creates compareTo method
        //distinguish one student being "bigger", "smaller" or "equal" to another
        //essential for searching and sorting
        public int compareTo(Object o)
        {
                student s = (student) o;
                int result = 0;
                result = this.lastName.compareTo(s.lastName);
                if(result != 0)
                        return result;
                result = this.firstName.compareTo(s.firstName);
                if(result != 0)
                        return result;
                result = this.age-s.age;
                return result;
        }//end compareTo method

        //Part D - creates seqSearch method
        //Find a student using sequential search
        //(Can do whether or not the class list is sorted)
        public static int seqSearch(student[] studentArray, student item)
        {
                int location= -1;
                for (int i=0;i<studentArray.length;i++)
                        if (studentArray[i].compareTo(item)==0)
                                location =i;
                        return location;
        }//end seqSearch method
       
        //Part F - creates binSearch method
        //Find a student using binary search
        //(class list must be sorted for it to work properly)
        public static int binSearch (student[] studentArray, student item)
        {
                int bottom=0;
                int top= studentArray.length-1;
                int middle;
                boolean found = false;
                int location = -1;
       
                while (bottom<= top & !found)
                {
                        middle = (bottom+top)/2;
                        if (studentArray[middle].compareTo(item)==0)
                        {
                                found = true;
                                location = middle;
                        }
                        else if (studentArray[middle].compareTo(item)<0)
                                bottom=middle+1;
                        else
                                top = middle-1;
                }
                return location;
        }// end binSearch method
       
        //creates findK method
        //calculate the best value for k
        //(essential to shell sort)
        public static int findK(int n)
        {
                int k = 1;
                while (k * 3 + 1 < n)
                        k = k * 3 + 1;
                return k;
        }//end findK method
       
        //Part G - creates shellSort method
        //Sort the class list
        public static student[] shellSort (student[] studentArray)
        {
                for (int k = findK(studentArray.length); k > 0; k = (k-1) / 3)
                        for (int loop = 0; loop < k; loop++)
                                for (int top = k + loop; top < studentArray.length; top += k)
                                {
                                        int i;
                                        student item = studentArray[top];
                                        for (i = top; i > 0 + loop && item.compareTo(studentArray[i-k]) < 0; i -= k)
                                                studentArray[i] = studentArray[i-k];
                                        studentArray[i] = item;
                                }
                return studentArray;
        }
}




class Assignment02
{
        public static void main(String[] args)
        {
                classFrame f = new classFrame();
        }
}

class classFrame extends JFrame implements ActionListener
{
        student[] sArray = student.createList();
        DrawArray buttonsArray;
       
        public classFrame()
        {
                super("Title");
                                               
                JPanel cpane = (JPanel) getContentPane();
                cpane.setLayout(new BorderLayout());
               
                buttonsArray = new DrawArray(sArray);
                cpane.add(buttonsArray.buttonsPane, BorderLayout.CENTER);
               
                JButton button33 = new JButton("Ownage");
                button33.addActionListener(this);
                cpane.add(button33, BorderLayout.SOUTH);
               
                setSize(1000,300);
                setVisible(true);
        }
       
        public void actionPerformed(ActionEvent e)
        {
                if(e.getActionCommand().equals("Owange"))
                {
                        sArray = student.random(student.createList());
                        buttonsArray = new DrawArray(sArray);
                        repaint();
                }
        }
}

class DrawArray extends JComponent
{
        JPanel buttonsPane;

        public student[] setArray(student[] array)
        {
                student[] studentArray = array;
                return studentArray;
        }       
       
        public DrawArray(student[] studentArray)
        {
           buttonsPane = new JPanel();
                buttonsPane.setLayout(new GridLayout(5,6));
               
                JButton button00 = new JButton(studentArray[0].firstName+" "+studentArray[0].lastName );
                buttonsPane.add(button00);     
                JButton button01 = new JButton(studentArray[1].firstName+" "+studentArray[1].lastName );
                buttonsPane.add(button01);
                JButton button02 = new JButton(studentArray[2].firstName+" "+studentArray[2].lastName );
                buttonsPane.add(button02);
                JButton button03 = new JButton(studentArray[3].firstName+" "+studentArray[3].lastName );
                buttonsPane.add(button03);
                JButton button04 = new JButton(studentArray[4].firstName+" "+studentArray[4].lastName );
                buttonsPane.add(button04);
                JButton button05 = new JButton(studentArray[5].firstName+" "+studentArray[5].lastName );
                buttonsPane.add(button05);
                JButton button06 = new JButton(studentArray[6].firstName+" "+studentArray[6].lastName );
                buttonsPane.add(button06);
                JButton button07 = new JButton(studentArray[7].firstName+" "+studentArray[7].lastName );
                buttonsPane.add(button07);
                JButton button08 = new JButton(studentArray[8].firstName+" "+studentArray[8].lastName );
                buttonsPane.add(button08);
                JButton button09 = new JButton(studentArray[9].firstName+" "+studentArray[9].lastName );
                buttonsPane.add(button09);
                JButton button10 = new JButton(studentArray[10].firstName+" "+studentArray[10].lastName );
                buttonsPane.add(button10);           
                JButton button11 = new JButton(studentArray[11].firstName+" "+studentArray[11].lastName );
                buttonsPane.add(button11);
                JButton button12 = new JButton(studentArray[12].firstName+" "+studentArray[12].lastName );
                buttonsPane.add(button12);
                JButton button13 = new JButton(studentArray[13].firstName+" "+studentArray[13].lastName );
                buttonsPane.add(button13);
                JButton button14 = new JButton(studentArray[14].firstName+" "+studentArray[14].lastName );
                buttonsPane.add(button14);
                JButton button15 = new JButton(studentArray[15].firstName+" "+studentArray[15].lastName );
                buttonsPane.add(button15);
                JButton button16 = new JButton(studentArray[16].firstName+" "+studentArray[16].lastName );
                buttonsPane.add(button16);
                JButton button17 = new JButton(studentArray[17].firstName+" "+studentArray[17].lastName );
                buttonsPane.add(button17);
                JButton button18 = new JButton(studentArray[18].firstName+" "+studentArray[18].lastName );
                buttonsPane.add(button18);
                JButton button19 = new JButton(studentArray[19].firstName+" "+studentArray[19].lastName );
                buttonsPane.add(button19);
                JButton button20 = new JButton(studentArray[20].firstName+" "+studentArray[20].lastName );
                buttonsPane.add(button20);           
                JButton button21 = new JButton(studentArray[21].firstName+" "+studentArray[21].lastName );
                buttonsPane.add(button21);
                JButton button22 = new JButton(studentArray[22].firstName+" "+studentArray[22].lastName );
                buttonsPane.add(button22);
                JButton button23 = new JButton(studentArray[23].firstName+" "+studentArray[23].lastName );
                buttonsPane.add(button23);
                JButton button24 = new JButton(studentArray[24].firstName+" "+studentArray[24].lastName );
                buttonsPane.add(button24);
                JButton button25 = new JButton(studentArray[25].firstName+" "+studentArray[25].lastName );
                buttonsPane.add(button25);
                JButton button26 = new JButton(studentArray[26].firstName+" "+studentArray[26].lastName );
                buttonsPane.add(button26);
                JButton button27 = new JButton(studentArray[27].firstName+" "+studentArray[27].lastName );
                buttonsPane.add(button27);
                JButton button28 = new JButton(studentArray[28].firstName+" "+studentArray[28].lastName );
                buttonsPane.add(button28);
                JButton button29 = new JButton(studentArray[29].firstName+" "+studentArray[29].lastName );
                buttonsPane.add(button29);
                JButton button30 = new JButton(studentArray[30].firstName+" "+studentArray[30].lastName );
                buttonsPane.add(button30);
                JButton button31 = new JButton(studentArray[31].firstName+" "+studentArray[31].lastName );
                buttonsPane.add(button31);
                JButton button32 = new JButton(studentArray[32].firstName+" "+studentArray[32].lastName );
                buttonsPane.add(button32);
                repaint();
        }
}


Thanks.

Author:  wtd [ Sun Nov 20, 2005 4:11 pm ]
Post subject: 

Without even looking at the logic, you have classes that don't begin with a capital letter, and you have plenty of redundant comments. Smile

Author:  eNc [ Sun Nov 20, 2005 4:13 pm ]
Post subject: 

lol yea thats the thing, its not my code I'm using its someone elses but i have to then implement a GUI for it.

Author:  eNc [ Sun Nov 20, 2005 4:15 pm ]
Post subject: 

sry i forgot i need to add the student list text file in order for you to have it work.

Author:  wtd [ Sun Nov 20, 2005 4:20 pm ]
Post subject: 

Java:
      JButton button00 = new JButton(studentArray[0].firstName+" "+studentArray[0].lastName );
      buttonsPane.add(button00);   
      JButton button01 = new JButton(studentArray[1].firstName+" "+studentArray[1].lastName );
      buttonsPane.add(button01);
      JButton button02 = new JButton(studentArray[2].firstName+" "+studentArray[2].lastName );
      buttonsPane.add(button02);
      JButton button03 = new JButton(studentArray[3].firstName+" "+studentArray[3].lastName );
      buttonsPane.add(button03);
      JButton button04 = new JButton(studentArray[4].firstName+" "+studentArray[4].lastName );
      buttonsPane.add(button04);
      JButton button05 = new JButton(studentArray[5].firstName+" "+studentArray[5].lastName );
      buttonsPane.add(button05);
      JButton button06 = new JButton(studentArray[6].firstName+" "+studentArray[6].lastName );
      buttonsPane.add(button06);
      JButton button07 = new JButton(studentArray[7].firstName+" "+studentArray[7].lastName );
      buttonsPane.add(button07);
      JButton button08 = new JButton(studentArray[8].firstName+" "+studentArray[8].lastName );
      buttonsPane.add(button08);
      JButton button09 = new JButton(studentArray[9].firstName+" "+studentArray[9].lastName );
      buttonsPane.add(button09);
      JButton button10 = new JButton(studentArray[10].firstName+" "+studentArray[10].lastName );
      buttonsPane.add(button10);     
      JButton button11 = new JButton(studentArray[11].firstName+" "+studentArray[11].lastName );
      buttonsPane.add(button11);
      JButton button12 = new JButton(studentArray[12].firstName+" "+studentArray[12].lastName );
      buttonsPane.add(button12);
      JButton button13 = new JButton(studentArray[13].firstName+" "+studentArray[13].lastName );
      buttonsPane.add(button13);
      JButton button14 = new JButton(studentArray[14].firstName+" "+studentArray[14].lastName );
      buttonsPane.add(button14);
      JButton button15 = new JButton(studentArray[15].firstName+" "+studentArray[15].lastName );
      buttonsPane.add(button15);
      JButton button16 = new JButton(studentArray[16].firstName+" "+studentArray[16].lastName );
      buttonsPane.add(button16);
      JButton button17 = new JButton(studentArray[17].firstName+" "+studentArray[17].lastName );
      buttonsPane.add(button17);
      JButton button18 = new JButton(studentArray[18].firstName+" "+studentArray[18].lastName );
      buttonsPane.add(button18);
      JButton button19 = new JButton(studentArray[19].firstName+" "+studentArray[19].lastName );
      buttonsPane.add(button19);
      JButton button20 = new JButton(studentArray[20].firstName+" "+studentArray[20].lastName );
      buttonsPane.add(button20);     
      JButton button21 = new JButton(studentArray[21].firstName+" "+studentArray[21].lastName );
      buttonsPane.add(button21);
      JButton button22 = new JButton(studentArray[22].firstName+" "+studentArray[22].lastName );
      buttonsPane.add(button22);
      JButton button23 = new JButton(studentArray[23].firstName+" "+studentArray[23].lastName );
      buttonsPane.add(button23);
      JButton button24 = new JButton(studentArray[24].firstName+" "+studentArray[24].lastName );
      buttonsPane.add(button24);
      JButton button25 = new JButton(studentArray[25].firstName+" "+studentArray[25].lastName );
      buttonsPane.add(button25);
      JButton button26 = new JButton(studentArray[26].firstName+" "+studentArray[26].lastName );
      buttonsPane.add(button26);
      JButton button27 = new JButton(studentArray[27].firstName+" "+studentArray[27].lastName );
      buttonsPane.add(button27);
      JButton button28 = new JButton(studentArray[28].firstName+" "+studentArray[28].lastName );
      buttonsPane.add(button28);
      JButton button29 = new JButton(studentArray[29].firstName+" "+studentArray[29].lastName );
      buttonsPane.add(button29);
      JButton button30 = new JButton(studentArray[30].firstName+" "+studentArray[30].lastName );
      buttonsPane.add(button30);
      JButton button31 = new JButton(studentArray[31].firstName+" "+studentArray[31].lastName );
      buttonsPane.add(button31);
      JButton button32 = new JButton(studentArray[32].firstName+" "+studentArray[32].lastName );
      buttonsPane.add(button32);


How about:

Java:
for (student s : studentArray)
   buttonsPane.add(new JButton(String.format("%s %s", s.firstName, s.lastName)));


Oh, and see how you're directly accessing the firstName and lastName members? That shouldn't be possible. They should be private. According to your code, they are private.

As for your searching and sorting methods... you do know there are methods in the API that will do that. Take a look at the Arrays class.

Author:  wtd [ Sun Nov 20, 2005 4:35 pm ]
Post subject: 

Ah. Not your code. I see. Still, tell your friend to try writing that in Java next time. Smile

Author:  wtd [ Sun Nov 20, 2005 4:40 pm ]
Post subject: 

Java:
     try
      {
         FileReader fr = new FileReader("C:\\java\\programs\\chanto\\studentList.txt");
         BufferedReader br = new BufferedReader(fr);
         String line;
         for(int i = 0; i<=studentArray.length; i++)
          {
             line =br.readLine();
             StringTokenizer st = new StringTokenizer(line,",", false);
             first = st.nextToken();
             last = st.nextToken();
             age = Integer.parseInt(st.nextToken());
            studentArray[i] = new student(first, last, age);
         }
         br.close();
      }
      catch (Exception y){}


This makes me want to cry.

The original author of this code is just flat out ignoring all exceptions coming from this code.

I would refuse to work with this drek, if I were you. There are very good reasons for doing so, and if this is required for a class, and I were the teacher, I would give you more points for refusing to continue, than for trying to slog through it.

Author:  eNc [ Sun Nov 20, 2005 4:49 pm ]
Post subject: 

lol ok. Umm we were supposed to write the searching and sorting methods. and ty for the help with the excessive buttons. But i still need some kind of way to display this and display changes.

Author:  wtd [ Sun Nov 20, 2005 5:01 pm ]
Post subject: 

I don't see anywhere in that code that you're actually randomizing the order of the student records.

Author:  eNc [ Sun Nov 20, 2005 5:08 pm ]
Post subject: 

So like in the action event method i call the random method.

sArray = student.random(student.createList());

Author:  wtd [ Sun Nov 20, 2005 5:26 pm ]
Post subject: 

Ok, I missed that. The reason, by the way is the huge number of extraneous comments making it exceedingly difficult to read this code. No, I never stop offering style criticism.

Have you tested this in a simple text-based UI to see if the random method is actually doing what it says it is?


: