Write a program that calculates percentage and GPA of 5 students. Each of the students has following attributes: name, class, rollnum, and marks obtained in 5 subjects.
Updated: Feb. 27, 2021 — Training Time: 3 minutes
Overseen by: Archangel Macsika
All Training Resources
Scroll for more menu list
Topic:Generic - Java Programming
Difficulty: Advance.
Companies who previously asked this: -.
Objective: Write a program that calculates percentage and GPA of 5 students. Each of the students has following attributes: name
, class
, rollnum
, and marks
obtained in 5 subjects.
Input: GPA of 5 students.
Expected Output: Result will vary based on the user input.
Sikademy Solution
package sikademy;
/**
*
* @author Archangel Macsika
*/
import java.util.InputMismatchException;
import java.util.Scanner;
// This class calculates the percentage and GPA of a student.
public class CalculatePercentageAndGPA {
final private int totalSubjects = 5;
private String[] name = new String[5];
private int[] classno = new int[5];
private int[] roll = new int[5];
private float[] science = new float[5];
private float[] math = new float[5];
private float[] social = new float[5];
private float[] english = new float[5];
private float[] health = new float[5];
private float[] percentage = new float[5];
// This function sets the value of variables name,classno,roll,and all five subjects
private void setData(String name, int classno, int roll, float science, float math, float social, float english, float health, int number){
this.name[number] = name;
this.classno[number] = classno;
this.roll[number] = roll;
this.science[number] = science;
this.math[number] = math;
this.social[number] = social;
this.english[number] = english;
this.health[number] = health;
}
// This method prompts user for input
private void prompt(){
String name="";
int classNumber=0;
int roll=0;
float science=0;
float math=0;
float social=0;
float english=0;
float health=0;
for (int i = 0; i < 5; i++) {
Scanner scan = new Scanner(System.in);
try {
System.out.println("Enter the name of student " + (i+1) + ": \t");
name = scan.nextLine();
System.out.println("Enter the class of student " + (i+1) + ": \t");
classNumber = scan.nextInt();
System.out.println("Enter the roll number of studnet " + (i+1) + 1
+ ": \t");
roll = scan.nextInt();
System.out.println("Enter the marks in science: \t");
science = scan.nextFloat();
System.out.println("Enter the marks in math: \t");
math = scan.nextFloat();
System.out.println("Enter the marks in social: \t");
social = scan.nextFloat();
System.out.println("Enter the marks in english: \t");
english = scan.nextFloat();
System.out.println("Enter the marks in health: \t");
health = scan.nextFloat();
} catch(InputMismatchException e){
e.printStackTrace();
System.out.println("Please Enter valid data");
prompt();
}
setData(name, classNumber, roll, science, math, social, english, health, i);
}
}
// This function calculates the percentage of each student
private void calculatePercentage() {
float total = 0;
for (int number = 0; number < 5; number++) {
total = science[number] + math[number] + social[number] + english[number] + health[number];
percentage[number] = total / totalSubjects;
}
display();
}
// This function displays the percentage of each student.
private void display() {
for (int i = 0; i < 5; i++) {
System.out.println("The percentage of \t" + name[i] + "is: \t" + percentage[i]);
}
}
public static void main(String[] args) {
CalculatePercentageAndGPA calculate = new CalculatePercentageAndGPA();
calculate.calculatePercentage();
}
}