Question

Modify your PredictGPA.java file from Lab 2 to create a method called GPA that takes a...

Modify your PredictGPA.java file from Lab 2 to create a method called GPA that takes a string argument (the grade) and returns the corresponding value (ie 4.0 for an A, 3.33 for a B+, etc). Use this method call to reduce the “redundant” code that you had in Lab 2. The program must otherwise still run correctly as to the directions in Lab 2 and compute the GPA after using the method calls to determine the appropriate grade values. Trying to change the below Code to use Methods.

import java.util.Scanner;  // ** for Scanner

public class GPAMethod {
        public static void main(String[] args) {

            Scanner userInput = new Scanner(System.in); // Allows to scan in user input

            System.out.print("How many classes did you have?: "); //Asks how many classes
            int classes = userInput.nextInt(); //initializes and stores user input into classes


            String grades; // establishes the string grades
            double hours; // hours needs to be a double in the case the a class is 1.5 hours
            double gpa =0; // GPA also needs to be a double
            int i; // declare i for the FOR loop
            double sumGPA = 0; // this is how I will add up the GPA every time through the FOR loop
            double sumHours = 0; // this is how I will add up Class Hours every time through the FOR loop
            // gpa = 10; // tried to debug using this because it is not catching gpa in the loop


            for (i = 0; i < classes; i++) { // we want to set i to zero, and we know we want a FOR loop
                // because we will know how many classes based on user input
                // we want i to increase by 1 every time

                System.out.print("Expected Grade for class " + i + "?:");
                // have the user input the expected grade for each class, represented by i
                // this is where I dont understand how to get i = 1 for the first class
                // because we are dealing with strings I have to set i to zero?
                // I feel like this is a small issue but for syntax it would be nice to know
                grades = userInput.nextLine(); // user puts grade
                userInput.next(); // needed this line of code to separate the strings, otherwise
                // they were appearing next to each other, not sure why I had
                // to do this
                System.out.print("How many hours is class " + i + "?: "); // asks for user hours
                // concatenates class number
                hours = userInput.nextDouble(); // user input
                sumHours = hours + sumHours; // each time we go through the loop this
                // line of code adds the hours
                sumGPA =  (hours * gpa) + sumGPA; // each time through this calculates GPA

                if (grades.equals("A")) { // because user input is a string
                    // I did grades.equals()
                    gpa = 4;
                    // sumGPA = hours + gpa;

                } else if (grades.equals("A-")) { // I go through each and assign a double
                    // to gpa, given the user string
                    gpa = 3.67;

                } else if (grades.equals("B+")) { // I use else if because they are exclusive
                    gpa = 3.37;

                } else if (grades.equals("B")) {
                    gpa = 3;

                } else if (grades.equals("B-")) {
                    gpa = 2.67;

                } else if (grades.equals("C+")) {
                    gpa = 2.37;

                } else if (grades.equals("C")) {
                    gpa = 2;

                } else if (grades.equals("C-")) {
                    gpa = 1.67;

                } else if (grades.equals("D+")) {
                    gpa = 1.37;

                } else if (grades.equals("D")) {
                    gpa = 1;

                } else if (grades.equals("D-")) {
                    gpa = .67;

                } else if (grades.equals("F")) {
                    gpa = 0;

                }


            }

            double GPA =  sumGPA/sumHours; // at the end I want the sumGPA to be divided by the sum of hours
            System.out.println("Your Semester GPA would be " + GPA); // print statement that lets user
          
        }
    }
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Program:

import java.util.Scanner; // ** for Scanner

public class GPAMethod {
   public static void main(String[] args) {
       Scanner userInput = new Scanner(System.in); // Allows to scan in user input

       System.out.print("How many classes did you have?: "); //Asks how many classes
       int classes = userInput.nextInt(); //initializes and stores user input into classes

       String grades; // establishes the string grades
       double hours; // hours needs to be a double in the case the a class is 1.5 hours
       double gpa =0; // GPA also needs to be a double
       int i; // declare i for the FOR loop
       double sumGPA = 0; // this is how I will add up the GPA every time through the FOR loop
       double sumHours = 0; // this is how I will add up Class Hours every time through the FOR loop
       // gpa = 10; // tried to debug using this because it is not catching gpa in the loop

       for (i = 0; i < classes; i++) { // we want to set i to zero, and we know we want a FOR loop
           // because we will know how many classes based on user input
           // we want i to increase by 1 every time

           System.out.print("Expected Grade for class " + i + "?:");
           userInput.nextLine(); // needed this line of code to separate the strings, otherwise
           // they were appearing next to each other, not sure why I had
           // to do this
           // have the user input the expected grade for each class, represented by i
           // this is where I dont understand how to get i = 1 for the first class
           // because we are dealing with strings I have to set i to zero?
           // I feel like this is a small issue but for syntax it would be nice to know
           grades = userInput.nextLine(); // user puts grade

           System.out.print("How many hours is class " + i + "?: "); // asks for user hours
           // concatenates class number
           hours = userInput.nextDouble(); // user input
           sumHours = hours + sumHours; // each time we go through the loop this
           //get the gpa by calling the GPA function
           gpa = GPA(grades);
           // line of code adds the hours
           sumGPA = (hours * gpa) + sumGPA; // each time through this calculates GPA      
       }
       double GPA = sumGPA/sumHours; // at the end I want the sumGPA to be divided by the sum of hours
       System.out.printf("Your Semester GPA would be %.2f" , GPA); // print statement that lets user
   }

   //method that takes a string(grade) as parameter and returns the corresponding gpa
   public static double GPA(String grade)
   {
       double gpa = 0.0;
       //run a switch case based on the grade received
       switch(grade)
       {
       case "A+":
           //for grade A+ set gpa = 4.0
           gpa = 4.0;
           break;
       case "A":
           //for grade A set gpa = 4.0
           gpa = 4.0;
           break;
       case "A-":
           //for grade A- set gpa = 3.67
           gpa = 3.67;
           break;
       case "B+":
           //for grade B+ set gpa = 3.37
           gpa = 3.37;
           break;
       case "B":
           //for grade B set gpa = 3
           gpa = 3;
           break;
       case "B-":
           //for grade B- set gpa = 2.67
           gpa = 2.67;
           break;
       case "C+":
           //for grade C+ set gpa = 2.37
           gpa = 2.37;
           break;
       case "C":
           //for grade C set gpa = 2
           gpa = 2;
           break;
       case "C-":
           //for grade C- set gpa = 1.67
           gpa = 1.67;
           break;
       case "D+":
           //for grade D+ set gpa = 1.37
           gpa = 1.37;
           break;
       case "D":
           //for grade D set gpa = 1
           gpa = 1;
           break;
       case "D-":
           //for grade D- set gpa = 0.67
           gpa = .67;
           break;
       case "F":
           //for grade F set gpa = 0
           gpa = 0;
           break;
       }
       return gpa;
   }
}

Output:

Code screenshot:

Add a comment
Know the answer?
Add Answer to:
Modify your PredictGPA.java file from Lab 2 to create a method called GPA that takes a...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Java Programming Language Edit and modify from the given code Perform the exact same logic, except...

    Java Programming Language Edit and modify from the given code Perform the exact same logic, except . . . The numeric ranges and corresponding letter grade will be stored in a file. Need error checking for: Being able to open the text file. The data makes sense: 1 text line of data would be 100 = A. Read in all of the numeric grades and letter grades and stored them into an array or arraylist. Then do the same logic....

  • Create a GPA calculator in Java. Ask the end user for the credit hours and letter...

    Create a GPA calculator in Java. Ask the end user for the credit hours and letter grade for each class. Once the user is finished with the data entry, calculate and output the overall GPA for the given input. For example, if four classes are input, each of which is a three-hour course, with grades of A, B, B, and B, the overall GPA will be 3.25. The exact user interaction is up to you. As long as the requirements...

  • Create a program in Python to change Grades in number and find the GPA of the...

    Create a program in Python to change Grades in number and find the GPA of the user Problem: The user wants an easy calculator to calculate their GPA. They want to enter the letter grade (A, B, C, D, F) for 5 classes, the credit hours (1, 2, 3,4, 5) and then run the program to calculate the GPA (3.50- 4.00 A, 3.49 – 2.50 B, 2.49 -1.60 C, 1.59 – .50 D, .49 -0 F) for the semester. Task...

  • I need to create a code for this prompt: In this project we will build a...

    I need to create a code for this prompt: In this project we will build a generic UserInput class for getting keyboard input from the user. Implementation: The class UserInput is a 'Methods only' class, and all the methods should be declared static. Look at the TestScanner.java program at the bottom of this page that inputs a string, int and double. It shows you how to use Scanner class to get input from the keyboard. Write FOUR simple methods, one...

  • JAVA HELP FOR COMP: Can someone please modify the code to create LowestGPA.java that prints out...

    JAVA HELP FOR COMP: Can someone please modify the code to create LowestGPA.java that prints out the name of the student with the lowestgpa. In the attached zip file, you will find two files HighestGPA.java and students.dat. Students.dat file contains names of students and their gpa. Check if the code runs correctly in BlueJ or any other IDE by running the code. Modify the data to include few more students and their gpa. import java.util.*; // for the Scanner class...

  • I need to change the following code so that it results in the sample output below...

    I need to change the following code so that it results in the sample output below but also imports and utilizes the code from the GradeCalculator, MaxMin, and Student classes in the com.csc123 package. If you need to change anything in the any of the classes that's fine but there needs to be all 4 classes. I've included the sample input and what I've done so far: package lab03; import java.util.ArrayList; import java.util.Scanner; import com.csc241.*; public class Lab03 { public...

  • Write a JAVA program that prompts the user for student grades and displays the highest and...

    Write a JAVA program that prompts the user for student grades and displays the highest and lowest grades in the class. The user should enter a character to stop providing values. Starter code: import java.util.Scanner; public class MaxMinGrades{   public static void main(String[] args){     Scanner input = new Scanner(System.in);     System.out.println("Enter as many student grades as you like. Enter a character to stop.");     double grade = input.nextDouble();     double minGrade = Double.MAX_VALUE;     double maxGrade = Double.MIN_VALUE;     while (Character.isDigit(grade)) {       if (grade == 0)...

  • need help with this JAVA lab, the starting code for the lab is below. directions: The...

    need help with this JAVA lab, the starting code for the lab is below. directions: The Clock class has fields to store the hours, minutes and meridian (a.m. or p.m.) of the clock. This class also has a method that compares two Clock instances and returns the one that is set to earlier in the day. The blahblahblah class has a method to get the hours, minutes and meridian from the user. Then a main method in that class creates...

  • PrintArray vi Create a class called PrintArray. This is the class that contains the main method....

    PrintArray vi Create a class called PrintArray. This is the class that contains the main method. Your program must print each of the elements of the array of ints called aa on a separate line (see examples). The method getArray (included in the starter code) reads integers from input and returns them in an array of ints. Use the following starter code: //for this program Arrays.toString(array) is forbidden import java.util.Scanner; public class PrintArray { static Scanner in = new Scanner(System.in);...

  • My Java code from last assignment: Previous Java code: public static void main(String args[]) { //...

    My Java code from last assignment: Previous Java code: public static void main(String args[]) { // While loop set-up boolean flag = true; while (flag) { Scanner sc = new Scanner(System.in); // Ask user to enter employee number System.out.print("Enter employee number: "); int employee_number = sc.nextInt(); // Ask user to enter last name System.out.print("Enter employee last name: "); String last_name = sc.next(); // Ask user to enter number of hours worked System.out.print("Enter number of hours worked: "); int hours_worked =...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT