Question

This program will ask the user to enter a number of players, then ask the user...

This program will ask the user to enter a number of players, then ask the user for each player's name and score. Once all of the players have been entered, the program will display a bar chart with each of their scores scaled as a percentage of the maximum score entered. See below for details on exactly how this should work - an example transcript of how the program should work is shown below (remember that values entered by the user are shown in BOLD and that the output should end with a new line:

Enter the number of players: 3
Enter a player name: John Wagner
Enter the score for John Wagner: 33

Enter a player name: Karen Berger
Enter the score for Karen Berger: 55

Enter a player name: Jo Duffy
Enter the score for Jo Duffy: 15

Current Scoreboard
------------------
John Wagner  ******************************
Karen Berger **************************************************
Jo Duffy     *************

If the user enters a 0 for the number of players, a goodbye message should be displayed instead:

Enter the number of players: 0
No players to display?  Goodbye!

Storing the input

You will need to create two arrays to store the data - one to hold player names and one to hold scores. The arrays will be what is known as parallel arrays - two values with the same index will be linked to each other. For example, given the transcript above, the array holding names would have the values ["John Wagner", "Karen Berger", "Jo Duffy"] and the array holding scores would have the values [33, 55, 15]`.

Once you have read the values into the two arrays, you can worry about formatting the output.

Formatting the output

The length of the bar for the player with the highest score must be 50 stars. The length of the bars for the other players is determined by using a proportion of their score to the maximum score:

numStars = ( score / maxScore ) * 50

For example, if the highest score is 10 and another player has a score of 8, then the length of the second player's bar will be (8/10)*50 - or 40 stars:

Enter the number of players: 2
Enter a player name: High Scorer
Enter the score for High Scorer: 10

Enter a player name: Low Scorer
Enter the score for Low Scorer: 8

Current Scoreboard
------------------
High Scorer **************************************************
Low Scorer  ****************************************

NOTE: Remember that if you want to use fractions in Java you have to do some extra work. You can look back at how you computed the percentage in the guess a number project for one way to solve this. Another way to solve it is to recognize that the above equation can be rewritten using a little bit of algebra as:

numStars = ( score * 50 ) / maxScore

If you do that, you don't have to convert values from int to double and back at all. In general in Java if you don't have to convert between int and double types it is better not to.

NOTE 2: Notice that the bars all line up on the left hand side, with a single space after the longest name that the user entered. There are a number of ways to solve this problem - one way to do it is to use a loop to append spaces to the end of the shorter names to "pad" them out to the right length. An algorithm to do this might be:

  1. Determine the string with the longest length, call it max.
  2. Start with a variable pos = 0.
  3. With the name stored at index pos, loop to append spaces until its length is the same as the length of max.
  4. Increment pos by 1.
  5. Repeat steps 3 and 4 until all of the strings are the same length.

NOTE 3: Your solution must be able to handle names of length 0 and scores of 0. You can assume that values entered by the user for scores will be non-negative. In the case of scores of 0, no stars should be printed - be careful of division by zero errors when determining your proportions! In the case of names of length 0, no name will be printed but the bar may be.

An example where the player name is left blank would be:

Enter the number of players: 1
Enter a player name: 
Enter the score for : 10

Current Scoreboard
------------------
 **************************************************

And an example where the player score is set to 0 (and the max score is also zero):

Enter the number of players: 1
Enter a player name: Jo Duffy
Enter the score for Jo Duffy: 0

Current Scoreboard
------------------
Jo Duffy
0 0
Add a comment Improve this question Transcribed image text
Answer #1

CODE:-

import java.util.*;
class Prac{
   public static void main(String[] args) {
       Scanner sc = new Scanner(System.in);
       System.out.print("Enter the number of players: ");
       int n = sc.nextInt();
       sc.nextLine();
       String[] names = new String[n];
       int[] scores = new int[n];
       int maxScore = Integer.MIN_VALUE;
       int maxName = 0;
       if(n == 0)
           System.out.println("No players to display? Good bye!");
       else{
           for(int i = 0;i<n;i++){
               //Ask the user to enter name
               System.out.print("Enter a player name: ");
               names[i] = sc.nextLine();
               // Ask the user to enter score
               System.out.print("Enter the score for "+names[i]+": ");
               scores[i] = sc.nextInt();
               if(maxScore < scores[i])
                   maxScore = scores[i];
               if(maxName < names[i].length())
                   maxName = names[i].length();
               System.out.println();
               // This is to clear buffer , otherwise it will take line break as name
               sc.nextLine();
           }
           System.out.println();
           System.out.println("Current Scoreboard");
           System.out.println("------------------");
           for(int i = 0;i<n;i++){
               // Add spaces at the end to shorter names
               if(names[i].length() < maxName){
                   for(int j = names[i].length();j<maxName;j++){
                       names[i] += " ";
                   }
               }
               System.out.print(names[i] + " ");
               //Handling divide by zero
               if(maxScore > 0){
                   int numOfStars = (int)((scores[i]*50) / maxScore);
                   for(int k = 0; k<numOfStars;k++){
                       System.out.print("*");
                   }
                   System.out.println();
               }
           }
       }
   }
}

OUTPUT:-

NOTE:- If you have any doubts, please comment below. Please give positive rating. THANK YOU!!!

Add a comment
Know the answer?
Add Answer to:
This program will ask the user to enter a number of players, then ask the user...
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
  • #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...

  • I need to update this C++ code according to these instructions. The team name should be...

    I need to update this C++ code according to these instructions. The team name should be "Scooterbacks". I appreciate any help! Here is my code: #include <iostream> #include <string> #include <fstream> using namespace std; void menu(); int loadFile(string file,string names[],int jNo[],string pos[],int scores[]); string lowestScorer(string names[],int scores[],int size); string highestScorer(string names[],int scores[],int size); void searchByName(string names[],int jNo[],string pos[],int scores[],int size); int totalPoints(int scores[],int size); void sortByName(string names[],int jNo[],string pos[],int scores[],int size); void displayToScreen(string names[],int jNo[],string pos[],int scores[],int size); void writeToFile(string...

  • Write a program that allows the user to enter a series of exam scores. The number...

    Write a program that allows the user to enter a series of exam scores. The number of scores the user can enter is not fixed; they can enter any number of scores they want. The exam scores can be either integers or floats. Then, once the user has entered all the scores they want, your program will calculate and print the average of those scores. After printing the average, the program should terminate. You need to use a while loop...

  • JAVA program. I have created a game called GuessFive that generates a 5-digit random number, with...

    JAVA program. I have created a game called GuessFive that generates a 5-digit random number, with individual digits from 0 to 9 inclusive. (i.e. 12345, 09382, 33044, etc.) The player then tries to guess the number. With each guess the program displays two numbers, the first is the number of correct digits that are in the proper position and the second number is the sum of the correct digits. When the user enters the correct five-digit number the program returns...

  • [JAVA/chapter 7] Assignment: Write a program to process scores for a set of students. Arrays and...

    [JAVA/chapter 7] Assignment: Write a program to process scores for a set of students. Arrays and methods must be used for this program. Process: •Read the number of students in a class. Make sure that the user enters 1 or more students. •Create an array with the number of students entered by the user. •Then, read the name and the test score for each student. •Calculate the best or highest score. •Then, Calculate letter grades based on the following criteria:...

  • Write a program that prompts the user to enter a list of names. Each person's name...

    Write a program that prompts the user to enter a list of names. Each person's name is separated from the next by a semi-colon and a space (: and the names are entered lastName, firstName (i.e. separated by ',). Your program should then print out the names, one per line, with the first names first followed by the last names. A sample run of your program should look like Please enter your list of names: Epstein, Susan; St. John, Katherine;...

  • Write a C++ program that asks user number of students in a class and their names....

    Write a C++ program that asks user number of students in a class and their names. Number of students are limited to 100 maximum. Then, it will ask for 3 test scores of each student. The program will calculate the average of test scores for each student and display with their names. Then, it will sort the averages in descending order and display the sorted list with students’ names and ranking. Follow the Steps Below Save the project as A4_StudentRanking_yourname....

  • Java Program: In this project, you will write a program called GradeCalculator that will calculate the...

    Java Program: In this project, you will write a program called GradeCalculator that will calculate the grading statistics of a desired number of students. Your program should start out by prompting the user to enter the number of students in the classroom and the number of exam scores. Your program then prompts for each student’s name and the scores for each exam. The exam scores should be entered as a sequence of numbers separated by blank space. Your program will...

  • Write a Python program that keeps reading in names from the user, until the user enters...

    Write a Python program that keeps reading in names from the user, until the user enters 0. Once the user enters 0, you should print out all the information that was entered by the user. Use this case to test your program: Input: John Marcel Daisy Samantha Nelson Deborah 0 ================ Output: John Marcel Daisy 25 Samantha Nelson Deborah Hints: Create an array Names to hold the input names. Use the Names.append(x) function to add a new name to the...

  • Question: - write a C++ program that asks user to enter students' quiz scores, calculate the...

    Question: - write a C++ program that asks user to enter students' quiz scores, calculate the total score for each students and average for all. Note: - number of students: 1 through 5. That is, at least one student and up to 5. - number of quizes: 8 through 10. That is, at least 8 quizes and up to 10. - quiz score range: 0 through 100. - when entering quiz scores, if user enters -1, that means the user...

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