Modify the program below so that it displays the highest and lowest scores. Plea
ID: 3632339 • Letter: M
Question
Modify the program below so that it displays the highest and lowest scores. Please put single line comment with explanation next to the line modified.
//////////////////////////////////////////////////////////////
// From JAVA PROGRAMMING: FROM THE BEGINNING, by K. N. King //
// Copyright (c) 2000 W. W. Norton & Company, Inc. //
// All rights reserved. //
// This program may be freely distributed for class use, //
// provided that this copyright notice is retained. //
// //
// AverageScores2.java (Chapter 5, page 193) //
//////////////////////////////////////////////////////////////
// Computes the average of a series of scores
import jpb.*;
public class AverageScores2 {
public static void main(String[] args) {
// Prompt user to enter number of scores
SimpleIO.prompt("Enter number of scores: ");
String userInput = SimpleIO.readLine().trim();
int numberOfScores = Integer.parseInt(userInput);
System.out.println();
// Create array to hold scores
int[] scores = new int[numberOfScores];
// Prompt user to enter scores and store them in an array
for (int i = 0; i < scores.length; i++) {
SimpleIO.prompt("Enter score #" + (i + 1) + ": ");
userInput = SimpleIO.readLine().trim();
scores[i] = Integer.parseInt(userInput);
}
// Compute sum of scores
int sum = 0;
for (int i = 0; i < scores.length; i++)
sum += scores[i];
// Display average score
System.out.println(" Average score: " +
sum / scores.length);
}
}
Explanation / Answer
please rate - thanks
//////////////////////////////////////////////////////////////
// From JAVA PROGRAMMING: FROM THE BEGINNING, by K. N. King //
// Copyright (c) 2000 W. W. Norton & Company, Inc. //
// All rights reserved. //
// This program may be freely distributed for class use, //
// provided that this copyright notice is retained. //
// //
// AverageScores2.java (Chapter 5, page 193) //
//////////////////////////////////////////////////////////////
// Computes the average of a series of scores
import jpb.*;
public class AverageScores2 {
public static void main(String[] args) {
// Prompt user to enter number of scores
SimpleIO.prompt("Enter number of scores: ");
String userInput = SimpleIO.readLine().trim();
int numberOfScores = Integer.parseInt(userInput);
System.out.println();
// Create array to hold scores
int[] scores = new int[numberOfScores];
int max,min;
// Prompt user to enter scores and store them in an array
for (int i = 0; i < scores.length; i++) {
SimpleIO.prompt("Enter score #" + (i + 1) + ": ");
userInput = SimpleIO.readLine().trim();
scores[i] = Integer.parseInt(userInput);
}
//set max and min to 1st score
max=scores[0];
min=scores[0];
for (int i = 1; i < scores.length; i++) {
//and if any are larger than max or smaller than min, make that the new one
if(scores[i]>max)
max=scores[i];
else if(scores[i]<min)
min=scores[i];
}
// Compute sum of scores
int sum = 0;
for (int i = 0; i < scores.length; i++)
sum += scores[i];
// Display average score
System.out.println(" Average score: " +
sum / scores.length);
System.out.println("highest number: "+max); //output largest score
System.out.println("lowest score: "+min); //and smallest score
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.