Java Program Suppose a student was taking 5 different course
Java Program
Suppose a student was taking 5 different courses last semester. Write a program that
(a) asks the student to input his/her name, student ID, marks for these 5 courses,
(b) calculate the average,
(c) determine the letter grade of each course.
(d) record the number of courses whose final letter grade is A+, A, A-, .... , F+, F, F-.
(e) Output the following information in a nice format: student name, student ID, listing of marks, the average, letter grade for each course, and the number of courses in each letter grade category.
I dont know how to do d
here is my code:
import java.util.Scanner;
public class Question_2 {
public String Grade(int mark) {
String GradeLetter = \"\";
if (mark >= 93 && mark <= 100)
GradeLetter = \"A+\";
if (mark >= 86 && mark < 93)
GradeLetter = \"A\";
if (mark >= 80 && mark < 86)
GradeLetter = \"A-\";
if (mark >= 77 && mark < 80)
GradeLetter = \"B+\";
if (mark >= 73 && mark < 77)
GradeLetter = \"B\";
if (mark >= 70 && mark < 73)
GradeLetter = \"B-\";
if (mark >= 67 && mark < 70)
GradeLetter = \"C+\";
if (mark >= 63 && mark < 67)
GradeLetter = \"C\";
if (mark >= 60 && mark < 63)
GradeLetter = \"C-\";
if (mark >= 57 && mark < 60)
GradeLetter = \"D+\";
if (mark >= 53 && mark < 57)
GradeLetter = \"D\";
if (mark >= 50 && mark < 53)
GradeLetter = \"D-\";
if (mark >= 35 && mark < 50)
GradeLetter = \"F\";
if (mark >= 0 && mark < 35)
GradeLetter = \"F-\";
return GradeLetter;
}
public static void main(String[] args) {
Question_2 q2 = new Question_2();
// declare variables
String name;// student name
int studentID;// student ID
int mark1, mark2, mark3, mark4, mark5;// student marks in each 5 courses
// asks the student to input his/her name
System.out.println(\"Input your first name: \");
Scanner input = new Scanner(System.in);
name = input.nextLine();
// asks the student to input student ID
System.out.println(\"Input your StudentID (integer in 5 digits),ex:000000 :\");
studentID = input.nextInt();
// asks the student to input marks of 5 different courses last semester
System.out.println(\"Input your courses grade (0-100)integer number \");
System.out.println(\"Your course1\'s grade: \");
mark1 = input.nextInt();
System.out.println(\"Your course2\'s grade: \");
mark2 = input.nextInt();
System.out.println(\"Your course3\'s grade: \");
mark3 = input.nextInt();
System.out.println(\"Your course4\'s grade: \");
mark4 = input.nextInt();
System.out.println(\"Your course5\'s grade: \");
mark5 = input.nextInt();
// Calculate the average of 5 different courses last semester
double average = (mark1 + mark2 + mark3 + mark4 + mark5) / 5.0;
/*
* Output the following information in a nice format: student name,
* student ID, listing of marks, the average, letter grade for each
* course, and the number of courses in each letter grade category.
*/
System.out.println(\"**********************************************\");
System.out.println(\"Student Name: \" + name);
System.out.println(\"Student ID : \" + studentID);
System.out.println(name + \" grade in \" + \"Course1: \" + mark1 + \" \" + q2.Grade(mark1));
System.out.println(name + \" grade in \" + \"Course2: \" + mark2 + \" \" + q2.Grade(mark2));
System.out.println(name + \" grade in \" + \"Course3: \" + mark3 + \" \" + q2.Grade(mark3));
System.out.println(name + \" grade in \" + \"Course4: \" + mark4 + \" \" + q2.Grade(mark4));
System.out.println(name + \" grade in \" + \"Course5: \" + mark5 + \" \" + q2.Grade(mark5));
System.out.println(name + \" avaerage grade is: \" + average);
System.out.println(\"**********************************************\");
}
}
Solution
import javax.swing.*;
import java.text.DecimalFormat;
public class Marks {
public static void main(String[] args) {
// String variables to hold name and ID created
String studentName, studentID;
// Arrays to store student marks and letter grades created
int[] studentMarks = new int[5];
String[] studentLetters = new String[5];
// Array to store break down of letter grades created
int[] letterCount = new int[15];
// Initialize array
for(int i = 0; i < 15; i++)
letterCount[i] = 0;
// Stores average mark
double average;
// Arrays that contain mark to letter conversion data created
String[] letterGrades = {\"\", \"A+\", \"A\", \"A-\", \"B+\", \"B\", \"B-\", \"C+\",
\"C\", \"C-\", \"D+\", \"D\", \"D-\", \"F\", \"F-\"};
int[] letterMarks = {101, 93, 86, 80, 77, 73, 70, 67, 63, 60, 57, 53, 50, 35, 0};
// Prompt user for name and ID
studentName = JOptionPane.showInputDialog(\"What is your name?\");
studentID = JOptionPane.showInputDialog(\"What is your Student ID?\");
// Prompt user for marks and convert them to int
studentMarks[0] = Integer.parseInt(JOptionPane.showInputDialog(\"What is the mark for your first course?\"));
studentMarks[1] = Integer.parseInt(JOptionPane.showInputDialog(\"What is the mark for your second course?\"));
studentMarks[2] = Integer.parseInt(JOptionPane.showInputDialog(\"What is the mark for your third course?\"));
studentMarks[3] = Integer.parseInt(JOptionPane.showInputDialog(\"What is the mark for your fourth course?\"));
studentMarks[4] = Integer.parseInt(JOptionPane.showInputDialog(\"What is the mark for your fifth course?\"));
// Stores total mark
int total = 0;
// Calculate total
for(int i = 0; i < 5; i++)
total += studentMarks[i];
// Determine average
average = (double)total / 5;
// For loop cycles through the five marks
for(int i = 0; i < 5; i++)
// Nested for loop checks each letter grade range
for(int j = 1; j < 15; j++)
// If the mark is in the correct range
if(studentMarks[i] < letterMarks[j - 1] && studentMarks[i] >= letterMarks[j]) {
// Assign the letter to the studentLetters array
studentLetters[i] = letterGrades[j];
// Increment the letterCount entry
letterCount[j]++;
}
// Output student name and ID
System.out.println(\"Name: \" + studentName + \"\\t\\tID: \" + studentID + \"\ \");
// Print marks and letters for each course
for(int i = 0; i < 5; i++)
System.out.println(\"Mark for course \" + (i + 1) + \": \" + studentMarks[i]
+ \"% (\" + studentLetters[i] + \")\");
// Format the average
DecimalFormat numFormat = new DecimalFormat(\"0.00\");
// Print average
System.out.println(\"\ The average is \" + numFormat.format(average));
System.out.println(\"\ The following is a break down of marks by letter grade:\");
// Print letter break down
for(int i = 1; i < 15; i++)
if(letterCount[i] > 0)
System.out.println(letterCount[i] + \" x \" + letterGrades[i]);
// Exit program
System.exit(0);
}
}
//RATE MY ANSWERS




