87.3k views
1 vote
Write a program that accepts any number of homework scores ranging in value from 0 through

10. Prompt the user for a new score if they enter a value outside of the specified range. Prompt
the user for a new value if they enter an alphabetic character. Store the values in an array.
Calculate the average excluding the lowest and highest scores. Display the average as well as the
highest and lowest scores that were discarded.

User Jameswelle
by
4.7k points

1 Answer

4 votes

Answer:

This program is written in Java programming language.

It uses an array to store scores of each test.

And it also validates user input to allow only integers 0 to 10,

Because the program says the average should be calculated by excluding the highest and lowest scores, the average is calculated as follows;

Average = (Sum of all scores - highest - lowest)/(Total number of tests - 2).

The program is as follows (Take note of the comments; they serve as explanation)

import java.util.*;

public class CalcAvg

{

public static void main(String [] args)

{

Scanner inputt = new Scanner(System.in);

// Declare number of test as integer

int numTest;

numTest = 0;

boolean check;

do

{

try

Scanner input = new Scanner(System.in);

System.out.print("Enter number of test (1 - 10): ");

numTest = input.nextInt();

check = false;

if(numTest>10

catch(Exception e)

{

check = true;

}

}

while(check);

int [] tests = new int[numTest];

//Accept Input

for(int i =0;i<numTest;i++)

{

System.out.print("Enter Test Score "+(i+1)+": ");

tests[i] = inputt.nextInt();

}

//Determine highest

int max = tests[0];

for (int i = 1; i < numTest; i++)

{

if (tests[i] > max)

{

max = tests[i];

}

}

//Determine Lowest

int least = tests[0];

for (int i = 1; i < numTest; i++)

{

if (tests[i] < least)

{

least = tests[i];

}

}

int sum = 0;

//Calculate total

for(int i =0; i< numTest;i++)

{

sum += tests[i];

}

//Subtract highest and least values

sum = sum - least - max;

//Calculate average

double average = sum / (numTest - 2);

//Print Average

System.out.println("Average = "+average);

//Print Highest

System.out.println("Highest = "+max);

//Print Lowest

System.out.print("Lowest = "+least);

}

}

//End of Program

User Conmadoi
by
5.0k points