Question

In: Computer Science

7.2 Lab 7 Sorting with Arrays.java For more tips, including lecture slides, demonsration code and video...

7.2 Lab 7 Sorting with Arrays.java

For more tips, including lecture slides, demonsration code and video hints, please see the Lab7 handout (part A)

For this lab you will explore using four different versions of Arrays.sort()to sort arrays:

  • Arrays.sort() for int arrays
  • Arrays.sort() for Object arrays
  • Arrays.sort() for a specified range within an int arrray
  • Arrays.sort() for a specified range within an Object arrray

Follow these steps:

  • First, make sure you have imported Arrays.java. Two arrays have been provided for you.
  • Then start by sorting ages from index 2 to the end of the array.
  • Then sort names up to and including index 2.
  • For the above note you will be using the version of Arrays.sort for a specified range within an int and String arrray
  • Print the arrays
  • Then use Arrays.sort() to sort the entire ages array (of integers).
  • Use Arrays.sort() to sort the entire names array (of Strings).

//Import Arrays

/**
* Lab Test driver for Sorting Arrays
*/

public class SortingArrays
{
public static void main(String [] args)
{   
System.out.println("Problem 1:");
//Initialize an array of ints
Integer [] ages = {34,22,29,19,25};
//Initialize an array of Strings
String [] names = {"John","Carrie","Alex","Zuri","Tom"};
  
//Sort the arrays using Arrays.sort
//Sort a sectoin of the ages array from index 2 to the end of the array
  
//Sort a section of the names array from index 0 to index 2 inclusive
  
  
  
//print the arrays
printArray(ages);
printArray(names);
  
//Now sort the entire arrays using Arrays.sort
//Sort ages completely
  
//Sort names completely
  
  
  
//print the arrays
printArray(ages);
printArray(names);
  
}
  
/**
* Print an array of integers to the screen
*/
private static void printArray(Object[] a)
{
for (int k=0; k<a.length; k++)
{
System.out.print(a[k] + " \n");
}
System.out.println();
}   
}

Solutions

Expert Solution

Solution:-

Here is the required code. Comments have been added to properly explain what is being done.

import java.util.Arrays;

public class SortingArrays {
    public static void main(String [] args)
    {
        System.out.println("Problem 1:");
//Initialize an array of ints
        Integer [] ages = {34,22,29,19,25};
//Initialize an array of Strings
        String [] names = {"John","Carrie","Alex","Zuri","Tom"};

//Sort the arrays using Arrays.sort
        // Here we have used the Arrays.sort() function to sort sections of the ages and names array. We will use the overloaded Arrays.sort() function which will take the following inputs:
        // First parameter: The function takes in the first input as the array to be sorted.
        // Second parameter: The second input to the function is the starting index of the section of the array to be sorted. This is inclusive.
        // Third parameter: The third input to the function is the end index of the section of the array to be sorted. This is exclusive.
//Sort a section of the ages array from index 2 to the end of the array
        // We have used the ages.length function as the end index because we want to sort from position 2 to the end of the array.
        Arrays.sort(ages,2,ages.length);
//Sort a section of the names array from index 0 to index 2 inclusive
        // We have used the end position as 3 because this is exclusive. Since we need to sort from positions 0 to 2, hence we need to specify the end position as (2+1) = 3.
        Arrays.sort(names,0,3);



//print the arrays
        printArray(ages);
        printArray(names);

//Now sort the entire arrays using Arrays.sort
        // To sort the entire arrays of ages and names, we will use the overloaded Arrays.sort() method which only takes in the array as a parameter.
        // This method will sort the entire array by default.
//Sort ages completely
        Arrays.sort(ages);
//Sort names completely
        Arrays.sort(names);


//print the arrays
        printArray(ages);
        printArray(names);

    }

    /**
     * Print an array of integers to the screen
     */
    private static void printArray(Object[] a)
    {
        for (int k=0; k<a.length; k++)
        {
            System.out.print(a[k] + " \n");
        }
        System.out.println();
    }
}

Related Solutions

Lab 7: Integumentary System Study skin diagrams, models, & slides, plus functions of integumentary structures
Lab 7: Integumentary System Study skin diagrams, models, & slides, plus functions of integumentary structures
Submit your presentation on Employee Motivation (7–9 slides, including speaker notes). In your presentation, address the...
Submit your presentation on Employee Motivation (7–9 slides, including speaker notes). In your presentation, address the following: Explain how managers use the motivation and performance formula (Lussier, 2017, p. 355) [performance = ability x motivation x resources] to motivate their employees. (1–2 slides) List and describe three motivation theories. (2–3 slides) Identify 3–5 rewards and recognitions organizations use to motivate employees. (2 slides) Describe two motivation strategies that managers can use to improve performance. (2 slides) In Part 2 of...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT