Question

In: Computer Science

Develop a rudimentary Java program that will allow anyone to enter any number of grades and...

Develop a rudimentary Java program that will allow anyone to enter any number of grades and then calculate grade point average.

For reference, a grade point average is a weighted average of a set of grades based on the relative number of credits. For example, a 1 credit “A” (4.0) should count twice as much as a .5 credit “C” (3.0), and a 1.5 credit “A+”(4.33) should count three times as much as the “C” and 1.5 times as much as the “A”.

Follow the exact specification written.

**Class GPACalculator

public GPACalculator(String title)
• provides a name for this instance of the calculator, such as “Spring 2020”

public void addGrade(double credits, double grade)
• adds the specified grade and credits to the object’s fields

public double getGPA()
• returns the (unrounded) calculated GPA
• if no grades have been entered, returns Double.POSITIVE_INFINITY

@Override
public String toString()
• returns a string representation of the calculated GPA. This method should not calculate the GPA, but should use the getGPA() method.
• returns the GPA as a string, or the string “N/A” if the GPA can’t be calculated.

Accessors--Provide accessor methods (getters) for each of the following class fields:

double getTotalCredits()

double getTotalGradePoints()

int getNumberOfGrades()

String getTitle()

**Class GPATester

public static void main(String[] args)
• creates an instance of GPACalculator and adds at least 5 grades that adequately test the calculations
• calls printResults() to clearly print all of the information available from the GPACalculator object.
• creates a second instance of GPACalculator to thoroughly test the calculations and special cases and calls printResults() to print out those values.

public static void printResults(GPACalculator gpa)
• prints a well-formatted output for the specified GPACalculator instance
• differentiates between an empty calculator and one with data by printing a message

Keep the code basic and readable. Thank you!

Solutions

Expert Solution

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


Related Solutions

Design a program that displays the following in Java: Enter the grades for an Essay: Grammer...
Design a program that displays the following in Java: Enter the grades for an Essay: Grammer (must be 30 or less): 40 Grammer (must be 30 or less): 26.3 Spelling (must be 20 or less): 17.4 Correct Length (must be 20 or less): 19 Content (must be 30 or less): 23.5 The recorded scores are: Grammer: 26.3 Spelling: 17.4 Correct Length: 19.0 Content: 23.5 Total score: 86.2 The grade for this essay is B
Create a C++ program that will accept any number of grades for an exam. The grades...
Create a C++ program that will accept any number of grades for an exam. The grades will be input as 4 for an A, 3 for a B, 2 for a C, 1 for a D, and 0 for an F. After all grades have been entered, allow the user to enter -1 to exit. Output the number of grades in each category. Using arrays.
write a program that: 1) asks the user to enter grades, a negative number is the...
write a program that: 1) asks the user to enter grades, a negative number is the signal to stop entering. (while loop) - add each grade to a list (do not add the negative number to the list) after the while loop to enter grades is finished: 2) use a for loop on the list to calculate the average of all numbers in the list 3) use a for loop on the list to find the largest number in the...
Develop a program that asks a user to enter the number 10, and then it outputs...
Develop a program that asks a user to enter the number 10, and then it outputs COUNT-DOWN from 10 to 0. using for-loop
Develop a program that asks a user to enter the number 10, and then it outputs...
Develop a program that asks a user to enter the number 10, and then it outputs COUNT-DOWN from 10 to 0.
JAVA Write a java program that will sum all positive number. Prompt the user to enter...
JAVA Write a java program that will sum all positive number. Prompt the user to enter numbers and add all positive numbers. The program will not add negative numbers and the program will stop when you enter ‘0’.   Enter a number> 25 Enter a number> 9 Enter a number> 5 Enter a number> -3 Enter a number> 0 Sum = 39
Java Program 1. Write a program that asks the user: “Please enter a number (0 to...
Java Program 1. Write a program that asks the user: “Please enter a number (0 to exit)”. Your program shall accept integers from the user (positive or negative), however, if the user enters 0 then your program shall terminate immediately. After the loop is terminated, return the total sum of all the previous numbers the user entered. a. What is considered to be the body of the loop? b. What is considered the control variable? c. What is considered to...
Develop a Java program that prompts the user to enter her birth year (e.g. 1980). The...
Develop a Java program that prompts the user to enter her birth year (e.g. 1980). The program then uses the current year (i.e. 2020) and displays age of the user (i.e. 40). The program should handle any exception that can occur due to the value entered by the user.
PLEASE DO NOT OVERRIDE ANY EXCEPTIONS TASK: You want to develop a Java program that will...
PLEASE DO NOT OVERRIDE ANY EXCEPTIONS TASK: You want to develop a Java program that will allow you to keep track of a set of employees. In reviewing your employee list, you notice that your employees fall into two categories: Salaried and Hourly. The following table shows the information that you keep in your employee list for each type of employee. Field Type Salaried Hourly id int Yes Yes name String Yes Yes title String Yes No position String No...
Create an Java application that uses a class to convert number grades to letter grades and...
Create an Java application that uses a class to convert number grades to letter grades and another class for data validation. Specifications The Grade class should have only one Instance Variable of type int. Use a class named Grade to store the data for each grade. This class should include these three methods:   public void setNumber(int number)   public int getNumber()   public String getLetter() The Grade class should have two constructors. The first one should accept no parameters and set the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT