Question

Write you code in a class named HighScores, in file named HighScores.java. Write a program that...

Write you code in a class named HighScores, in file named HighScores.java.

Write a program that records high-score data for a fictitious game. The program will ask the user to enter five names, and five scores. It will store the data in memory, and print it back out sorted by score.

The output from your program should look approximately like this:

Enter the name for score #1: Suzy

Enter the score for score #1: 600

Enter the name for score #2: Kim

Enter the score for score #2: 9900

Enter the name for score #3: Bob

Enter the score for score #3: 1012

Enter the name for score #4: Armando

Enter the score for score #4: 8000

Enter the name for score #5: Tim

Enter the score for score #5: 514

Top Scorers:

Kim: 9900

Armando: 8000

Bob: 1012

Suzy: 600

Tim: 514

Requirements

The data must be stored in two ArrayLists: one ArrayList of strings named names, and one ArrayList of Integers named scores. These ArrayLists must be declared in the main method.

All of the user input should be done in a method named initialize. It should have the following signature:

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

You should write a function that sorts both array lists, based on the values in the scores array list. This is one of the more conceptually challenging parts of the assignment. You want to sort the scores array list, and simultaneously alter the names array list so that the names continue to line up with their respective scores by index. In the example data above, when the score 9900 moves to the first element of the scores array list, the name “Kim” should also be moved to the top of the names array list. The function should have the following signature:

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

Finally you should write a method that displays the contents of the two arraylists. It should have the following signature:

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

The main method should be very short. It should just declare and initialize the two arraylists and then invoke these three methods.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

HighScores.java

import java.util.ArrayList;

import java.util.Scanner;

public class HighScores {

public static void main(String[] args) {

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

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

//reading names and scores

initialize(names, scores);

//sorting the lists based on score.

sort(names, scores);

//displaying the contents of both arrayist

display(names, scores);

}

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

Scanner scan = new Scanner(System.in);

for(int i=1; i<=5; i++){

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

names.add(scan.next());

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

scores.add(scan.nextInt());

}

}

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

int n = names.size();

int temp = 0;

String tempStr = "";

for(int i=0; i < n; i++){

for(int j=1; j < (n-i); j++){

//checking max score and sorting based on it.

if(scores.get(j-1) < scores.get(j)){

//swap the elements!

//swapping scores

temp = scores.get(j-1);

scores.set(j-1,scores.get(j));

scores.set(j, temp);

//swapping names

tempStr = names.get(j-1);

names.set(j-1,names.get(j));

names.set(j, tempStr);

}

}

}

}

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

System.out.println("Top Scorers:");

//printing the elements up to the size of both arrays.

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

System.out.println(names.get(i)+": "+scores.get(i));

}

}

}

Output:

Enter the name for score #1:
Suzy
Enter the score for score #1:
600
Enter the name for score #2:
Kim
Enter the score for score #2:
9900
Enter the name for score #3:
Bob
Enter the score for score #3:
1012
Enter the name for score #4:
Armando
Enter the score for score #4:
8000
Enter the name for score #5:
Tim
Enter the score for score #5:
514
Top Scorers:
Kim: 9900
Armando: 8000
Bob: 1012
Suzy: 600
Tim: 514

Add a comment
Know the answer?
Add Answer to:
Write you code in a class named HighScores, in file named HighScores.java. Write a program that...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Hi this is C++, I'm really struggle with it please help me.... ************************ Here is the...

    Hi this is C++, I'm really struggle with it please help me.... ************************ Here is the original high score program,: Write a program that records high-score data for a fictitious game. The program will ask the user to enter five names, and five scores. It will store the data in memory, and print it back out sorted by score. The output from your program should look exactly like this: Enter the name for score #1: Suzy Enter the score for...

  • Complete the following Java class. In this class, inside the main() method, user first enters the...

    Complete the following Java class. In this class, inside the main() method, user first enters the name of the students in a while loop. Then, in an enhanced for loop, and by using the method getScores(), user enters the grades of each student. Finally, the list of the students and their final scores will be printed out using a for loop. Using the comments provided, complete the code in methods finalScore(), minimum(), and sum(). import java.util.Scanner; import java.util.ArrayList; public class...

  • Code in java Using the template below: public class Lab03 { public static void main(String[] args)...

    Code in java Using the template below: public class Lab03 { public static void main(String[] args) { ArrayList<Integer> list1 = new ArrayList<Integer>(); Collections.addAll(list1, 1, 3, 5, 5 ); ArrayList<Integer> list2 = new ArrayList<Integer>(); Collections.addAll(list2, 3, 7, 3, 2, 4 ); ArrayList<Integer> result1= uniqueUnion(list1,list2); System.out.println(result1); Integer[] array = new Integer[]{ 29, 28, 27, 16, 15, -14, 3, -2, 2}; ArrayList<Integer> arrayList = new ArrayList<Integer>(Arrays.asList(array)); System.out.printf("The average is: %.2f%n", averagePositive(arrayList)); } // end main public static ArrayList<Integer> uniqueUnion(ArrayList<Integer> list1, ArrayList<Integer> list2) {...

  • Program Overview This brief exercise is designed for you to consider how reference variables behave when...

    Program Overview This brief exercise is designed for you to consider how reference variables behave when you are passing them as arguments by-value. Instructions Name your class References.java. 1. Implement the following methods. 1.1 Method name: readStudentNames Purpose: This method accepts an array of strings as a parameter. It loops through the array and asks the user to input names. It populates the array of strings with the names. Parameters: an array of Strings, stringArray Return type: void In the...

  • #PLEASE WRITE THE CODE IN JAVA! THANK YOU IN ADVANCE! Write a program that manages a...

    #PLEASE WRITE THE CODE IN JAVA! THANK YOU IN ADVANCE! Write a program that manages a list of up to 10 players and their high scores in the computer's memory. Use two arrays to manage the list. One array should store the players' names, and the other array should store the players' high scores. Use the index of the arrays to correlate the names with the scores. Your program should support the following features: a. Add a new player and...

  • Write a test program that prompt the user to enter seven numbers, stores them in an...

    Write a test program that prompt the user to enter seven numbers, stores them in an array list and do the following: 1. Displays its elements (numbers) in increasing order by invoking a method with the following header that sorts the array: public static void sort(ArrayList<Integer>list) 2. Write a method that returns and displays the sum of all numbers (elements) of the ArrayList. Using the following header: public static double sum(ArrayList<Integer>list) 3. Write a method that removes the duplicate numbers...

  • *Java* Hi. I need some help with creating generic methods in Java. Write a program GenMethods...

    *Java* Hi. I need some help with creating generic methods in Java. Write a program GenMethods that has the following generic methods: (1) Write the following method that returns a new ArrayList. The new list contains the nonduplicate (i.e., distinct) elements from the original list. public static ArrayList removeDuplicates(ArrayList list) (2) Write the following method that shuffles an ArrayList. It should do this specifically by swapping two indexes determined by the use of the random class (use Random rand =...

  • Write a program that instantiates an array of integers named scores. Let the size of the...

    Write a program that instantiates an array of integers named scores. Let the size of the array be 10. The program then first invokes randomFill to fill the scores array with random numbers in the range of 0 -100. Once the array is filled with data, methods below are called such that the output resembles the expected output given. The program keeps prompting the user to see if they want to evaluate a new set of random data. Find below...

  • Implement the classes in the following class diagram. The Book class implements the Comparable interface. Use impl...

    Implement the classes in the following class diagram. The Book class implements the Comparable interface. Use implements Comparable<Book> in the class definition. Now, all book objects are instances of the java.lang.Comparable interface. Write a test program that creates an array of ten books. 1. Use Arrays.sort( Book[]l books) from the java.util package to sort the array. The order of objects in the array is determined using compareTo...) method. 2. Write a method that returns the most expensive book in the...

  • Write a static method named findMin that accepts an ArrayList of Person objects named list. Each...

    Write a static method named findMin that accepts an ArrayList of Person objects named list. Each Person object has private myAge and myName properties and corresponding public getAge and getName accessors. The method must return the name of the youngest person. public static String findMin(ArrayList<Person>list) {

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT