Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Q5) Re-order the following program statements so that they define the PrintHiLow

ID: 3766388 • Letter: Q

Question

Q5)

Re-order the following program statements so that they define the PrintHiLow() method that takes an unsorted array of names and another array of associated scores, and then prints out the names and scores together, ordering them from the highest to the lowest score, which means to sort them by score. For instance if the following arrays were passed to this method:

Then the following would be printed out by the method:

        Jane: 95.0
        Julie: 75.0
        Joe: 35.0

Just re-order the lines. You don't have to add any lines. The entire method is already there:

names[i] + ": " + scores[i].ToString("#.0"));
List valueStrings = new List();
Console.WriteLine(str.Substring(6));
valueStrings.Add(scores[i].ToString("000.0") + ":" +
foreach (string str in valueStrings)
valueStrings.Reverse();
for (int i = 0; i < names.Length; ++i)
}
public static void PrintHiLow(string[] names, double[] scores) {
valueStrings.Sort();

Explanation / Answer

import java.util.ArrayList;

import java.util.Scanner;

public class Assignment5

{

    public static void initialize(ArrayList<String> names, ArrayList<Integer> scores)

    {

       // Create a Scanner object for keyboard input.

       Scanner keyboard = new Scanner (System.in);

       // Ask the user for the names and scores for 5 people.

       System.out.println("Enter the name for score #1: ");

       names.add(keyboard.next());

       System.out.println("Enter the score for score #1: ");

       scores.add(keyboard.nextInt());

      System.out.println("Enter the name for score #2: ");

       names.add(keyboard.next());

       System.out.println("Enter the score for score #2: ");

       scores.add(keyboard.nextInt());

       System.out.println("Enter the name for score #3: ");

       names.add(keyboard.next());

       System.out.println("Enter the score for score #3: ");

       scores.add(keyboard.nextInt());

       System.out.println("Enter the name for score #4: ");

       names.add(keyboard.next());

       System.out.println("Enter the score for score #4: ");

       scores.add(keyboard.nextInt());

       System.out.println("Enter the name for score #5: ");

       names.add(keyboard.next());

       System.out.println("Enter the score for score #5: ");

       scores.add(keyboard.nextInt());

    }

    public static void sort(ArrayList<String> names, ArrayList<Integer> scores)

    {

    }

    public static void display(ArrayList<String> names, ArrayList<Integer> scores)

    {

       for (int index = 0; index < names.size(); index++)

       {

          String userNames = names.get(index);

          System.out.println(names.get(index));

       }

       for (int index = 0; index < scores.size(); index++)

       {

          Integer userScores = scores.get(index);

          System.out.println(scores.get(index));

       }

    }

    public static void main(String[] args)

    {

       // Create an ArrayList for names and scores.

       ArrayList<String> names = new ArrayList<String>();

       ArrayList<Integer> scores = new ArrayList<Integer>();

       // Call the three methods.

       initialize(names, scores);

       sort(names, scores);

       display(names, scores);

    }

}

**

Loops are also very useful to learn about, esp When using arrays,

int[] array = new int[10];

Random rand = new Random();

for (int i = 0; i < array.length; i++)

    array[i] = rand.nextInt(100) + 1;

Arrays.sort(array);

System.out.println(Arrays.toString(array));

// in reverse order

for (int i = array.length - 1; i >= 0; i--)

  System.out.print(array[i] + " ");

System.out.println();

Loops are also very useful to learn about, esp When using arrays,

int[] array = new int[10];

Random rand = new Random();

for (int i = 0; i < array.length; i++)

    array[i] = rand.nextInt(100) + 1;

Arrays.sort(array);

System.out.println(Arrays.toString(array));

// in reverse order

for (int i = array.length - 1; i >= 0; i--)

  System.out.print(array[i] + " ");

System.out.println();