Question

Java Description You are given the task of reading a student’s name, semester letter grades, and...

Java

Description

You are given the task of reading a student’s name, semester letter grades, and semester hours attempted from one file; calculating the semester GPA; and writing the student’s name and semester GPA to another file.

GPA Calculation

GPA = Total Quality Points / Hours Attempted

Quality Points for each class = Grade Conversion Points * Hours Attempted

The letter grades with corresponding point conversions is based upon the following table:

Letter Grade

Conversion Points

A

4 points

B

3 points

C

2 points

D

1 points

F

0 points

Examples:

A student earns an ‘A’ in a three hour course, a ‘C’ in a four hour course, and an ‘F’ in a two hour course. The overall GPA would be:

(1) Total Quality Points = (4 * 3) + (2 * 4) + (0 * 2) = 20

(2) Total Hours = (3 + 4 + 2) = 9

(3) GPA = 20/9 = 2.22

Letter Grade

Conversion Points

Hours Attempted

Quality Points

A

4 points

3

4 * 3 = 12

C

2 points

4

2 * 4 = 8

F

0 points

2

0 * 2 = 0

Total = 9

Total = 20

GPA= TotalQualityPoints / Hours à 20 / 9 = 2.22

A student earns an ‘A’ in a four hour course, a ‘D’ in a four hour course, and a ‘C’ in a two hour course. The overall GPA would be:

(1) Quality Points = (4* 4) + (1 * 4) + (2 * 2) = 24

(2) Total Points = (4 + 4 + 2) = 10

(3) GPA = 24/10 = 2.40

Letter Grade

Conversion Points

Hours Attempted

Quality Points

A

4 points

4

4 * 4 = 16

D

1 points

4

1 * 4 = 4

C

2 points

2

2 * 2 = 4

Total = 10

Total = 24

GPA = 24 / 10 = 2.40

Requirements

You may work on this with up to one other person from the class – only if you both provide equal effort in coding. If working as a team of two, only turn in and submit one solution.

(~ 2 pts) Include class header documentation that contains a professional class description and your name. The description should provide a good summary overview of the class, but it need not be long.

The code for main below must be used. Re-align as needed after copying/pasting into your code

public static void main(String[] args) throws IOException

{

    Scanner inFile = openFile();

    String[] gradeSummary = processGrades(inFile);

    inFile.close();

    storeGpa(gradeSummary);

    System.out.println("Data processing complete.");

} // end main

(~8 pts) Create a static method named openFile that meets these requirements:

Contains proper header documentation that includes a professional description, @throws, and @return. Note that the @ throws can simply be “@throws IOException”.

Include internal comments to describe paragraphs of code.

Throw an IOException.

Prompt for and retrieves a file name from the user. Use the exact prompt shown in the sample run.

Check if the file exists and exits with a value of 1 if it does not.

Create and return a new Scanner object that refers to the opened input file.

(~10 pts)Create a static method named processGrades that meets these requirements:Contains proper header documentation that includes a professional description, @param, @precondition, @param, @throws, and @return.

Check the call in main to see what kind of formal parameter it takes.

The assumption (precondition) is that the input file parameter has been successfully opened.

The @throws can simply be “@throws IOException”.

It will return an array containing two strings:

“student’s full name”

“calculated gpa”

Include internal comments to document paragraphs of code.

Throw an IOException.

Read the student’s full name from the file and stores it into element one of the string array.

Continually read each grade (String) and hours attempted (int) until there are no more tokens left.

The String class split method as discussed for the Kansas Census stats lab program will be very helpful here!

When tallying the quality points, you may hard code in the numbers 1, 2, 3, and 4 here. They correspond to an “A”, “B”, “C”, or “D” read. There is no a need to add a 0 to the quality points if an “F” is read.

Calculate the GPA and store as a string into element two of the array. A helpful method is Double.toString(gpa).

(~6 pts) Create a static method named storeGpa that meets these requirements:

Contains proper header documentation that includes a professional description, @param, and @throws. Note that the @ throws can simply be “@throws IOException”. There is no return value from this method (i.e. void). Determine what the formal parameter is by looking at the method call in main.

Internal comments are optional as the method can be very short.

Throw an IOException.

Open a file for writing. The file name will consist of the student’s full name (element 0 ) with “.csv” appended to it. There is no need to check if the file previously existed.

Note: On some operating systems, it is a good practice to remove spaces from file names because spaces can make file management somewhat more difficult. Your program does not need to remove spaces from the file name.

Write the student’s full name, followed by a comma, followed by the GPA to the first line of the file. Two digits of precision must be used for the GPA. See the sample output files for examples.

Additional Note

Be sure to do a great job with coding standards including comments. Please don’t lose several points here.

Submission

Before class: Submit the source code to D2L and print the source code.

Beginning of class: Turn in the source code and staple as needed.

Sample Runs

Run 1

Input file: grades.csv

Resulting output file: Jack Johnson.csv

Jack Johnson

A,2

C,3

F,2

D,2

B,4

A,4

Jack Johnson,2.59

Screen output (user input in dark red)

Enter grade input file in the form filename.ext: grades.csv

Data processing complete.

Run 2

Input file: grades2.csv

Resulting output file: Miguel Ortiz.csv

Miguel Ortiz

C,4

A,3

F,2

Miguel Ortiz,2.22

Screen output (user input in dark red)

Enter grade input file in the form filename.ext: grades2.csv

Data processing complete.

Run 3 – showing output if the input file is not found

Screen output (user input in dark red)

Enter grade input file in the form filename.ext: grades

File open error: grades

grades.csv file content

Jack Johnson
A 2
C 3
F 2
D 2
B 4
A 4

grades2.csv content

Miguel Ortiz
C 4
A 3
F 2
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Hi, here is the completed code for you, implemented all three methods using well defined comments to explain each steps, and verified the output. Check the code and let me know if you have any doubts. Thanks.

// StudentGrade.java

import java.io.File;

import java.io.IOException;

import java.io.PrintWriter;

import java.util.Scanner;

public class StudentGrade {

                public static void main(String[] args) throws IOException

                {

                                Scanner inFile = openFile();

                                String[] gradeSummary = processGrades(inFile);

                                inFile.close();

                                storeGpa(gradeSummary);

                                System.out.println("Data processing complete.");

                } // end main

                /**

                * method to fetch student name and grades from a file and calculate the gpa

                *

                * @param inFile

                *            - input file scanner

                * @return - a String array containing two elements - student name and gpa

                */

                private static String[] processGrades(Scanner inFile) {

                                /**

                                * defining a String array of 2 elements

                                */

                                String[] data = new String[2];

                                if (inFile.hasNext()) {

                                                // fetching name

                                                String name = inFile.nextLine();

                                                data[0] = name;// adding to array

                                }

                                int quality_points = 0;

                                int total_points = 0;

                                double gpa = 0;

                                /**

                                * looping until the scanner has no more elements

                                */

                                while (inFile.hasNext()) {

                                                // fetching a line

                                                String line = inFile.nextLine();

                                                // splitting by comma

                                                String[] fields = line.split(",");

                                                // fetching grade

                                                char grade = fields[0].charAt(0);

                                                // parsing hours from string

                                                int hours = Integer.parseInt(fields[1]);

                                                /**

                                                * calculating the quality points from grade

                                                */

                                                switch (grade) {

                                                case 'A':

                                                                quality_points += 4 * hours;

                                                                total_points += hours;

                                                                break;

                                                case 'B':

                                                                quality_points += 3 * hours;

                                                                total_points += hours;

                                                                break;

                                                case 'C':

                                                                quality_points += 2 * hours;

                                                                total_points += hours;

                                                                break;

                                                case 'D':

                                                                quality_points += 1 * hours;

                                                                total_points += hours;

                                                                break;

                                                case 'F':

                                                                quality_points += 0 * hours;

                                                                total_points += hours;

                                                                break;

                                                }

                                }

                                /**

                                * finding the gpa once data has been read, please note that if the file

                                * doesn't contain any grade data, this will possibly cause divided by

                                * zero exception

                                */

                                gpa = (double) quality_points / total_points;

                                /**

                                * formatting the gpa to maintain two digits of precision

                                */

                                data[1] = String.format("%.2f", gpa);

                                return data;

                }

                /**

                * method to store the calculated gpa along with student name into a file

                * with student name as file name

                *

                * @param gradeSummary

                *            - String array containing name and gpa

                * @throws IOException

                */

                static void storeGpa(String[] gradeSummary) throws IOException {

                                //defining a file using student name

                                File outFile = new File(gradeSummary[0]);

                                //defining a print writer for writing data

                                PrintWriter writer = new PrintWriter(outFile);

                                //printing data to the file

                                writer.append(gradeSummary[0] + "," + gradeSummary[1]);

                                writer.close();

                }

                /**

                * method to prompt the user for a file name and check if the file exists,

                * and return a scanner

                *

                * @return - a scanner instance created from the file

                * @throws IOException

                */

                static Scanner openFile() throws IOException {

                                //defining a scanner to read user input

                                Scanner keyboard = new Scanner(System.in);

                                System.out.println("Enter the file name: ");

                                //getting file name

                                String fileName = keyboard.nextLine();

                                File file = new File(fileName);

                                if (!file.exists()) {

                                                //file doesnt exist, quitting

                                                System.exit(1);

                                }

                                //defining a scanner from input file

                                Scanner fileScanner = new Scanner(file);

                                return fileScanner;

                }

}

/*OUTPUT*/

Enter the file name:

grades.csv

Data processing complete.

/*Jack Johnson.csv after running the program*/

Jack Johnson,2.59

Add a comment
Know the answer?
Add Answer to:
Java Description You are given the task of reading a student’s name, semester letter grades, and...
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
  • Problem Specification: Write a C++ program to calculate student’s GPA for the semester in your class. For each student, the program should accept a student’s name, ID number and the number of courses...

    Problem Specification:Write a C++ program to calculate student’s GPA for the semester in your class. For each student,the program should accept a student’s name, ID number and the number of courses he/she istaking, then for each course the following data is needed the course number a string e.g. BU 101 course credits “an integer” grade received for the course “a character”.The program should display each student’s information and their courses information. It shouldalso display the GPA for the semester. The...

  • Design and code a JAVA program called ‘Grades’. ? Your system should store student’s name, and...

    Design and code a JAVA program called ‘Grades’. ? Your system should store student’s name, and his/her course names with grades. ? Your system should allow the user to input, update, delete, list and search students’ grades. ? Each student has a name (assuming no students share the same name) and some course/grade pairs. If an existing name detected, user can choose to quit or to add new courses to that student. ? Each student has 1 to 5 courses,...

  • Must be written in JAVA Code Write a program that will read in a file of student academic credit data and create a list of students on academic warning. The list of students on warning will be written...

    Must be written in JAVA Code Write a program that will read in a file of student academic credit data and create a list of students on academic warning. The list of students on warning will be written to a file. Each line of the input file will contain the student name (a single String with no spaces), the number of semester hours earned (an integer), the total quality points earned (a double). The following shows part of a typical...

  • 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...

  • 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...

  • Microsoft Visual Studios 2017 Write a C++ program that computes a student’s grade for an assignment...

    Microsoft Visual Studios 2017 Write a C++ program that computes a student’s grade for an assignment as a percentage given the student’s score and total points. The final score must be rounded up to the nearest whole value using the ceil function in the <cmath> header file. You must also display the floating-point result up to 5 decimal places. The input to the program must come from a file containing a single line with the score and total separated by...

  • Please help me do the java project For this project you will be reading in a...

    Please help me do the java project For this project you will be reading in a text file and evaluating it in order to create a new file that represents the Class that will represent the properties of the text file. For example, consider the following text file: students.txt ID              Name                              Age                    IsMale           GPA 1                Tom Ryan                       22                       True              3.1 2                Jack Peterson                31                       True              2.7 3                Cindy LuWho                12                       False             3.9 When you read in the header line, you...

  • C++ Program classes READ THE FOLLOWING CAREFULLY TO GET FULL VALUE FROM THE PRACTICE. IF NOT...

    C++ Program classes READ THE FOLLOWING CAREFULLY TO GET FULL VALUE FROM THE PRACTICE. IF NOT COMFORTABLE WITH CLASSES, I WOULD START WITH DEFINING THE CLASS WITH ONE constructor AND GO FROM THERE Use the following to calculate a GPA (the following is for calculation information only): A = 4.00 grade points per credit hour A- = 3.70 grade points per credit hour B+ = 3.33 grade points per credit hour B = 3.00 grade points per credit hour B-...

  • Java - In NetBeans IDE: • Create a class called Student which stores: - the name...

    Java - In NetBeans IDE: • Create a class called Student which stores: - the name of the student - the grade of the student • Write a main method that asks the user for the name of the input file and the name of the output file. Main should open the input file for reading . It should read in the first and last name of each student into the Student’s name field. It should read the grade into...

  • (7) Student grade point statistics [Problem description] There is a need to make statistics of the grade points o...

    (7) Student grade point statistics [Problem description] There is a need to make statistics of the grade points of the students in the first semester of 2018. It is assumed that there are n (can be set as 6) classes in this grade, and there are 20 students in each class, the total number of courses with exam is m (can be set as 10), and the percentage system is adopted for each course. The gpa is 4, 3, 2,1,...

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