CMPS 134 Fall 2025
Quiz #3 Sample Solution

The problem was to complete a method that computes the number of quality points earned by a student, given an instance of the CSV_String class that represents a CSV-String containing the relevant data. Each field in that CSV-String identifies a course (e.g., "MATH123"), the number of credits that it is worth (e.g., 3), and the letter grade (e.g., "B+") earned by the student in that course. With each letter grade there is an associated quality-points value (e.g., "B+" maps to 3.67, etc.). The total number of quality points earned by the student is the sum, taken over all courses, of the number of course credits by the quality-points value associated to the grade earned. All this would have been familiar to any student who completed the GPA_App program from Prog. Assg. #5.

Assumed to be available was the method gradeToQualityPoints() that maps each letter grade to the corresponding quality-points value:

// Returns the # of quality points associated to the given letter grade.
// (E.g., "A" maps to 4.0, "A-" maps to 3.67, etc.)
public static double gradeToQualityPoints(String grade) { ... } 

Here is the incomplete method that the student was to complete:

/* Given is a CSV-string whose fields are separated by commas. Each field 
** identifies a course, a # of credits, and a letter grade, separated by
** single spaces. Example: "CMPS134 3 B+,MATH142 4 B-,HIST157 3 C+".
** To be returned is the total number of quality points earned.
*/
public static double totalQualityPoints(String coursesInfo) { 

   // Create an object of the CSV_String class using the parameter.
   CSV_String coursesCSV = new CSV_String(`,', coursesInfo);

   double totQualPnts = 0.0;   // to be returned at the end

   // Iterate through the fields of coursesCSV and accumulate the result

   MISSING CODE
   

   return totQualPnts;
}

In what follows, we use the blue font to indicate pseudocode needing to be refined. We start with some high-level pseudocode:

public static double totalQualityPoints(String coursesInfo) { 

   // Create an object of the CSV_String class using the parameter.
   CSV_String coursesCSV = new CSV_String(`,', coursesInfo);

   double totQualPnts = 0.0;   // to be returned at the end

   // Iterate through the fields of coursesCSV and accumulate the result

   for each field in coursesCSV {
      double qualPnts = # quality points earned in the course described
                        by that field of coursesCSV; 

      totQualPnts = totQualPnts + qualPnts
   } 
   return totQualPnts;
}

Now we refine the loop heading and part of the loop body:

public static double totalQualityPoints(String coursesInfo) { 

   // Create an object of the CSV_String class using the parameter.
   CSV_String coursesCSV = new CSV_String(`,', coursesInfo);

   double totQualPnts = 0.0;   // to be returned at the end

   // Iterate through the fields of coursesCSV and accumulate the result

   for (int j=0; j != coursesCSV.numberOfFieldsIn(); j=j+1) {
      String oneCourse =  coursesCSV.getIthField(j);  // (e.g., "CMPS134 3 B-")
      
      numCredits = numeric value of credits component in oneCourse(e.g., 3)
      grade = grade component in oneCourse (e.g., "B-")
      
      qualPnts = numCredits * gradeToQualityPoints(grade)  (e.g., 3 * 2.67)

      totQualPnts = totQualPnts + qualPnts
   }
   return totQualPnts;
}

What remains to be refined is the "extraction" of the number of credits and the grade from oneCourse. The solution offered next makes use of the fact that the substring of oneCourse indicating the number of credits lies between the two spaces and the grade follows the 2nd (and last) space.

/* Given is a CSV-string whose fields are separated by commas. Each field 
** identifies a course, a # of credits, and a letter grade, separated by
** single spaces. Example: "CMPS134 3 B+,MATH142 4 B-,HIST157 3 C+".
** To be returned is the total number of quality points earned.
*/
public static double totalQualityPoints(String coursesInfo) { 

   // Create an object of the CSV_String class using the parameter.
   CSV_String coursesCSV = new CSV_String(`,', coursesInfo);

   double totQualPnts = 0.0;   // to be returned at the end

   // Iterate through the fields of coursesCSV and accumulate the result

   for (int j = 0; j != coursesCSV.numberOfFieldsIn(); j = j+1) {
      String oneCourse = coursesCSV.getIthField(j);

      int posOf1stSpace = oneCourse.indexOf(' ');
      int posOf2ndSpace = oneCourse.lastIndexOf(' ');
      String creditsStr = oneCourse.substring(posOf1stSpace+1, posOf2ndSpace);
      int numCredits = Integer.parseInt(creditsStr);
      String grade = oneCourse.substring(posOf2ndSpace+1);

      double qualPnts = credits * gradeToQualityPoints(grade);
      totQualPnts = totQualPnts + qualPnts;
   }
   return totQualPnts; 
}

An alternative way of extracting the number of credits and the grade from oneCourse would be to treat oneCourse as itself being a CSV-String in which the delimiter is the space character. Taking that approach, the following code has the effect of assigning to numCredits and grade the desired values:

CSV_String oneCourseCSV = new CSV_String(' ', oneCourse);
String creditsStr = oneCourseCSV.getIthField(1);
int numCredits = Integer.parseInt(creditsStr);
String grade = oneCourseCSV.getIthField(2);