In: Advanced Math
4) Write a brief reflection of Task 2 and 3 which may include description on data type used to solve the given task, variable used, and objects created. Reflection should also include justification on logic used to solve the given task along with proper references
Step 1: Task "2"
Program:
File name: “CalculateGPA.java”
import java.util.Scanner;
//Create a class
public class CalculateGPA {
public static void main(String[] args) {
// Scanner class object to take user input
Scanner scan = new Scanner(System.in);
//Prompt user to enter total number of students to be inserted
System.out.print("Enter how many Students you want to be insert ");
int numberOfStudents = Integer.parseInt(scan.nextLine());
// 5 modules for each student
int modules = 5;
// Create array to hold name, ID, marks for each student and gpa
String name_list[] = new String[numberOfStudents];
String ID_list[] = new String[numberOfStudents];
double marks_list[][] = new double[numberOfStudents][modules];
double gpa_list[] = new double[numberOfStudents];
System.out.println("Enter details of " + numberOfStudents + " students");
// Create a for loop to take input for students
for (int i = 0; i < numberOfStudents; i++) {
// Ask to enter name and ID
System.out.print("Student name" + (i + 1) + " ");
name_list[i] = scan.nextLine();
System.out.print("Student ID" + (i + 1) + " ");
ID_list[i] = scan.nextLine();
// Inner loop to take input for 5 modules
System.out.println("Mark of Modules\n********************");
for (int j = 0; j < modules; j++) {
System.out.print("Module" + (j + 1) + " ");
marks_list[i][j] = Double.parseDouble(scan.nextLine());
}
System.out.println("********************");
}
// Call function to calculate gpa for each student and store in gpa_list
calculate_gpa_score(marks_list, gpa_list);
// Call function to print details
display_report(name_list, ID_list, marks_list, gpa_list);
}
private static void display_report(String[] name_list, String[] ID_list, double[][] marks_list, double[] gpa_list) {
int total_students = name_list.length;
System.out.println("Details of " + total_students + " students");
// Create loop to print details of each student
for (int i = 0; i < total_students; i++) {
// print name and ID
System.out.println("Student name" + (i + 1) + ": " + name_list[i]);
System.out.println("Student ID" + (i + 1) + ": " + ID_list[i]);
System.out.println("Mark of Modules and Grades\n********************");
System.out.printf("%-15s%-15s%-15s%-15s%-15s\n", "Module1", "Module2", "Module3", "Module4", "Module5");
// Inner loop to print module marks
for (int j = 0; j < marks_list[0].length; j++) {
System.out.printf("%-15.2f", marks_list[i][j]);
}
System.out.println();
// Inner loop to print letter grade. Call function to get corresponding letter
// to marks
for (int j = 0; j < marks_list[0].length; j++) {
System.out.printf("%-15s", letter_grade(marks_list[i][j]));
}
// Print gpa for current student using gpa_list
System.out.println("\nGPA - " + String.format("%.2f", gpa_list[i]));
System.out.println();
}
}
public static void calculate_gpa_score(double marks_list[][], double gpa_list[]) {
double gpa = 0;
double grade = 0;
for (int i = 0; i < marks_list.length; i++) {
for (int j = 0; j < marks_list[0].length; j++) {
// Call grade_value function to get grade_value corresponding to marks
grade = grade_value(marks_list[i][j]);
// Add grade*10 to current gpa
gpa += grade * 10;
}
// Calculate gpa and store in gpa_list at index i
gpa_list[i] = gpa / 50;
// Re-set gpa to 0 for another student
gpa = 0;
}
}
public static String letter_grade(double marks) {
String letter = "";
// Use if-else if-else to set letter grade and return it at the end
if (marks >= 91 && marks <= 100) {
letter = "A";
} else if (marks >= 87 && marks <= 90) {
letter = "A-";
} else if (marks >= 84 && marks <= 86) {
letter = "B+";
} else if (marks >= 80 && marks <= 83) {
letter = "B";
} else if (marks >= 77 && marks <= 79) {
letter = "B-";
} else if (marks >= 74 && marks <= 76) {
letter = "C+";
} else if (marks >= 70 && marks <= 73) {
letter = "C";
} else if (marks >= 66 && marks <= 69) {
letter = "C-";
} else if (marks >= 60 && marks <= 65) {
letter = "D+";
} else if (marks >= 55 && marks <= 59) {
letter = "D";
} else if (marks >= 50 && marks <= 54) {
letter = "D-";
} else {
letter = "F";
}
return letter;
}
public static double grade_value(double marks) {
double grade = 0;
// Use if-else if-else to set numeric grade and return it at the end
if (marks >= 91 && marks <= 100) {
grade = 4;
} else if (marks >= 87 && marks <= 90) {
grade = 3.67;
} else if (marks >= 84 && marks <= 86) {
grade = 3.33;
} else if (marks >= 80 && marks <= 83) {
grade = 3.0;
} else if (marks >= 77 && marks <= 79) {
grade = 2.67;
} else if (marks >= 74 && marks <= 76) {
grade = 2.33;
} else if (marks >= 70 && marks <= 73) {
grade = 2.0;
} else if (marks >= 66 && marks <= 69) {
grade = 1.67;
} else if (marks >= 60 && marks <= 65) {
grade = 1.33;
} else if (marks >= 55 && marks <= 59) {
grade = 1.0;
} else if (marks >= 50 && marks <= 54) {
grade = 0.7;
} else {
grade = 0;
}
return grade;
}
}