Question

ANY HELP PLEASE. PLEASE READ THE WHOLE INSTRUCTION THANK YOU Write a program GS.java that will...

ANY HELP PLEASE. PLEASE READ THE WHOLE INSTRUCTION

THANK YOU

Write a program GS.java that will be responsible for reading in the names and grades for a group of students, then reporting some statistics about those grades. The statistics to be gathered are the number of scores entered, the highest score and the name(s) of the student(s) who earned that score, the lowest score and the name(s) of the student(s) who earned that score, and the average score for the class.


So if the names and scores are as follows:


Jake 76

Meghan 99

Liu 34

Alice 99

Joshua 25


program would print output:
There were 5 scores, averaging 66.6.

The lowest score was 25 by Penny.

The highest score was 99 by Hermione and Linus.

INSTRUCTIONS:
The program will need loops in two places: (i) to be able to read in data repeatedly, and (ii) to ensure that any erroneous input is detected and re-read until a valid input is obtained. 

The program should read in names (single-word names are sufficient here) and scores until the name is entered as "done", at which time we stop reading names and scores and report the final statistics. 

All scores should be in a valid range 0 to 100. If a score is entered outside of that range, your program should issue an appropriate message and re-read only the score (not the name, we already know that!) until a valid score is entered. 

The program will need to keep track of a running total of the number of scores entered and the total number of points (to be used later to compute the average). 

The program will need to keep track of the highest (and lowest) score seen so far and the name(s) of the students who earned that highest (and lowest) score. 

Correct initialization of the variables you will use to keep track of high and low scores is essential to get this right. Note that the first score read in should become both the high and low score after the first time through the main loop. Think about how you can initialize those variables so that they will be set to the first score on the first loop iteration without treating that iteration as a special case (that is, the body of your loop does exactly the same thing on the first iteration as it will do on all subsequent iterations). 

Note that if there is a tie (among any number of students) for the highest and/or lowest score, all students who tied the high or low should have their name included in the printouts at the end. Hint: think string concatenation. 

If no valid names and scores are entered, a special message "You did not enter any scores!" should be printed rather than potentially erroneous stats. 

Prompt for and read in both the name (which you may still assume consists of a single word) and score (which you may assume is an integer) in one step. That is, issue one prompt, then have two calls to your Scanner: one to read the name, the next to read the score. If the name is read as done, then do not attempt to read any score given on that line (nor report any error if no such score is present on the input).

Note that all comparisons of String values should use the equals method, and any comparisons of numeric values should use== and !=. 

Define named constants to represent the highest possible score and the special name that is used to stop the loop. Those definitions should be the only places in the program where I see the int literal 100 or the String literal "done". 

Remember to take the proper precautions when computing your average (which should be a double) from the scores (which are all ints). When you print your average, use a DecimalFormat object to make sure it prints with exactly one digit after the decimal point.

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

Hello, I have a solution for you. Implemented everything as per the requirements. Defined following things in this answer.

  1. Created a class GS.java
  2. Defined constants to represent the maximum score and the termination word.
  3. Defined methods to read the name and score.
  4. Implemented a main method, initialized the required variables and set up a loop which will loop again and again until user inputs ‘done’, then the stats are displayed.
  5. Comments are included explaining every statements , If you have any doubts, feel free to ask, Thanks

//GS.java

import java.text.DecimalFormat;

import java.util.Scanner;

public class GS {

      static final int MAX_SCORE = 100;

      static final String NAME_TO_END = "done";

      static Scanner scanner;

      public static void main(String[] args) {

            /**

            * Initializing scanner object

            */

            scanner = new Scanner(System.in);

            /**

            * initializing variables required for running the program

            */

            String highscorer = null, lowscorer = null, name = "";

            double highscore = 0, lowscore = 0, score = 0;

            double totalScore = 0;

            int numberOfEntries = 0;

            boolean firstEntry = true; /* to denote if any scores are added or not */

            /**

            * Loops until 'done' is entered

            */

            do {

                  name = readName();

                  if (!name.equalsIgnoreCase(NAME_TO_END)) {

                        score = readScore();

                        if (firstEntry) {

                              /**

                              * In the first input, both high and low scores are the

                              * same, by the same person

                              */

                              highscorer = name;

                              lowscorer = name;

                              highscore = score;

                              lowscore = score;

                              firstEntry = false; /*

                                                            * one input is added, so any further

                                                            * inputs are not considered as first

                                                            * entry

                                                            */

                        } else {

                              if (score < lowscore) {

                                    /**

                                    * new low score

                                    */

                                    lowscore = score;

                                    lowscorer = name;

                              } else if (score == lowscore) {

                                    /**

                                    * another person with same lowest score

                                    */

                                    lowscorer += " and " + name;

                              }

                              if (score > highscore) {

                                    /**

                                    * new high score

                                    */

                                    highscore = score;

                                    highscorer = name;

                              } else if (score == highscore) {

                                    /**

                                    * another person with same highest score

                                    */

                                    highscorer += " and " + name;

                              }

                        }

                        /**

                        * Adding to the total score, and incrementing the number of

                        * entries to calculate average

                        */

                        totalScore += score;

                        numberOfEntries++;

                  } else {

                        if (firstEntry) {

                              /**

                              * No inputs are added

                              */

                              System.out.println("You did not enter any scores!");

                        } else {

                              double average = (double) totalScore / numberOfEntries;

                              /**

                              * Formatter to format the average to maximum of one digit after point

                              */

                              DecimalFormat decimalFormat = new DecimalFormat("#.#");

                              System.out.println("There were " + numberOfEntries

                                          + " scores, averaging "

                                          + decimalFormat.format(average));

                              System.out.println("The lowest score was " + lowscore

                                          + " by " + lowscorer);

                              System.out.println("The highest score was " + highscore

                                          + " by " + highscorer);

                        }

                  }

            } while (!name.equalsIgnoreCase(NAME_TO_END));

      }

      /**

      * method to read and return the name

      */

      static String readName() {

            String name = "";

            System.out.println("Enter name (or 'done' to stop): ");

            name = scanner.nextLine();

            return name;

      }

      /**

      * method to read and return the score will ask repetitively in case of

      * invalid input

      */

      static double readScore() {

            double score = 0;

            System.out.println("Enter score (0-100):");

            try {

                  score = Double.parseDouble(scanner.nextLine());

                  if (score < 0 || score > MAX_SCORE) {

                        System.out.println("Score should be between 0-100");

                        return readScore();

                  }

                  return score;

            } catch (Exception e) {

                  System.out.println("Invalid input, try again");

                  return readScore();

            }

      }

}

/*OUTPUT*/

Enter name (or 'done' to stop):

Jake

Enter score (0-100):

600

Score should be between 0-100

Enter score (0-100):

76

Enter name (or 'done' to stop):

Meghan

Enter score (0-100):

99

Enter name (or 'done' to stop):

Liu

Enter score (0-100):

300

Score should be between 0-100

Enter score (0-100):

234

Score should be between 0-100

Enter score (0-100):

34

Enter name (or 'done' to stop):

Alice

Enter score (0-100):

99

Enter name (or 'done' to stop):

Joshua

Enter score (0-100):

25

Enter name (or 'done' to stop):

done

There were 5 scores, averaging 66.6

The lowest score was 25.0 by Joshua

The highest score was 99.0 by Meghan and Alice

Add a comment
Know the answer?
Add Answer to:
ANY HELP PLEASE. PLEASE READ THE WHOLE INSTRUCTION THANK YOU Write a program GS.java that will...
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
  • 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 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...

  • //Done in C please! //Any help appreciated! Write two programs to write and read from file...

    //Done in C please! //Any help appreciated! Write two programs to write and read from file the age and first and last names of people. The programs should work as follows: 1. The first program reads strings containing first and last names and saves them in a text file (this should be done with the fprintf function). The program should take the name of the file as a command line argument. The loop ends when the user enters 0, the...

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

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

  • CIS 22A C++ Project Exam Statistics Here is what your program will do: first it welcomes...

    CIS 22A C++ Project Exam Statistics Here is what your program will do: first it welcomes the user and displays the purpose of the program. It then prompts the user to enter the name of an input file (such as scores.txt). Assume the file contains the scores of the final exams; each score is preceded by a 5 characters student id. Create the input file: copy and paste the following data into a new text file named scores.txt DH232 89...

  • 1- Write a program to read 10 numbers and find the average of these numbers. Use...

    1- Write a program to read 10 numbers and find the average of these numbers. Use a while loop to read these numbers and keep track of input numbers read. Terminate the while loop with a sentinel value. 2- Now modify the program to find the maximum of these numbers as well. The program should print the number of elements read as input and run until -1 is entered. (-1 is the sentinel that terminates the loop and the program)...

  • I need help with this c++ question please thank you ltls. The program should urt valte....

    I need help with this c++ question please thank you ltls. The program should urt valte. 11. Lowest Score Drop Write a program that calculates the average of a group of test scores, where the lowest score in the group is dropped. It should use the following functions: void getscore() should ask the user for a test score, store it in a reference param- eter variable, and validate it. This function should be called by main once for each of...

  • THIS IS FINAL COURSE ASSIGNMENT, PLEASE FOLLOW ALL REQUIREMENTS. Hello, please help write program in JAVA...

    THIS IS FINAL COURSE ASSIGNMENT, PLEASE FOLLOW ALL REQUIREMENTS. Hello, please help write program in JAVA that reads students’ names followed by their test scores from "data.txt" file. The program should output "out.txt" file where each student’s name is followed by the test scores and the relevant grade, also find and print the highest test score and the name of the students having the highest test score. Student data should be stored in an instance of class variable of type...

  • Using C++. Please Provide 4 Test Cases. Test # Valid / Invalid Data Description of test...

    Using C++. Please Provide 4 Test Cases. Test # Valid / Invalid Data Description of test Input Value Actual Output Test Pass / Fail 1 Valid Pass 2 Valid Pass 3 Valid Pass 4 Valid Pass Question 2 Consider a file with the following student information: John Smith 99 Sarah Johnson 85 Jim Robinson 70 Mary Anderson 100 Michael Jackson 92 Each line in the file contains the first name, last name, and test score of a student. Write a...

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