Skip Top Navigation Bar

AP Computer Science A Free Response Practice Using For Loops

Grade Improvement

This single part question is worth 4 points.

AP CSA Alignment

Unit Alignment Learning Objectives Skills
Unit 1: Selection and Iteration 1.14.A Develop code to call instance methods and determine the result of these calls. 2.C Write program code involving procedural abstractions.
Unit 2: Selection and Iteration 2.3.A Develop code to respresent branching logical processes by using selection statements and determine the result of these processes. 2.A Write program code to implement an algorithm.
Unit 2: Selection and Iteration 2.8.A Develop code to represent iterative processes using `for` loops and determine the result of these processes. 2.A Write program code to implement an algorithm.
Unit 2: Selection and Iteration 2.9.A Develop code for standard and original algorithms (without data structures) and determine the result of these algorithms. 2.A Write program code to implement an algorithm.

The student grade analzyer program examines a students most recent 5 grades and determines the number of times the student's grade improved from one assignment to the next.

Consider the following GradeAnalyzer class.

public class GradeAnalyzer {
    private GradeBook grades;

    public GradeAnalyzer (GradeBook g) { 
        //not shown
    }

    /*
    * The countImprovement method analyzes the first 5 grades 
    * in GradeBook grades to determine the number of assignment
    * have been improved since the previous as described below.
    */
    public int countImprovement() {

    }
    //Some methods may not be shown
}

The GradeAnalyzer class uses the GradeBook class.

Consider the following GradeBook class.

public class GradeBook {
    //instance variable and constructors not shown

    //returns the grade associated with each assignment
    int getGrade(int assignment) {
    }
}

Each grade in GradeBook can be accessed by calling the getGrade method and passing the grade number 1 - how many grades have been recorded. There are at least 5 grades in GradeBook.

countImprovement() Method

Write the countImprovement method of the GradeAnalyzer program. This method will count the number of times the grades improve from one assignment to the next for the first 5 grades in GradeBook grades. For each consecutive pair of assignments (for example, grade 1 and grade 2), if the grade increased, this counts as one improvement.

For example, if the first 5 grades are 70, 75, 72, 80, 85, then the improvments are:

  • 70 to 75 (improved)
  • 75 to 72 (not improved)
  • 72 to 80 (improved)
  • 80 to 85 (improved)

In this case the method would return 3.

You must use a for loop when answering this question.

Write Your Response

  • In order to test your program, we have provided some grades for the getGrades method to analyze. You can modify these values to create additional tests if you want.
  • Write code to complete the countImprovement() method in the Java Playground below.
  • All four test should return true if your code is correct.