Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    Regular Coder
    Join Date
    Jan 2004
    Location
    Maryland
    Posts
    468
    Thanks
    0
    Thanked 0 Times in 0 Posts

    How does this work? - Java

    I'm using the following code as an example to work from and I don't understand how to return parts from an array of objects. In the following code I was looking for how they did it but I can't find the method.
    Code:
    public void displayList()
      {
        for (int index = 0; index < studentList.length; index++)
        {
          String wholeName = studentList[index].getLastName() + ", " +
              studentList[index].getFirstName();
          System.out.println(Format.right(index + 1, 3) + "  " +
              Format.left(wholeName, 15) +
              Format.right(studentList[index].getAge(), 5));
        }
        System.out.println();
      }
    From this it looks like they just made a string using the array index plus the methods getLastName() and getFirstName(). But that's where the problem lies. I can't find a getFirstName() or getLastName method anywhere in the code!

    Entire code:
    Code:
    import chn.util.*;
    import apcslib.Format;
    
    /**
     *  Sample program using an array of objects
     *
     * @author     G. Peck
     * @created    July 18, 2002
     */
    public class Ages
    {
      private Student[] studentList;
    
      /**
       *  Default Constructor for the Ages object. Invokes the
       *  Ages(String fName) constructor to load the file "names.txt"
       */
      public Ages()
      {
        this("names.txt");
      }
    
      /**
       *  Constructor for the Ages object. Loads Student records from file
       *  fName.
       *
       * @param  fName  File name containing student data
       */
      public Ages(String fName)
      {
        loadFile(fName);
      }
    
      /**
       *  Displays the contents of the Student array
       */
      public void displayList()
      {
        for (int index = 0; index < studentList.length; index++)
        {
          String wholeName = studentList[index].getLastName() + ", " +
              studentList[index].getFirstName();
          System.out.println(Format.right(index + 1, 3) + "  " +
              Format.left(wholeName, 15) +
              Format.right(studentList[index].getAge(), 5));
        }
        System.out.println();
      }
    
      /**
       *  Stores the Student array data to disk. The first line of the
       *  file contains the number of Student records contained in the
       *  file. Each Student record consists of 2 Strings (lastName,
       *  firstName) and an integer (age).
       *
       * @param  outFileName  Name of output file
       */
      public void saveFile(String outFileName)
      {
        FileOutput outFile = new FileOutput(outFileName);
    
        outFile.println(studentList.length);
    
        for (int recNum = 0; recNum < studentList.length; recNum++)
        {
          outFile.println(studentList[recNum].getLastName() + " " +
              studentList[recNum].getFirstName() + " " +
              studentList[recNum].getAge());
        }
        outFile.close();
      }
    
      /**
       *  Helper method to invoke the recursive quicksort method
       */
      public void Sort()
      {
        quickSort(studentList, 0, studentList.length - 1);
      }
    
      /**
       *  Description of the Method
       *
       * @return    Description of the Returned Value
       */
      public String toString()
      {
        String report = "";
    
        for (int numStudents = 0; numStudents < studentList.length; numStudents++)
        {
          report += (numStudents + 1) + " " + studentList[numStudents].toString() + "\n";
        }
    
        return report;
      }
    
      /**
       *  Loads a file containing Student data. The first line of the file
       *  contains the number of Student records contained in the file.
       *  Each Student record consists of 2 Strings (lastName, firstName)
       *  and an integer (age).
       *
       * @param  inFileName  Name of file to be opened and read
       */
      private void loadFile(String inFileName)
      {
        int age;
        String lastName;
        String firstName;
    
        FileInput inFile = new FileInput(inFileName);
    
        int numStudents = inFile.readInt();
        studentList = new Student[numStudents];
    
        for (int recNum = 0; recNum < numStudents; recNum++)
        {
          lastName = inFile.readToken();
          firstName = inFile.readToken();
          age = inFile.readInt();
          studentList[recNum] = new Student(lastName, firstName, age);
        }
      }
    
      /**
       *  Sorts Student objects in an array between first index and last
       *  index using a quicksort algorithm.
       *
       * @param  list   an array of type Student
       * @param  first  start location in array to be sort
       * @param  last   end location in the array to be sorted
       */
      private void quickSort(Student[] list, int first, int last)
      {
        int g = first;
        int h = last;
        int midIndex;
        Student dividingValue;
    
        midIndex = (first + last) / 2;
        dividingValue = list[midIndex];
        do
        {
          while (list[g].compareTo(dividingValue) < 0)
          {
            g++;
          }
    
          while (list[h].compareTo(dividingValue) > 0)
          {
            h--;
          }
          if (g <= h)
          {
            // swap g and h
            Student temp = list[g];
            list[g] = list[h];
            list[h] = temp;
            g++;
            h--;
          }
        } while (g < h);
        if (h > first)
        {
          quickSort(list, first, h);
        }
        if (g < last)
        {
          quickSort(list, g, last);
        }
      }
    
      /**
       *  Instantiates an Ages object, displays the contents, sorts the
       *  Student records by age, redisplays the contents, and saves the
       *  resulting sorted data to disk.
       *
       * @param  args  The command line arguments (not used)
       */
      public static void main(String[] args)
      {
        Ages studentData = new Ages("names20.txt");
    
        System.out.println("Unsorted List");
        System.out.println();
        studentData.displayList();
    
        studentData.Sort();
    
        System.out.println("List Sorted by Age");
        System.out.println();
        studentData.displayList();
    
        studentData.saveFile("sortedNames.txt");
      }
    }

  • #2
    New Coder
    Join Date
    Nov 2004
    Posts
    54
    Thanks
    0
    Thanked 0 Times in 0 Posts
    The reason you can't find the methods you are looking for are they are in another class. You displayed the ages class, but the array is an array of Student objects. The methods you are looking for come from the Student class.

  • #3
    Regular Coder
    Join Date
    Jan 2004
    Location
    Maryland
    Posts
    468
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Oy. They didn't give me a students class. Thanks.


  •  

    Posting Permissions

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