Question

In: Computer Science

Write a Java program that will first ask the user how many grades they want to...

Write a Java program that will first ask the user how many grades they want to enter. Then use a do…while loop to populate an array of that size with grades entered by the user. Then sort the array. In a for loop read through that array, display the grades and total the grades. After the loop, calculate the average of those grades and display that average.

Specifications

  • Prompt the user for the number of grades they would like to find the average of. This will be the length of the array you want to create.
  • Create an array of the proper size to hold the grades the user will enter.
  • Set up a do…while loop to read in the grades that will populate the array.
  • Sort the array using the Arrays.sort method.
  • Set up a for loop to iterate through the array you just created and accumulate those grades.
  • Find the average of those grades.
  • Use: good variable names, indentations of four spaces, comments within the program code, and label the output appropriately.

Solutions

Expert Solution

import java.util.Arrays;
import java.util.Scanner;
public class ArraySumAvg{

   public static void main (String args[])
   {
       double sum=0;
      
           Scanner sc = new Scanner (System.in);
           System.out.print("Enter how many grades you want: ");
           int n=sc.nextInt();
           System.out.print("Enter Array elements: ");
           int[] arr=new int[n];
           for(int i=0;i<n;i++)
           {
               arr[i]=sc.nextInt();
           }
           //sort array
           Arrays.sort(arr);
          
           System.out.println("Array is after sort: ");
           int i=0;
          
           //populate the array using do..while loop
           do
           {  
               System.out.print(arr[i]+" ");
               i++;
           }
           while(i<n);
          
          
          
          
          
           //average of grade
           for(i=0;i<n;i++)
           {
               sum+=arr[i];
           }
           double avg=sum/n;
           System.out.println();
          
           System.out.println("Average is: "+avg);
  
   }
     
}


Related Solutions

Write a program in Java to: Ask the user how many scores they want to enter...
Write a program in Java to: Ask the user how many scores they want to enter (=> Create an array based the entered size) Ask the user to enter their scores in the quarter (Fill the array with the entered scores(assigned to elements)) ==>> Use a "for" loop Find the greatest score Calculate and show the average Show the final grade based on the average (For example A,B,C ... ) Print the scores (the elements of the array) => Use...
The JAVA program should do the following: –Ask the user for how many lines of text...
The JAVA program should do the following: –Ask the user for how many lines of text they wish to enter –Declare and initialize an array of Strings to hold the user’s input –Use a while loop to prompt for and read the Strings (lines of text) from the user at the command line. Typically, this would be a 'for' loop since we know the number of times to execute the loop based upon the number supplied by the user, but...
Python Program Write a program that will ask a user on how many input colored balls...
Python Program Write a program that will ask a user on how many input colored balls of the following codes: R-red, B-blue, W-white, G-green and O-orange -will he or she would like to enter in the program and print the total number of Red balls were encountered. Assume an uppercase and lower case letter will be accepted.
write a program i java that ask the user to enter salary, user ID and username...
write a program i java that ask the user to enter salary, user ID and username and out put them
Write java program that will ask for the user for 2 input lines and print out...
Write java program that will ask for the user for 2 input lines and print out all words that occur 1 or more times on both lines (case sensitive). Write this without arrays and method. Here is a sample run: <Output> Enter two lines to process. The quick brown fox jumps over a lazy dog The fox hound outruns the lazy dog The words that occur on both lines are: The fox lazy dog
Write a java program that will ask the user to enter integers (use loops) until -100...
Write a java program that will ask the user to enter integers (use loops) until -100 is entered. The program will find and display the greatest, the smallest, the sum and the average of the entered numbers. also write a separate driver class which will be used to run and display the greatest, the smallest, the sum and the average of the entered numbers. Thanks!!
Write a Java program that will ask the user for his/her salary (numerical integer salary) and...
Write a Java program that will ask the user for his/her salary (numerical integer salary) and then convert this numerical salary into income class. The following is a guideline to the income class used. The numeric range within parenthesis maps to the preceding class. If the user gave you a number greater than 700,000 or less than 10,000, you should print a message that the input is invalid. In this code, DO NOT USE && OPERATOR. You should use if-else....
Write a Java program that will ask the user for his/her mark (numerical integer mark) and...
Write a Java program that will ask the user for his/her mark (numerical integer mark) and then convert this numerical mark into letter grades. The following is a guideline to the grading scale used. The numeric range within parenthesis maps to the preceding letter grade. If the user gave you a number greater than 100 or less than 0, you should print a message that the input is invalid. In this code, DO NOT USE && OPERATOR. You should use...
Create a C++ program that will ask the user for how many test scores will be...
Create a C++ program that will ask the user for how many test scores will be entered. Setup a while loop with this loop iteration parameter. (no fstream) The data needs to include the student’s first name, student number test score the fields should be displayed with a total width of 15. The prompt should be printed with a header in the file explaining what each is: ex. First Name student number Test Score 1) mike 6456464   98 2) phill...
Write a program in Java that first asks the user to type in today's price of...
Write a program in Java that first asks the user to type in today's price of one dollar in Japanese yen, then reads U.S. dollar values and converts each to yen. Use 0 as a sentinel to denote the end of dollar input. THEN the program reads a sequence of yen amounts and converts them to dollars. The second sequence is terminated by another zero value.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT