Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Program : GpaTestEx2.java Coding: import java.util.Scanner; public class GpaTest

ID: 3735612 • Letter: P

Question

Program : GpaTestEx2.java

Coding:

import java.util.Scanner;

public class GpaTestEx2
{

   public static void main (String[] args)
   {
//declarations
Scanner in = new Scanner(System.in); //input object
int numCourses; //number of courses - can be changed
int credits; //number of credits for a course
String grade; //grade for course


//read in number of courses
System.out.print("Enter number of courses: ");
numCourses = in.nextInt();

//create Gpa object to hold specified number of courses
Gpa myGPA = new Gpa(numCourses);

//read in all courses and add course information to Gpa object
for (int k=0; k<numCourses; k++)
{
   System.out.print("Enter credits for course " + (k+1) + ": ");
   credits = in.nextInt();
   System.out.print("Enter grade for course " + (k+1) + ": ");
   grade = in.next();
  
   myGPA.addCourse(credits, grade);
}

//print results
System.out.println();
System.out.printf("GPA is %4.2f%n", myGPA.calcGPA( ));

   } //end main
}

Exercise 1- GPA calculation using arrays (3 points) The exercise must be completed during the lab period. This lab requires the development of a java program to calculate the standard grade point average (GPA) for a student at RIT. The program will prompt the user to enter the credits and letter grade for each of exactly 4 courses. The GPA is calculated by dividing the sum of points times credits by the sum of credits. To compute the sum of points, the letter grade must be converted to its equivalent number of points as follows: A is 4 points, B is 3 points, C is 2 points, D is 1 point, and F is O points. Then multiply the points by the number of credits and sum for the 4 courses. For example, if a student scores C in a 3-credit course, B in a 4-credit course, D in a 4-credit course, and A in a 3- credit course, the calculation would go as follows: Sum of credits -3+4+ 4+3- 14 Sum of points times credits-3 * 2 (-C) 4 * 3 (-B) + 4 * 1 (D) + 3 * 4 (A)-34 GPA 34/14 2.42857... 2.43 (rounded to 2 decimal places. After the user enters the credits and letter grade for all four courses, print the sum of the credits for all four courses, the sum of the points times credits for all four courses and the GPA for all four courses. For Exercise 1, write all of the code in the main method. Store the credits for each course in an array with 4 elements. Store the letter grades in another array with 4 elements. When computing the sum of the credits, an enhanced for loop must be used. This exercise uses "parallel" arrays; i.e. a data element in one array is matched to a related data element in the other array using the same index value. A constant must be defined and used for the number of courses. Also, in Exercise 1, write a method: which, given a letter grade, returns the appropriate numeric grade. In Exercise 2, develop a Gpa class using "paralle" arrays to hold the grades and credits. lin public double letterToNumeric(char letterGrade) Exercise 3, develop a Gpa class using an ArrayList to hold the course information.

Explanation / Answer

Code for Exercise-1:

import java.util.*;

public class GPACalc

{

public static void main(String[] args)

{

//constant

int NUMBER_OF_COURSE=4;

int credits[]= new int[NUMBER_OF_COURSE];

String grades[]= new String[NUMBER_OF_COURSE];

//scanner class to read input from keyboard

Scanner in=new Scanner(System.in);

//loop to ask user inputs

for(int i=0;i<credits.length;i++)

{

System.out.print("Enter credits for course "+(i+1)+":");

credits[i]=in.nextInt();

System.out.print("Enter grade for course "+(i+1)+":");

grades[i]=in.next();

}

//variable to hold sum of credits and sum of points

int sumOfCredits=0;

double sumOfPoints=0;

//enhanced for loop to compute the sum of credits

for(int c: credits)

sumOfCredits=sumOfCredits+c; //summing of credits

//displaying sum of credits to console

System.out.println("Total number of credits: "+sumOfCredits);

//for loop to calculate sum of points times credits

for(int i=0;i<grades.length;i++)

{

//converting String grade to char grade

char grade=grades[i].charAt(0);

//summing of points

sumOfPoints=sumOfPoints+credits[i]*(letterToNumeric(grade));

}

//displaying sum of points to console

System.out.println("Total number of points: "+sumOfPoints);

//displaying GPA to console using printf

if(sumOfPoints==0)

System.out.println("GPA: 0.00");

else

System.out.printf("GPA: %.2f",(sumOfPoints/sumOfCredits));

}

//static method to convert letter grade to numeric grade

public static double letterToNumeric(char letterGrade)

{

//variable to hold numeric grade

double numericGrade=0;

//returning numeric grade using switch case

switch(letterGrade)

{

case 'A': numericGrade=4.0;break;

case 'B': numericGrade=3.0;break;

case 'C': numericGrade=2.0;break;

case 'D': numericGrade=1.0;break;

case 'F': numericGrade=0.0;break;

}

return numericGrade;

}

}

Output:

Enter credits for course 1:3
Enter grade for course 1:C
Enter credits for course 2:4
Enter grade for course 2:B
Enter credits for course 3:4
Enter grade for course 3:D
Enter credits for course 4:3
Enter grade for course 4:A
Total number of credits: 14
Total number of points: 34.0
GPA: 2.43

Code for Exercise 2:

public class Gpa

{

//attributes

int credits[];

String grades[];

int numCourses;

int maxCourses;

//constructor

public Gpa(int _maxCourses)

{

credits=new int[_maxCourses];

grades=new String[_maxCourses];

maxCourses=_maxCourses;

numCourses=0;

}

public void addCourse(int _credits, String _grade)

{

credits[numCourses]=_credits;

grades[numCourses]=_grade;

numCourses=numCourses+1;

}

//calculates the gpa and returns

public double calcGPA()

{

int sumOfCredits=0;

double sumOfPoints=0,gpa=0;

//enhanced for loop to compute the sum of credits

for(int c: credits)

sumOfCredits=sumOfCredits+c; //summing of credits

//for loop to calculate sum of points times credits

for(int i=0;i<grades.length;i++)

{

//converting String grade to char grade

char grade=grades[i].charAt(0);

//summing of points

sumOfPoints=sumOfPoints+credits[i]*(letterToNumeric(grade));

}

//calcualte gpa

gpa=(sumOfPoints/sumOfCredits);

//displaying GPA to console using printf

if(sumOfPoints==0)

return 0.0;

else

return gpa;

}

//static method to convert letter grade to numeric grade

public double letterToNumeric(char letterGrade)

{

//variable to hold numeric grade

double numericGrade=0;

//returning numeric grade using switch case

switch(letterGrade)

{

case 'A': numericGrade=4.0;break;

case 'B': numericGrade=3.0;break;

case 'C': numericGrade=2.0;break;

case 'D': numericGrade=1.0;break;

case 'F': numericGrade=0.0;break;

case 'a': numericGrade=4.0;break;

case 'b': numericGrade=3.0;break;

case 'c': numericGrade=2.0;break;

case 'd': numericGrade=1.0;break;

case 'f': numericGrade=0.0;break;

}

return numericGrade;

}

}

Output:

Enter number of courses: 2
Enter credits for course 1: 4
Enter grade for course 1: A
Enter credits for course 2: 3
Enter grade for course 2: B

GPA is 3.57

Code for excercise3:

Course class:

public class Course

{

//attributes

int credits;

String grade;

//constructor

public Course() {}

public Course(int credits, String grade)

{

super();

this.credits = credits;

this.grade = grade;

}

//getter methods

public int getCredits() {

return credits;

}

public String getGrade() {

return grade;

}

}

Gpa class code:

import java.util.*;

public class Gpa

{

//array list to hold course info

ArrayList<Course> courseList;

//attributes

int credits[];

String grades[];

int numCourses;

int maxCourses;

//constructor

public Gpa(int _maxCourses)

{

credits=new int[_maxCourses];

grades=new String[_maxCourses];

maxCourses=_maxCourses;

numCourses=0;

courseList=new ArrayList<Course>();

}

//method to add Course to ArrayList

public void addCourse(Course obj)

{

courseList.add(obj);

}

//calculates the gpa and returns

public double calcGPA()

{

int sumOfCredits=0;

double sumOfPoints=0,gpa=0;

//enhanced for loop to compute the sum of credits

for(Course course: courseList)

sumOfCredits=sumOfCredits+course.getCredits(); //summing of credits

for(int i=0;i<courseList.size();i++)

{

//converting String grade to char grade

char grade=courseList.get(i).grade.charAt(0);

sumOfPoints=sumOfPoints+(courseList.get(i).credits*letterToNumeric(grade)); //summing of credits

}

//calculate gpa

gpa=(sumOfPoints/sumOfCredits);

//displaying GPA to console

if(sumOfPoints==0)

return 0.0;

else

return gpa;

}

//static method to convert letter grade to numeric grade

public double letterToNumeric(char letterGrade)

{

//variable to hold numeric grade

double numericGrade=0;

//returning numeric grade using switch case

switch(letterGrade)

{

case 'A': numericGrade=4.0;break;

case 'B': numericGrade=3.0;break;

case 'C': numericGrade=2.0;break;

case 'D': numericGrade=1.0;break;

case 'F': numericGrade=0.0;break;

case 'a': numericGrade=4.0;break;

case 'b': numericGrade=3.0;break;

case 'c': numericGrade=2.0;break;

case 'd': numericGrade=1.0;break;

case 'f': numericGrade=0.0;break;

}

return numericGrade;

}

}

Modified Test class:

import java.util.Scanner;

public class GpaTestEx2
{

public static void main (String[] args)
{
//declarations
Scanner in = new Scanner(System.in); //input object
int numCourses; //number of courses - can be changed
int credits; //number of credits for a course
String grade; //grade for course


//read in number of courses
System.out.print("Enter number of courses: ");
numCourses = in.nextInt();

//create Gpa object to hold specified number of courses
Gpa myGPA = new Gpa(numCourses);

//read in all courses and add course information to Gpa object
for (int k=0; k<numCourses; k++)
{
System.out.print("Enter credits for course " + (k+1) + ": ");
credits = in.nextInt();
System.out.print("Enter grade for course " + (k+1) + ": ");
grade = in.next();

Course obj=new Course(credits,grade);
myGPA.addCourse(obj);
}

//print results
System.out.println();
System.out.printf("GPA is %4.2f%n", myGPA.calcGPA( ));

} //end main
}

Output:

Enter number of courses: 2
Enter credits for course 1: 4
Enter grade for course 1: a
Enter credits for course 2: 3
Enter grade for course 2: B

GPA is 3.57

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote