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

Calculate GPA Using Input File 1. CourseTaken.java – Contains the class CourseTa

ID: 3575865 • Letter: C

Question

Calculate GPA Using Input File

1. CourseTaken.java – Contains the class CourseTaken –write this code

2. Transcript.java – Contains the class Transcript – Partially completed

3. TestTranscript.java – Contains the class TestTranscript –write this code

Details on what is contained in each class:

1. Class CourseTaken – You must write this code

Private attribute creditHours of type int

Private attribute letterGrade of type String

No-arg constructor

A public set method for each attribute

A public get method for each attribute

Additional public get methods:

public int getNumericGrade()

o Looks at the letterGrade contained in the object, and returns the corresponding numericGrade. Use String’s equals() method to check the value of a String.

public int getScore()

o Returns the “score”, based on the following formula:

score = numeric Grade * credit Hours

2. Class Transcript – File provided, finish the code

A constant for MAX_COURSES = 20

A constant FILE_NAME which will contain the file name that is read for input

Private attribute courseList which is an array of CourseTaken objects. Size is MAX_COURSES.

Private attribute numCourses of type int

No-arg constructor

A public method addCourse which accepts a CourseTaken object and adds it to the list.

Additional methods:

public double getGPA() – calculates the GPA based on the courses

public static void readInfoFromUser()

o (this is a static method)

o Create one Transcript object

o Prompts the user “Enter the number of courses you have taken”

o Loop for each course:

Create a CourseTaken object

Prompt the user for the letterGrade and creditHours and set them into the object

Add the CouseTaken object into the Transcript object

public static void readInfoFromFile() – reads the input from the file

o (Note that this is a static method)

o Create one Transcript object

o Reads the input file, which contains one record for each course.

o For each course, create a CourseTaken object, set the letterGrade and creditHours, and set them into the object. Then add the CourseTaken object into the Transcript object.

3. Class TestTranscript – You must write this code

o Contains a main method that:

Prompts the user:

Enter 1 to enter data from User or 2 to read data from a file

Accepts user input

If user enters 1, then call the static method from Transcript: readInfoFromUser(). Note that you must call this method statically.

If user enters 2, then call the static method from Transcript: readInfoFromFile(). Note that you must call this method statically.

Reading the Input file:

program reads an input file called transcript.txt. The file shall contain the letter grade and number of credit hours, delimited by a comma (,) or other delimiter.

Sample Output: Enter 1 to enter data from User or 2 to read data from file

1

Enter the number of courses you have taken:

3

Enter the letter grade you received for course #1

A

Enter the number of credit hours for course #1

3

Enter the letter grade you received for course #2

B

Enter the number of credit hours for course #2

3

Enter the letter grade you received for course #3

C

Enter the number of credit hours for course #3

3

Your GPA is 3.00

Enter 1 to enter data from User or 2 to read data from a file

2

Successfully read line: A,4

Successfully read line: B,3

Successfully read line: C,3

Successfully read line: D,4

Success! End of file reached

Your GPA is 2.50

package ExerciseGPA;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.Scanner;

public class Transcript {

   private static final int MAX_COURSES = 20;
   private static final String FILE_NAME = "Transcript.txt";
  
   private static Scanner input = new Scanner(System.in);
  
  
   // TODO: private attribute courseList
   // is an array of CourseTaken objects
   // size of MAX_COURSES
  
   // TODO: private attribute numCourses of type int
  
   // TODO: no-arg constructor
  
   // TODO: method to add a course. CourseTaken
   // object and adds it to the courseList array.
  
   // getGPA method is complete
   public double getGPA() {
      
       int totalScore = 0;
       int totalHours = 0;
      
       for (int x = 0; x < numCourses; x++) {
           totalHours = totalHours + courseList[x].getCreditHours();
           totalScore = totalScore + courseList[x].getScore();
       }
       return totalScore / ((double) totalHours);
   }
  
   public static void readInfoFromUser() {

       // TODO: Complete the code this method per the instructions.
      
       System.out.printf("Your GPA is %.2f%n", Transcript.getGPA());
   }

   // getGPA method is complete
   public static void readInfoFromFile() {
  
       // TODO: Create the Transcript object

       // create a handle to the file
       File file = new File(FILE_NAME);
      
       try {
           FileReader fileReader = new FileReader(file);
              
           // read one line at a time.
           // use BufferedReader - it is "line-oriented" input
           BufferedReader lineReader = new BufferedReader(fileReader);
          
           String record = null;
           do {
               // read one line from the file
               record = lineReader.readLine();
              
               if (record == null) {
                  
                   // null means end of file reached
                   System.out.println(" End of file ");
                  
               } else {
                  
                   // not null; we must process this record
                   System.out.println(" Yes read line: " + record);
              
                   // TODO: Create the CourseTaken object, c
                  
                   // TODO: provide correct argument to String's split method
                   // This is the delimiter used in the file.
                   String[] items = record.split();
                  
                   // first item (index 0) is letter grade
                   // TODO: set this value into the CourseTaken object
                  
                   // second item (index 1) is hours, but in String format
                   // need to convert from String to int
                   String hoursString = items[1];
                   Integer intObject = Integer.valueOf(hoursString);
                   int hours = intObject.intValue();
                   // TODO: set hours into the CourseTaken object
                  
                   // or combine 4 steps in one line
                   c.setCreditHours(Integer.valueOf(items[1]).intValue());
                  
                   // TODO: add the CourseTaken object to the kardex

               }
              
           } while(record != null);
          
           lineReader.close();
          
       } catch(IOException ioe) {
           System.err.println("Error reading file " + file.getAbsolutePath());
           ioe.printStackTrace();
       }
       System.out.printf(" GPA is %.2f%n", Transcript.getGPA());
   }

Sample Output: Enter 1 to enter data from User or 2 to read data from file

1

Enter the number of courses you have taken:

3

Enter the letter grade you received for course #1

A

Enter the number of credit hours for course #1

3

Enter the letter grade you received for course #2

B

Enter the number of credit hours for course #2

3

Enter the letter grade you received for course #3

C

Enter the number of credit hours for course #3

3

Your GPA is 3.00

Enter 1 to enter data from User or 2 to read data from a file

2

Successfully read line: A,4

Successfully read line: B,3

Successfully read line: C,3

Successfully read line: D,4

Success! End of file reached

Your GPA is 2.50

Explanation / Answer

public class CourseTaken {
   private int creditHours;
   private String letterGrade;
   private int gradeNum;
  
   public void setCreditHours ( int creditHours)
   {
       this.creditHours = creditHours;
   }
   public int getCreditHours()
   {
       return creditHours;
   }
  
   public void setLetterGrade ( String letterGrade)
   {
       this.letterGrade = letterGrade;
   }
   public String getLetterGrade()
   {
       return letterGrade;
   }
  
   public int getNumericGrade()
   {
       CourseTaken obj = new CourseTaken();
       if (letterGrade.equalsIgnoreCase("A"))
       {
           obj.gradeNum = 4;
       }
       else if(letterGrade.equalsIgnoreCase("B"))
       {
           obj.gradeNum = 3;
       }
       else if(letterGrade.equalsIgnoreCase("C"))
       {
           obj.gradeNum = 2;
       }
       else if(letterGrade.equalsIgnoreCase("D"))
       {
           obj.gradeNum = 1;
       }
       return gradeNum;
   }
  
   public int getScore()
   {
       CourseTaken obj = new CourseTaken();
       return obj.gradeNum * obj.creditHours;
   }
}

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.Scanner;
import java.util.*;
public class Transcript {
private static final int MAX_COURSES = 20;
private static final String FILE_NAME = "Transcript.txt";
  
private static Scanner input = new Scanner(System.in);
  
// TODO: private attribute courseList is an array of CourseTaken objects
// size of MAX_COURSES
private List<CourseTaken> courseList = new ArrayList<CourseTaken>();

// TODO: private attribute numCourses of type int
private int numCourses;
// TODO: no-arg constructor
  
// TODO: method to add a course. CourseTaken object and adds it to the courseList array.
public void addCourse(CourseTaken course)
{
   courseList.add(course);
}
  
// getGPA method is complete
public double getGPA() {
  
int totalScore = 0;
int totalHours = 0;
  
for (int x = 0; x < numCourses; x++)
{       
   totalHours = totalHours + courseList.get(x).getCreditHours();
totalScore = totalScore + courseList.get(x).getScore();
}
return totalScore / ((double) totalHours);
}
  
public static void readInfoFromUser() {
// TODO: Complete the code this method per the instructions.
   Transcript transobj = new Transcript();
     
System.out.println("Enter the number of courses you have taken:");
transobj.numCourses = input.nextInt();

for(int i = 1; i<= transobj.numCourses; i++)
{
   CourseTaken courseobj = new CourseTaken();
   System.out.println("Enter the number of credit hours for course # #" + i);
   courseobj.setCreditHours(input.nextInt());
   System.out.println("Enter the letter grade you received for course #" + i);
   courseobj.setLetterGrade(input.next());
   transobj.addCourse(courseobj);
}

System.out.printf("Your GPA is %.2f%n", transobj.getGPA());
}
// getGPA method is complete
public static void readInfoFromFile() {
  
// TODO: Create the Transcript object
   Transcript transobj = new Transcript();
// create a handle to the file
File file = new File(FILE_NAME);
  
try {
FileReader fileReader = new FileReader(file);
  
// read one line at a time.
// use BufferedReader - it is "line-oriented" input
BufferedReader lineReader = new BufferedReader(fileReader);
  
String record = null;
do
{
// read one line from the file
record = lineReader.readLine();
  
if (record == null)
{
  
// null means end of file reached
System.out.println(" End of file ");
  
}
else
{
// not null; we must process this record
System.out.println(" Yes read line: " + record);
  
// TODO: Create the CourseTaken object, c
  
// TODO: provide correct argument to String's split method
// This is the delimiter used in the file.
String[] items = record.split(",");
  
// first item (index 0) is letter grade
// TODO: set this value into the CourseTaken object
  
// second item (index 1) is hours, but in String format
// need to convert from String to int
String hoursString = items[1];
Integer intObject = Integer.valueOf(hoursString);
int hours = intObject.intValue();
// TODO: set hours into the CourseTaken object
  
// or combine 4 steps in one line
c.setCreditHours(Integer.valueOf(items[1]).intValue());
  
// TODO: add the CourseTaken object to the kardex
}
  
} while(record != null);
  
lineReader.close();
  
} catch(IOException ioe) {
System.err.println("Error reading file " + file.getAbsolutePath());
ioe.printStackTrace();
}
System.out.printf(" GPA is %.2f%n", transobj.getGPA());
}
}

import java.util.*;
public class TestTranscript {

   public static void main(String[] args) {      
       Scanner input = new Scanner(System.in);
       System.out.println("Enter 1 to enter data from User or 2 to read data from a file");
       int n = input.nextInt();
       if(n == 1)
       {
           Transcript.readInfoFromUser();
       }
       else if(n == 2)
       {
           Transcript.readInfoFromFile();
       }
       else
       {
           System.out.println("Please Enter 1 to enter data from User or 2 to read data from a file");
       }
   }
}

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