\Java
StudentGrades.java
//********************************************************************
//
// StudentGrades.java
// Program Title: Student Grade File Processing
//
//********************************************************************
//
// Program Description:
// -------------------
//
// This is a program that updates a student's cumulative grade data after new grades
// for the most recent quarter have been recorded and prints out the new updated results and
// the results for the information on the most recently completed quarter
//
//
// Input Requirements:
// ------------------
//
// A text file contains grade data for many students. Data for each student is stored on
// three lines of the file. This is called a record. There may be any number of records in
// a file, including zero. Data values on each line are separated from each other by white
// space. However, the Student Name field may contain embedded blanks. The description of
// the data fields in a record for one student is:
//
// First Line: Student identification data
// 1. Student ID#: student's ID #: 9 decimal digits (with no dashes between the digits)
// 2. Student name: student's last name, first name, and initial(s):
// There will be blank spaces within the name separating each part of the name. The
// number of parts of a name can be different for each student (some names may be 1 or 2
// words and others as many as 4 or 5 words).
//
// Second Line: Cumulative grade data
// 1. Cumulative number of quarters completed: the number of quarters the student has
// completed to date (not including the current quarter): an integer in the range 0-40
// 2. Cumulative credits: the number of credits the student has completed to date (not
// including the current quarter): an integer in the range 0-300
// 3. Overall GPA: the student's grade point average to date (not including the current
// quarter): a floating point number in the range 0.00-4.00 with two decimal places
//
// Third Line: Current quarter grade data
// 1. Count of courses taken in the current quarter:
// an integer between 2 and 6 (each student may have a different count)
// 2. Credit and grade data for each course (same number of credits and grades as the
// count value):
// This data appears in the order: # credits 1st course, grade for 1st course, # credits
// 2nd course, grade for 2nd course, ...
// Each credit value is an integer in the range 2-5 and each grade value is a floating
// point number in the range 0.0 to 4.0.
//
// Last Line:
// 1. The end of the input data is signaled by a sentinel value of zero as a student ID#.
// There is no other data after the zero.
//
//
// Output Requirements:
// -------------------
//
// The output includes three lines of text for each student. The output for each student is:
//
// First Line: Student identification data -- same as the input
// 1. Student ID#
// 2. Student name
//
// Second Line: Cumulative grade data -- same as the input except quarters completed, credit
// units, and GPA are the new updated values.
// 1. Cumulative number of quarters completed
// 2. Cumulative credits
// 3. Overall GPA
//
// Third Line: Current quarter grade data
// 1. Count of courses current quarter (same as input file)
// 2. Total credits current quarter: an integer
// 3. Quality points current quarter: a floating point number with one decimal place
// 4. GPA current quarter: a floating point number with two decimal places
// 5. Letter grade current quarter: a single character, A, B, C, D, or F
// (no + and - grades are computed in this program)
//
// Last Line:
// The end of the output is signaled by a sentinel value of zero as a student ID#. There
// is no other output after the zero
//
// Problem Solution Discussion:
// ---------------------------
//
// This Program first reads the value of the ID number. if it is not equal to zero, it
// enters the loop. Then, it inputs the name of sudent, cumulative number of quarters,
// cumulative number of credits, overall GPA prior to this quarter, and number of courses in
// this quarter. It then runs another loop for the amount of times as the number of courses
// the student is taking. In the loop, it inputs the amount of credits and grade for each
// course taken in the quarter and takes the sum of each. To get the quality points for the
// current quarter, the product of the grade and number of credits of each course are all
// summed up. the total credits taken that quarter is also the sum of the credits of each
// course. exit loop. To get the quality points for the prior quarters, we multiply the
// cumulative number of credits prior to this quarter with the GPA prior to this quarter.
// Then to get the average GPA for this quarter, the quality points of the quarter is divided
// by the total credits taken this quarter. The cumulative number of quarters is incremented
// by one. The new cumulative credits is the cumulative credits before this quarter plus the
// total credits taken this quarter. the new overall GPA is the sum of the quality points
// prior to this quarter and the quality points of this quarter all divided by the cumulative
// amount of credits. The letter grade for the quarter is derived by the following:
// if GPA current quarter >= 3.67, then A
// if current quarter GPA < 3.67 and >= 3.0, then B
// if current quarter GPA < 3.0 and >= 2.0, then C
// if current quarter GPA < 2.0 and >= 1.0, then D
// if current quarter GPA < 1.0, then F
// exit loop.
//
// Data Structures:
// ---------------
//
// Name Data Type Description
// --------- --------- -----------------------------
// IDNum, integer student ID number
// cumQuarters integer cumulative number of quarters completed
// cumCredits integer cumulative number of credits completed
// curNumCourses integer number of courses in current quarter
// curCredits integer number of credits in course in current quarter
// curTotCredits integer total number of credits in current quarter
// count integer used to define amount of times to run loop
// oldGPA float overall GPA prior to current quarter
// curGrade float grade in specific course taken this quarter
// curGPA float GPA of current quarter
// curQualPts float quality points in current quarter
// oldQualPts float quality points prior to current quarter
// newGPA float overall GPA after current quarter
// name String student name
// letterGrade String average letter grade for current quarter
//
//
// Problem Solution Design Steps:
// -----------------------------
//
//input IDnum (priming read)
//run loop while IDnum not equal to zero (sentinel)
// enter loop
//
// input name
// input cumQuarters
// input cumCredits
// input oldGPA
// input curNumCourses
// set count equal to curNumCourses
// run loop while count > 0
//
// enter loop
// input curCredits
// input curGrade
// set curQualPts equal to curQualPts + {curCredits * curGrade)
// set curTotCredits equal to curTotCredits + curCredits
// reduce count by increment of 1
// exit loop
//
// set oldQualPts equal to (cumCredits * oldGPA)
// set curGPA equal to (curQualPts / curTotCredits)
// cumQuarters up by 1
// set cumCredits equal to (cumCredits + curTotCredits)
// set newGPA equal to ((oldQualPts + curQualPts) / cumCredits)
//
// if curGPA >= 3.67, then letterGrade = A
// if curGPA < 3.67 and >= 3.0, then letterGrade = B
// if curGPA < 3.0 and >= 2.0, then letterGrade = C
// if curGPA < 2.0 and >= 1.0, then letterGrade = D
// if curGPA < 1.0, then letterGrade = F
//
// print IDNum and name on same line
// print cumQuarters, cumCredits, and newGPA (extended to 2 decimal places) on same line
// print curNumCourses, curCredits, curQualPts (extended to one decimal place),
// curGPA(extended to 2 decimal places), and letterGrade on same line
// input IDNum
//
// exit loop
//
//print 0
//
//end
//********************************************************************
import cs1.Keyboard; //textbook's Keyboard input class
import java.text.DecimalFormat; //class to extend number out to # decimal places
public class StudentGrades
{
//-----------------------------------------------------------------
// The class main method
//-----------------------------------------------------------------
public static void main (String[] args)
{
int IDNum, //student ID number
cumQuarters, //cumulative number of quarters completed
cumCredits, //cumulative number of credits completed
curNumCourses, //number of courses in current quarter
curCredits=0, //number of credits in course in current quarter
curTotCredits, //total number of credits in current quarter
count; //used to define amount of times to run loop
float oldGPA, //overall GPA prior to current quarter
curGrade, //grade in specific course taken this quarter
curGPA, //GPA of current quarter
curQualPts, //quality points in current quarter
oldQualPts, //quality points prior to current quarter
newGPA; //overall GPA after current quarter
String name, //student name
letterGrade = ""; //average letter grade for current quarter
DecimalFormat fmtGPA = new DecimalFormat ("0.00");
DecimalFormat fmtPts = new DecimalFormat ("#.0");
//input IDnum (priming read)
IDNum = Keyboard.readInt();
//run loop while IDnum not equal to zero (sentinel)
while (IDNum != 0)
{
//initialize all necessary values to zero so program doesnt include prior student's stats as well
cumQuarters = 0;
cumCredits = 0;
curNumCourses = 0;
curTotCredits = 0;
oldGPA = 0;
curGrade = 0;
curQualPts = 0;
oldQualPts = 0;
newGPA = 0;
letterGrade = "";
//input name
name = Keyboard.readString();
//input cumQuarters
cumQuarters = Keyboard.readInt();
//input cumCredits
cumCredits = Keyboard.readInt();
//input oldGPA
oldGPA = Keyboard.readFloat();
//input curNumCourses
curNumCourses = Keyboard.readInt();
//set count equal to curNumCourses
//run loop while count > 0
//reduce count by increment of 1
for (count=curNumCourses; count > 0; count--)
{
//input curCredits
curCredits = Keyboard.readInt();
//input curGrade
curGrade = Keyboard.readFloat();
//set curQualPts equal to curQualPts + (curCredits * curGrade)
curQualPts += (curCredits * curGrade);
//set curTotCredits equal to curTotCredits + curCredits
curTotCredits += curCredits;
}
//set oldQualPts equal to (cumCredits * oldGPA)
oldQualPts = (cumCredits * oldGPA);
//set curGPA equal to (curQualPts / curTotCredits)
curGPA = (curQualPts / curTotCredits);
//cumQuarters up by 1
cumQuarters ++;
//set cumCredits equal to (cumCredits + curTotCredits)
cumCredits += curTotCredits;
//set newGPA equal to ((oldQualPts + curQualPts) / cumCredits)
newGPA = ((oldQualPts + curQualPts) / cumCredits);
//if curGPA >= 3.67, then letterGrade = A
if (curGPA >= 3.67)
letterGrade = "A" ;
//if curGPA < 3.67 and >= 3.0, then letterGrade = B
else if (curGPA < 3.67 && curGPA >= 3.0)
letterGrade = "B" ;
//if curGPA < 3.0 and >= 2.0, then letterGrade = C
else if (curGPA < 3.0 && curGPA >= 2.0)
letterGrade = "C";
//if curGPA < 2.0 and >= 1.0, then letterGrade = D
else if (curGPA < 2.0 && curGPA >= 1.0)
letterGrade = "D" ;
//if curGPA < 1.0, then letterGrade = F
else if (curGPA < 1.0)
letterGrade = "F";
//print IDNum and name on same line
System.out.println (IDNum + " " + name);
//print cumQuarters, cumCredits, and newGPA (extended to 2 decimal places) on same line
System.out.println (cumQuarters + " " + cumCredits + " " + fmtGPA.format(newGPA));
//print curNumCourses, curTotCredits, curQualPts (extended to one decimal place), curGPA(extended to 2 decimal places), and letterGrade on same line
System.out.println (curNumCourses + " " + curTotCredits + " " + fmtPts.format(curQualPts) + " " + fmtGPA.format(curGPA) + " " + letterGrade);
//input IDNum
IDNum = Keyboard.readInt();
}
//print 0
System.out.println (0);
} //end main
} // end class StudentGrades