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 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...
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 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.
In the C# programming language... Write a program to perform student record manage for class IST311....
In the C# programming language... Write a program to perform student record manage for class IST311. Create student record class (Student.cs) and it should get the student information from the user and set up student record class. The program will perform recording student’s grade information and compute final grade, and then print out the student’s class information. Student class definitions: It should contain attributes as following: first name, last name, 5 labs grade, 3 grade, final grade. Its member functions...
write the program in java. Demonstrate that you understand how to use create a class and...
write the program in java. Demonstrate that you understand how to use create a class and test it using JUnit Let’s create a new Project HoursWorked Under your src folder, create package edu.cincinnatistate.pay Now, create a new class HoursWorked in package edu.cincinnatistate.pay This class needs to do the following: Have a constructor that receives intHours which will be stored in totalHrs Have a method addHours to add hours to totalHrs Have a method subHours to subtract hours from totalHrs Have...
program language: JAVA For this project, you get to design and write a WeightedCourseGrade class to...
program language: JAVA For this project, you get to design and write a WeightedCourseGrade class to keep track of a student's current grade. You also get to design and write WeightedCourseGradeDriver class that requests input from the user and interacts with the WeightedCourseGrade class. Your WeightedCourseGrade class should store the following information: Weighted subtotal (the sum of all of the categories multiplied by the grade category weight) Total category weights (the sum of all the grade category weights) Provide the...
User the Scanner class for your input Write a java program to calculate the area of...
User the Scanner class for your input Write a java program to calculate the area of a rectangle. Rectangle Area is calculated by multiplying the length by the width   display the output as follow: Length =   Width = Area = Load: 1. Design (Pseudocode ) 2. Source file (Java file, make sure to include comments) 3. Output file (word or pdf or jpig file)
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT