Question

In: Computer Science

Java Write a program to record the GPAs of a class and find the percentage of...

Java

Write a program to record the GPAs of a class and find the percentage of students in each GPA. The program first allows the user to enter the number of students enrolled in the class, and then to enter the GPA of all students one by one. For each student, the input must be between 1 and 4 inclusively. Otherwise, the software displays a message as “Invalid number!” and asks for a new GPA for the same student

Enter number of students: 8 Enter GPA for student #1 is: 1 Enter GPA for student #2 is: 5 Invalid number!

Enter GPA for student #2 is: 2 Enter GPA for student #3 is: 3 Enter GPA for student #4 is: 4 Enter GPA for student #5 is: 4Enter GPA for student #6 is: 2 Enter GPA for student #7 is: 3 Enter GPA for student #8 is: 4

                 

The total number of students is 8. *****************
GPA 1 --- 1 student(s)
GPA 2 --- 2 student(s)

GPA 3 --- 2 student(s)
GPA 4 --- 3 student(s)
******************
The percentage of students with GPA (1) is (12.5%). The percentage of students with GPA (2) is (25.0%). The percentage of students with GPA (3) is (25.0%). The percentage of students with GPA (4) is (37.5%).

Note: you should trace the GPA list just once. So, you should use an array GPAFreq with four elements; to store the frequencies of each GPA from 1 to 4.

Solutions

Expert Solution

The program is given below: that take number of students from user, take GPA for student from user (between 1 to 4 inclusively), if entered GPA is not in between 1 to 4 inclusively then ask again, print total number of students, print GPA frequency, then print percentage of student in GPA.

import java.util.*;
public class Main
{
   public static void main(String[] args) {
   int n,i;
   Scanner sc=new Scanner(System.in);
  
   //take number of students from user
   System.out.print("Enter number of students: ");
   n=sc.nextInt();
  
   int[] GPA_s=new int[n];
   int[] GPAFreq=new int[4];
  
   //execute for loop upto number of students
   for(i=0;i<n;i++)
   { //take GPA for student from user between 1 to 4 inclusively
   System.out.printf("Enter GPA for student #%d is: ",(i+1));
   GPA_s[i]=sc.nextInt();
     
   //if entered GPA is not in between 1 to 4 inclusively
   while(!(GPA_s[i]>=1 && GPA_s[i]<=4))
   { //then print Invalid number
   System.out.println("Invalid number!");
   //ask again
   System.out.printf("Enter GPA for student #%d is: ",(i+1));
   GPA_s[i]=sc.nextInt();
   }
   //if GPA is 1
   if(GPA_s[i]==1)
   { //increment GPAFreq[0] by 1
   GPAFreq[0]+=1;
   }
   //if GPA is 2
   else if(GPA_s[i]==2)
   { //increment GPAFreq[1] by 1
   GPAFreq[1]+=1;
   }
   //if GPA is 3
   else if(GPA_s[i]==3)
   { //increment GPAFreq[2] by 1
   GPAFreq[2]+=1;
   }
   //if GPA is 4
   else if(GPA_s[i]==4)
   { //increment GPAFreq[3] by 1
   GPAFreq[3]+=1;
   }
   }
   //print total number of students
   System.out.println("\nThe total number of students is "+n+".");
   System.out.println("*****************");
   //print GPA frequency
   for(i=0;i<4;i++)
   {
   System.out.println("GPA "+(i+1)+" --- "+GPAFreq[i]+" student(s)");
   }
   System.out.println("*****************");
   //print percentage of student in GPA
   for(i=0;i<4;i++)
   {
   float ans=((GPAFreq[i]*100)/(float)n);
   System.out.printf("\nThe percentage of students with GPA (%d) is (%.1f%%).",i+1,ans);
   }
   }
}

The screenshot of code is given below:

Output:


Related Solutions

Write program#2 upload .java file. 2A) Write a java program that uses the Random class to...
Write program#2 upload .java file. 2A) Write a java program that uses the Random class to generate a number in the range 21 to 64.  Print the generated number. 2B) Using the Random class and nextInt(6), rolls two die generating two random numbers each in the range 1 through 6. Total by adding the two values together. Print the value of each die, and the total value. 2C) Using the Math class and sqrt(num), calculate the square root of integer twenty-two...
Write program#2 upload .java file. 2A) Write a java program that uses the Random class to...
Write program#2 upload .java file. 2A) Write a java program that uses the Random class to generate a number in the range 21 to 64.  Print the generated number. 2B) Using the Random class and nextInt(6), rolls two die generating two random numbers each in the range 1 through 6. Total by adding the two values together. Print the value of each die, and the total value. 2C) Using the Math class and sqrt(num), calculate the square root of integer twenty-two...
Write a java program that has a class named Octagon that extends the class Circ and...
Write a java program that has a class named Octagon that extends the class Circ and implements Comparable (compare the object's area) and Cloneable interfaces. Assume that all the 8 sides of the octagon are of equal size. Your class Octagon, therefore, must represent an octagon inscribed into a circle of a given radius (inherited from Circle) and not introduce any new class variables. Provide constructors for clas Octagon with no parameters and with 1 parameter radius. Create a method...
[For Java Programming: Objects Class] Write a Java application program that prompts the user to enter...
[For Java Programming: Objects Class] Write a Java application program that prompts the user to enter five floating point values from the keyboard (warning: you are not allowed to use arrays or sorting methods in this assignment - will result in zero credit if done!). Have the program display the five floating point values along with the minimum and maximum values, and average of the five values that were entered. Your program should be similar to (have outputted values be...
Java Program to write a Fraction class that models a mathematical fraction. The fraction class needs...
Java Program to write a Fraction class that models a mathematical fraction. The fraction class needs to support simple arithmetic functions including taking the sum, difference, and the product of the division of the two. Do not include a main() method in the Fraction class. The Fraction class will implement the following class methods: Fraction add (Fraction f1, Fraction f2); // f1 + f2 and returns a new Fraction Fraction sub (Fraction f1, Fraction f2); // f1 - f2 and...
Write a java program StudentDriver class to test the methods of the Student class. Use data:...
Write a java program StudentDriver class to test the methods of the Student class. Use data: Mary Brown 1234 John Jones 5678 Maty Jones 1234 Note: we are including Mary Jones twice so we can test the "equals" method. We can test "compareTo", by test student1 and student2, then student2 and student3. Just submit your driver class as text file. Make sure you also test addQuiz, getTotalScore, getAverage, and toString. Don't worry about testing all of the sets and gets......
Write a Java program such that it consists of 2 classes: 1. a class that serves...
Write a Java program such that it consists of 2 classes: 1. a class that serves as the driver (contains main()) 2. a class that contains multiple private methods that compute and display a. area of a triangle (need base and height) b area of a circle (use named constant for PI) (need radius) c. area of rectangle (width and length) d. area of a square (side) e. surface area of a solid cylinder (height and radius of base) N.B....
Write a program in java that does the following: Create a StudentRecord class that keeps the...
Write a program in java that does the following: Create a StudentRecord class that keeps the following information for a student: first name (String), last name (String), and balance (integer). Provide proper constructor, setter and getter methods. Read the student information (one student per line) from the input file “csc272input.txt”. The information in the file is listed below. You can use it to generate the input file yourself, or use the original input file that is available alone with this...
Write a Java program such that it consists of 2 classes: 1. a class that serves...
Write a Java program such that it consists of 2 classes: 1. a class that serves as the driver (contains main()) 2. a class that contains multiple private methods that compute and display a. area of a triangle (need base and height) b area of a circle (use named constant for PI) (need radius) c. area of rectangle (width and length) d. area of a square (side) e. surface area of a solid cylinder (height and radius of base) N.B....
Java programming Write the max-heapify code and test it and then write the program to find...
Java programming Write the max-heapify code and test it and then write the program to find the three largest values of the array without sorting the entire array to get values.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT