In: Computer Science
Note: Could you plz go through this code and let me
know if u need any changes in this.Thank You
=================================
// GPACalculator.java
import java.text.DecimalFormat;
public class GPACalculator {
private String title;
private double totalGrades;
private double totCredits;
private int noOfGrades;
/**
* @param title
*/
public GPACalculator(String title) {
this.title = title;
this.totalGrades = 0.0;
this.totCredits = 0.0;
this.noOfGrades = 0;
}
public void addGrade(double credits, double grade)
{
this.totalGrades += (credits *
grade);
this.totCredits += credits;
this.noOfGrades++;
}
public double getGPA() {
if (totCredits == 0 || totalGrades
== 0)
return
Double.POSITIVE_INFINITY;
else
return
(totalGrades / totCredits);
}
/*
* (non-Javadoc)
*
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
// DecimalFormat class is used
to format the output
DecimalFormat df = new
DecimalFormat(".00");
if (totCredits == 0.0 ||
totalGrades == 0.0)
return
"N/A";
return "GPA : " +
df.format(getGPA());
}
public double getTotalCredits() {
return totCredits;
}
public double getTotalGradePoints() {
return totalGrades;
}
public int getNumberOfGrades() {
return noOfGrades;
}
/**
* @return the title
*/
public String getTitle() {
return title;
}
}
==========================================
==========================================
// GPATester.java
import java.util.Scanner;
public class GPATester {
public static void main(String[] args)
{
String title;
double credits, grade;
String gradeLetter;
/*
* Creating an Scanner class object
which is used to get the inputs
* entered by the user
*/
Scanner sc = new
Scanner(System.in);
// Getting the input
entered by the user
/*
* Getting the input entered by the
user
*/
System.out.print("Enter title
:");
title =
sc.nextLine();
GPACalculator gpaCalc = new GPACalculator("CMPSC 101");
while (true) {
System.out.print("Enter no of credits :");
credits =
sc.nextDouble();
System.out.print("Enter Grade Letter :");
gradeLetter =
sc.next();
grade =
findGrade(gradeLetter);
gpaCalc.addGrade(credits, grade);
// Getting the
character from the user 'Y' or 'y' or 'N' or 'n'
System.out.print("\nDo you want to continue(Y/N) ::");
char ch =
sc.next(".").charAt(0);
if (ch == 'Y' ||
ch == 'y')
continue;
else
{
break;
}
}
System.out.printf("\nTotal Credits :%.2f\n" ,
gpaCalc.getTotalCredits());
System.out.printf("Total Grades
:%.2f\n" , gpaCalc.getTotalGradePoints());
System.out.println(gpaCalc);
}
private static double findGrade(String
gradeLetter) {
double grade = 0.0;
if (gradeLetter.equals("A+") ||
gradeLetter.equals("A") )
grade =
4.0;
else if
(gradeLetter.equals("A-"))
grade = 3.7;
else if
(gradeLetter.equals("B+"))
grade = 3.3;
else if
(gradeLetter.equals("B"))
grade = 3.0;
else if
(gradeLetter.equals("B-"))
grade = 2.7;
else if
(gradeLetter.equals("C+"))
grade = 2.3;
else if
(gradeLetter.equals("C"))
grade = 2.0;
else if
(gradeLetter.equals("C-"))
grade = 1.7;
else if
(gradeLetter.equals("D+"))
grade = 1.3;
else if
(gradeLetter.equals("D"))
grade = 1;
else if
(gradeLetter.equals("F"))
grade = 0.0;
return grade;
}
}
==========================================
==========================================
Output:
=====================Could you plz rate me well.Thank You