Answers
Hello, I have a solution for you. Implemented everything as per the requirements. Defined following things in this answer.
- Created a class GS.java
- Defined constants to represent the maximum score and the termination word.
- Defined methods to read the name and score.
- 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.
- 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
.