MCX1/2 Mr.Stanley
Teitel, Principal
Mr. Gary Jaye
Mr.
Danny Jaye, Math Chair
1999 Question 1 from APCS A Exam: Part II (adapted for Java)
Assume that student records are implemented using the following declararion:
public class Student {
public Student(String nm, int cHrs, double gPts) {
name = nm; creditHours = cHrs, gradePoints = gPts;
}
public String getName() { return name; }
public int getCreditHours() { return creditHours; }
public double getGradePoints() { return gradePoints; }
public double getGPA() ( return GPA;}
public setGPA(double gpa) { GPA = gpa;}
private String name;
private int creditHours;
private double gradePoints, GPA;
}
(a) Write function computeGPA, as started below. computeGPA should fill in the GPA data
field for all the records in its ArrayList parameter roster. A student's GPA (grade
point average) is computed by dividing gradePoints by creditHours. The GPA for a
student with 0 credit hours should be set to 0.
(b) Write method isSenior, as started below. isSenior should return true if the given
student has at least 125 credit hours and has a GPA of at least 2.0; otherwise,
isSenior should return false.
For example:
result of the call
student isSenior(student)
____________________________________ __________________________
name creditHours gradePoints GPA
King 45 171 3.8 false (not enough credit hours)
Norton 128 448 3.5 true
Solo 125 350 2.8 true
Kramden 150 150 1.0 false (GPA too low)
Complete function isSenior below.
(c) Write function fillSeniorList, as started below. fillSeniorList determines which
students in the ArrayList roster are seniors and copies those students' records to
the ArrayList seniors.
In writing fillSeniorList, you may call method isSenior specified in part (b). Assume
that isSenior works as specified, regardless of what you wrote in part (b). Complete
function fillSeniorList below. Assume that fillSeniorList is called only with
parameters that satisfy its precondition.
//=========================================================================================
// A99_1.java
import java.util.*;
public class A99_1 {
public static void computeGPA(ArrayList roster) {
//precondition: A contains A.size() Student object variables
} //=== computeGPA
public static boolean isSenior(final Student student) {
//postcondition: returns true if this student's credit hours >= 125 and
// GPA >= 2.0; otherwise, returns false
return false;
} //=== isSenior
public static void fillSeniorList(final ArrayList roster, ArrayList seniors) {
//precondition: seniors.size() == 0
} //=== fillSeniorList
//---------------------------------------------------------------------------------------
static final String TAB = "\t";
public static void main(String[] args) {
cls();
System.out.println("\n\t1999 APCS A Exam: Question 1\n");
driver();
}//== main
static void driver() {
System.out.println("Testing computeGPA:\nroster BEFORE computeGPA:");
ArrayList A = makeArray();
print(A); computeGPA(A);
System.out.println("\nroster AFTER computeGPA:");
print(A);
System.out.println("\nTesting isSenior:");
for (int k = 0; k < A.size(); k++) {
Student student = (Student) A.get(k);
System.out.println(k + TAB + student + "\tisSenior = " + isSenior(student));
}
System.out.println("\nTesting fillSeniorList:");
ArrayList seniors = new ArrayList();
System.out.println("ArrayList seniors AFTER fillSeniorList:");
fillSeniorList(A, seniors);
print(seniors);
System.out.println();
}
static ArrayList makeArray() {
Student[] B = { new Student("King", 45, 171), new Student("Norton", 128, 448),
new Student("Solo", 125, 350), new Student("Kramden", 150, 150)};
ArrayList A = new ArrayList(B.length);
for (int k = 0; k < B.length; k++) A.add(B[k]);
return A;
}//=== makeArray
static void print(final ArrayList A) {
for (int k = 0; k < A.size(); k++) System.out.println(k + TAB + (Student)A.get(k));
}//=== print
static void cls() {
for (int k = 0; k < 55; k++) System.out.println();
}
}
class Student {
public Student(String nm, int cHrs, double gPts) {
name = nm; creditHours = cHrs; gradePoints = gPts;
}
public String getName() { return name; }
public int getCreditHours() { return creditHours; }
public double getGradePoints() { return gradePoints; }
public String toString() { return name + "\t" + creditHours + "\t" +
gradePoints + "\t" + GPA;}
public double getGPA() { return GPA;}
public void setGPA(double gpa) { GPA = gpa;}
private String name;
private int creditHours;
private double gradePoints, GPA;
}
//================================== end of A99_1 file