Question

Write a program that inputs a string of comma separated test scores from the user and...

Write a program that inputs a string of comma separated test scores from the user and prints out a histogram of the scores. The histogram should have one row on it for each distinct test score, followed by one star for each test score of that value. For example, if the tests scores in the file were 55, 80, 80, 95, 95, 95 and 98, the output to the screen should look like:

55 *

80 **

95 ***

98 *

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

Please find the code below::

ScoreHistogram.java

package classes1;

import java.util.Map.Entry;
import java.util.Scanner;
import java.util.TreeMap;

public class ScoreHistogram {
   public static void main(String[] args) {
       Scanner sc = new Scanner(System.in);
       System.out.print("Enter scores comma separated : ");
       String scores = sc.nextLine();
       String scoreRecords[] = scores.split(",");
       TreeMap<String, Integer> scoreMap = new TreeMap<>();
       for(String s : scoreRecords){
           s = s.trim();
           if(scoreMap.containsKey(s)){
               scoreMap.put(s, scoreMap.get(s)+1);
           }else{
               scoreMap.put(s, 1);
           }
       }
       System.out.println("*****************************");
       System.out.println("Score histogram is as below");
       System.out.println("*****************************");
       System.out.printf("%-20s%s\n","Score","Stars");
       for(Entry<String, Integer> entry : scoreMap.entrySet()) //printing map
       {
           System.out.printf("%-20s",entry.getKey());
           for(int i=0;i<entry.getValue();i++){
               System.out.printf("*");
           }
           System.out.println();
       }
   }
}

output:

Add a comment
Know the answer?
Add Answer to:
Write a program that inputs a string of comma separated test scores from the user and...
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
  • C++ program that reads students' names followed by their test scores (5 scores) from the given...

    C++ program that reads students' names followed by their test scores (5 scores) from the given input file, students.txt. The program should output to a file, output.txt, each student's first name, last name, followed by the test scores and the relevant grade. All data should be separated by a single space. Student data should be stored in a struct variable of type StudentType, which has four components: studentFName and studentLName of type string, an array of testScores of type int...

  • Write a complete C++ program that reads students names and their test scores from an input...

    Write a complete C++ program that reads students names and their test scores from an input text file. The program should output each student’s name followed by the test scores and the relevant grade in an output text file. It should also find and display on screen the highest/lowest test score and the name of the students having the highest/lowest test score, average and variance of all test scores. Student data obtained from the input text file should be stored...

  • For Python: Write a program that gets five integer test scores from the user, calculates their...

    For Python: Write a program that gets five integer test scores from the user, calculates their average, and prints it on the screen. Test your program with the following data: Run 1: 40, 80, 97, 32, 87 Run 2: 60, 90, 100, 99, 95

  • Write a program that asks the user to enter five test scores. The program should display...

    Write a program that asks the user to enter five test scores. The program should display a letter grade for each score and the average test score. Design the following functions in the program: calcAverage—This function should accept five test scores as arguments and return the average of the scores. determineGrade—This function should accept a test score as an argument and return a letter grade for the score (as a String), based on the following grading scale: Score Letter Grade...

  • Write a program that asks the user to enter five test scores. The program should display...

    Write a program that asks the user to enter five test scores. The program should display a letter grade for each score and the average test score. Design the following functions in the program: calcAverage—This function should accept five test scores as arguments and return the average of the scores. determineGrade—This function should accept a test score as an argument and return a letter grade for the score (as a String), based on the following grading scale: Round the average...

  • 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...

  • C++ Write a program that reads students’ names followed by their test scores from the given...

    C++ Write a program that reads students’ names followed by their test scores from the given input file. The program should output to a file, output.txt, each student’s name followed by the test scores and the relevant grade. Student data should be stored in a struct variable of type StudentType, which has four components: studentFName and studentLName of type string, testScore of type int and grade of type char. Suppose that the class has 20 students. Use an array of...

  • Write a javascript program that asks the user to enter five test scores. The program should...

    Write a javascript program that asks the user to enter five test scores. The program should display a letter grade for each score and the average test score. Write the following methods in the program: calcAverage—This method should accept five test scores as arguments and return the average of the scores. determineGrade—This method should accept a test score as an argument and return a letter grade for the score,

  • Construct a​ stem-and-leaf plot of the test scores 67 comma 72 comma 85 comma 75 comma 89 comma 89 comma 88 comma 90 com...

    Construct a​ stem-and-leaf plot of the test scores 67 comma 72 comma 85 comma 75 comma 89 comma 89 comma 88 comma 90 comma 98 comma 100.67, 72, 85, 75, 89, 89, 88, 90, 98, 100. How does the​ stem-and-leaf plot show the distribution of these​ data? Construct the​ stem-and-leaf plot. Choose the correct answer below. A. Stem Leaves 6 77 7 2 62 6 8 5 6 9 85 6 9 8 9 0 90 9 10 0 B....

  • 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...

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