CSCI-1320 Lab: Strings and Arrays II

Instructor: Todd Dole

Purpose Statement

This assignment gives further practice manipulating arrays, including arrays of strings and sorting. You will have two weeks to complete this assignment.

Instructions

Create a class called EmployeeData. Copy and paste the data and method at the bottom of this page into your file, before any methods. Then, create the following methods: (You will also create a main method as described below.)

(Hint: Before you begin writing each method, think through your approach and jot down a plan. What steps do you need to take?)

  1. public static void printReport()
    Iterate through the EmployeeData variables and print a report, that looks similar to the following:
                     Employee ID   Name                  Address                                                         Phone
                     ===========   ===================   =============================================================   ============
                              13   John Doe              1234 Mockingbird Lane, Abilene, TX 79605                        325-555-1234
                              ... (more data here)
    Hint: You can use printf with codes like %10d to help with formatting. I have also included a helper class that will pad a string with spaces to the right to help with the formatting.
  2. public static int[] sortedIds()

    returns a copy of employeeIds with the numbers in sorted order

    Hint #1: You can create a fresh copy of employeeId with the following code:

    int[] copiedArray = originalArray.clone();
    (Think about why this is necessary -- what happens to your report if you sort employee Id's but leave names, addresses and phone numbers unchanged?)

    Here is a helpful reference page on selection sort: https://www.geeksforgeeks.org/selection-sort-algorithm-2/

  3. public static void sortEmployeeData()
    Use selection sort to sort all the employee data by employee Id. Note that we are now sorting the original variables rather than making copies.

    Hint: Every time you swap two employee IDs, you will also need to swap the corresponding Name, Address and Phone Number so that all the data stays together.

  4. main method: Write a main method that will test each of these:
    1. First, call printReport() to print out an inital copy of the report
    2. Second, call sortedIds() and print out the results.
    3. Finally, call sortEmployeeData() and then call printReport() again to print out the (now sorted) results.

Submit your working .java file through Canvas.

Copy and paste into your EmployeeData class:

            static int[] employeeIds = { 13, 25, 267, 592608, 592324, 2023001, 2024001, 2023007, 2022013, 2022014 } ;
            static String[] employeeNames = {
               "John Doe",
               "Jeremiah Smith",
               "Jennifer O'Conner",
               "Gretchen Hughes",
               "Lebron James",
               "Elizabeth Holsteader",
               "Kevin Hanson",
               "Zoe Bartholomew",
               "Sophia Lovejoy",
               "Lucas Lovejoy"
            };
            static String[] employeeAddresses = {
                "1234 Mockingbird Ln, Abilene, TX 79605",
                "5678 Pine St, Abilene, TX 79601",
                "910 Cedar Ave, Abilene, TX 79603",
                "345 Elmwood Dr, Abilene, TX 79602",
                "789 Willow Ct, Abilene, TX 79606",
                "111 Oakridge Rd, Abilene, TX 79605",
                "222 Maple Ln, Abilene, TX 79601",
                "333 Lakeview Blvd, Abilene, TX 79606",
                "444 Sunflower Dr, Abilene, TX 79603",
                "444 Sunflower Dr, Abilene, TX 79603"
            };

            static String[] employeePhoneNumbers = {
                "325-555-1234",
                "325-555-2345",
                "325-555-3456",
                "325-555-4567",
                "325-555-5678",
                "325-555-6789",
                "325-555-7890",
                "325-555-8901",
                "325-555-9012",
                "325-555-0123"
            };

            /***
            * rightPad(String start, int total_length) returns a copy of start with spaces added to the end, to make the total length
            * of the string equal to total_length.
            **/
            public static String rightPad(String start, int total_length) {
                String newString = new String(start);
                for (int i=start.length(); i < total_length; i++) newString+=" ";
                return newString;
            }