9 Replies Latest reply: Nov 16, 2007 1:35 AM by 807600 RSS

    Arrays

    807600
      package project3;
      
      public class FormEmployee extends javax.swing.JFrame {
           Employee[] people = new Employee [500];
           private int numofpeople = 0;
           private int ourperson = -1;
           private String result = people[ourperson].toString();
       
          /** Creates new form FormEmployee */
          public FormEmployee() {
              initComponents();
          }
          
          /** This method is called from within the constructor to
           * initialize the form.
           * WARNING: Do NOT modify this code. The content of this method is
           * always regenerated by the Form Editor.
           */
                            
          public void screenToArray(Employee p)
          {
          p.setFirstname(fldFirst.getText());
          p.setLastname(fldLast.getText());
          p.setID(fldID.getText());
          p.setPhone(fldPhone.getText());
          p.setYearlySalary(Double.parseDouble(fldSalary.getText()));
          p.calcBonus(Double.parseDouble(fldBonus.getText()));
          }
          
          public void arrayToScreen(Employee p)
          {
           fldFirst.setText(p.getFirstname());
           fldLast.setText(p.getLastname());
           fldID.setText(p.getID());
           fldPhone.setText(p.getPhone());
           fldSalary.setText(String.valueOf(p.getYearlySalary()));
           fldBonus.setText(String.valueOf(p.getBonus()));
          }
          
          public String getDelete()
          {
             return result; 
          }
        
          public static void sort(int[] people)
          {
              int index, indexOfNextSmallest;
              for (index = 0; index < people.length - 1; index++)
              {// Place correct value in people[index];
                  indexOfNextSmallest = indexOfSmallest(index, people);
                  interchange(index,indexOfNextSmallest, people);
              }
          }
          private static int indexOfSmallest(int startIndex, int[] people)
              {
                  int min = people[startIndex];
                  int indexOfMin =startIndex;
                  int index;
                  for (index = startIndex + 1; index < people.length; index++)
                      if (people[index] <min)
                      {
                      min = people[index];
                      indexOfMin = index;
                      }
                  return indexOfMin;
              }
      
          private static void interchange(int i, int j, int[] people)
          {
              int temp;
              temp = people;
      people[i] = people[j];
      people[j] = temp;
      }



      private void PreviousBActionPerformed(java.awt.event.ActionEvent evt) {                                         
      if (ourperson == 0)
      {
      //array limit
      }
      else
      {
      arrayToScreen(people[--ourperson]);
      pack();
      }
      }

      private void NextBActionPerformed(java.awt.event.ActionEvent evt) {                                     
      if (ourperson == numofpeople -1)
      {
      //array limit
      }
      else
      {
      arrayToScreen(people[++ourperson]);
      pack();
      }
      }

      private void PrintBActionPerformed(java.awt.event.ActionEvent evt) {                                      
      FormEmployee.sort(people);
      for (numofpeople = 0; numofpeople < people.length; numofpeople++)
      fldPrint.setText(String.valueOf(people[numofpeople]));


      }

      private void DeleteBActionPerformed(java.awt.event.ActionEvent evt) {                                       
      // deletes an array element
      //position is valid, if not, throw an exception
      if ((1 > ourperson) && (ourperson > numofpeople))
      throw new IndexOutOfBoundsException("Invalid item position: " + ourperson);
      people[ourperson] = null; // delete the entry
      numofpeople--; // decrease the entry by 1
      if(numofpeople > 0) // position to previous item
      ourperson--;
      getDelete();
      }

      private void ModifyBActionPerformed(java.awt.event.ActionEvent evt) {                                       

      }

      private void AddBActionPerformed(java.awt.event.ActionEvent evt) {                                    
      if (numofpeople == 500)
      {
      // array is full
      }

      else
      { numofpeople++;
      Employee p = new Employee();
      people[++ourperson] = p;
      screenToArray(people[ourperson]);
      pack();
      }
      }


      public static void main(String args[]) {
      java.awt.EventQueue.invokeLater(new Runnable() {
      public void run() {
      FormEmployee gui = new FormEmployee();
      gui.setVisible(true);


      }
      });
      }
      I'm having trouble with the sort method. It says (sort.int[]) in project3.FormEmployee can't be applied to (project3.Employee[])
      
      I'm trying to do a selection sort that puts in order last names of the employees. There's other problems but I'll do this first.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    
        • 1. Re: Arrays
          807600
          Your sort method takes an array of ints as a parameter but you are trying to pass an array of Employee to it.
          • 2. Re: Arrays
            807600
            How would I go about fixing that? I tried putting Employee in front of it instead of int but it creates more problems.

            Can someone move this to the Swing forum? I just saw a post on my last thread saying it would be better to put it there.

            Edited by: Program_1 on Nov 15, 2007 9:19 PM
            • 3. Re: Arrays
              807600
              Program_1 wrote:
              How would I go about fixing that? I tried putting Employee in front of it instead of int but it creates more problems.
              That doesn't mean doing that was wrong. It just means the compiler has now found other problems in your code. So you now have to work on fixing them.
              • 4. Re: Arrays
                807600
                Quacked wrote:
                Program_1 wrote:
                How would I go about fixing that? I tried putting Employee in front of it instead of int but it creates more problems.
                That doesn't mean doing that was wrong. It just means the compiler has now found other problems in your code. So you now have to work on fixing them.
                Ok. I was just making sure that this wasn't going to get messy and solve nothing. Thanks.

                null
                • 5. Re: Arrays
                  807600
                  What do I do when the operators like < and > in an if statement aren't allowable?

                  if (people[index] < min)

                  It says it can't be applied.
                  • 6. Re: Arrays
                    807600
                    It is because the people array holds Employee objects. So you are trying to say if this employee is less than 10. Does that make sense to you?
                    • 7. Re: Arrays
                      807600
                      Quacked wrote:
                      It is because the people array holds Employee objects. So you are trying to say if this employee is less than 10. Does that make sense to you?
                      Oh yeah, lol In what other ways could I express the method then?
                      • 8. Re: Arrays
                        807600
                        I don't know since I don't know what you are trying to do. Does your Employee class have some value that you are trying to search by? Does your Employee class have some method to access that value?
                        if(people[x].getValue() < min)
                        • 9. Re: Arrays
                          807600
                          public void screenToArray(Employee p)
                              {
                              p.setFirstname(fldFirst.getText());
                              p.setLastname(fldLast.getText());
                              p.setID(fldID.getText());
                              p.setPhone(fldPhone.getText());
                              p.setYearlySalary(Double.parseDouble(fldSalary.getText()));
                              p.calcBonus(Double.parseDouble(fldBonus.getText()));
                              }
                              }
                          These are the info that are added to the array people when I press the add button. I just need to sort the last names in order that go to each element. Wouldn't all the other info that's part of that given element (whatever info like phone #) switch to the correct element automatically or no?