Write a Java program that reads the full name and GPA of a group of 3 students f
ID: 3585814 • Letter: W
Question
Write a Java program that reads the full name and GPA of a group of 3 students from the console and stores the data – full names and GPA’s – in single-dimensional arrays.
The program should check to be sure that each input of student name is not empty string and GPA is not negative.
After processing the data, the program prints out (to the console) the following pieces of information, each in one line:
• The average GPA of this group of students.
• Full name and GPA of the student(s) that has(have) the highest GPA
o Notes: If more than one student have the highest GPA, print out them all – one student and his/her GPA in one line.
• Full name and GPA of the student(s) that has(have) the lowest GPA
o Notes: If more than one student have the lowest GPA, print out them all – one student and his/her GPA in one line.
Explanation / Answer
StudentGPA.java
import java.util.Scanner;
public class StudentGPA {
public static void main(String[] args) {
//Creating an Array of Names and gpa
String names[] = new String[3];
double gpa[] = new double[3];
//Declaring variables
String name;
double GPA, tot = 0.0, avg = 0.0, min, max;
/*
* Creating an Scanner class object which is used to get the inputs
* entered by the user
*/
Scanner sc = new Scanner(System.in);
for (int i = 0; i < 3;) {
System.out.println(":: Student#" + (i + 1) + " ::");
while (true) {
//Getting the name and GPA entered by the user
System.out.print("Enter Name :");
name = sc.nextLine();
if (name.isEmpty()) {
System.out.println("**Invalid.Name Must not be empty **");
continue;
} else {
names[i] = name;
break;
}
}
while (true) {
System.out.print("Enter GPA :");
GPA = sc.nextDouble();
if (GPA < 0) {
System.out.println("**Invalid.GPA must not be negative **");
continue;
} else {
gpa[i] = GPA;
break;
}
}
sc.nextLine();
i++;
}
min = gpa[0];
max = gpa[0];
for (int i = 0; i < 3; i++) {
//Calculating the total GPA of all students
tot += gpa[i];
//Finding the minimum GPA among all students
if (min > gpa[i])
min = gpa[i];
//Finding the maximum GPA among all students
if (max < gpa[i])
max = gpa[i];
}
//Calculating the average GPA
avg = tot / 3;
//Displaying the average GPA
System.out.printf("The average GPA of this group of students :%.2f ", avg);
//Displaying the students who got lowest GPA
System.out.println(" ::Full name and GPA of the student(s) that has(have) the lowest GPA ::");
for (int i = 0; i < 3; i++) {
if (gpa[i] == min) {
System.out.println("Name :" + names[i] + " GPA :" + gpa[i]);
}
}
//Displaying the students who got highest GPA
System.out.println(" ::Full name and GPA of the student(s) that has(have) the highest GPA ::");
for (int i = 0; i < 3; i++) {
if (gpa[i] == max) {
System.out.println("Name :" + names[i] + " GPA :" + gpa[i]);
}
}
}
}
__________________
Output:
:: Student#1 ::
Enter Name :Sachin Tendulkar
Enter GPA :3.7
:: Student#2 ::
Enter Name :Ricky Pointing
Enter GPA :3.44
:: Student#3 ::
Enter Name :Mitchel Johnson
Enter GPA :2.8
The average GPA of this group of students :3.31
::Full name and GPA of the student(s) that has(have) the lowest GPA ::
Name :Mitchel Johnson GPA :2.8
::Full name and GPA of the student(s) that has(have) the highest GPA ::
Name :Sachin Tendulkar GPA :3.7
_____________Could you rate me well.Plz .Thank You
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.