2005 Average
<< 2005Tickets | APQuestionsTrailIndex | 2004 WordList >>
Question from the College Board
public class StudentRecord
{
private int[] scores;
public StudentRecord(int[] scores) {
this.scores = scores;
}
/**
* returns the average (arithmetic mean) of the
* whose subscripts are between first and last,
* precondition: 0 <= first <= last < scores.length
*/
public double average(int first, int last)
{
// part (a)
return 0D;
}
/**
* returns true if each successive value in scores is greater
* than or equal to the previous value;
* otherwise, returns false
*/
public boolean hasImproved()
{
//part (b)
return false;
}
/**
* if the values in scores have improved, returns the average
* of the elements in scores with indexes greater than or equal
* to scores.length/2;
* otherwise, returns the average of all of the values in scores
*
*/
public double finalAverage()
{
// part (c)
return 0D;
}
public static void main (String[] args)
{
//three different ways to make our int arrays
int[] a1={50, 50, 20, 80, 53};
int a2[]={20, 50, 50, 53, 80};
int[] a3=new int[4];
a3[0]=20;
a3[1]=a3[2]=50;
a3[3]=80;
StudentRecord s1=new StudentRecord(a1);
StudentRecord s2=new StudentRecord(a2);
StudentRecord s3=new StudentRecord(a3);
System.out.println("s1 improved? "+ passFail(false, s1.hasImproved()));
System.out.println("s2 improved? "+ passFail(true, s2.hasImproved()));
System.out.println("s3 improved? "+ passFail(true, s3.hasImproved()));
System.out.println("\ns1 average(0,1): "+ passFail(50, s1.average(0, 1)));
System.out.println("s2 average(1,2): "+ passFail(50, s2.average(1, 2)));
System.out.println("s3 average(0,2): "+ passFail(40, s3.average(0, 2)));
System.out.println("\ns1 final average: " + passFail(50.6, s1.finalAverage()));
System.out.println("s2 final average: " + passFail(61.0, s2.finalAverage()));
System.out.println("s3 final average: " + passFail(65.0, s3.finalAverage()));
}
}
Driver to test it out:
public class AverageTester{
public static void main (String[] args)
{
//I made 3 integer arrays in three different ways!
int[] a1={50, 50, 20, 80, 53};
StudentRecord s1=new StudentRecord(a1);
int a2[]={20, 50, 50, 53, 80};
StudentRecord s2=new StudentRecord(a2);
int[] a3=new int[4];
a3[0]=20;
a3[1]=a3[2]=50;
a3[3]=80;
StudentRecord s3=new StudentRecord(a3);
System.out.println("s1 improved? (should be false):"+ s1.hasImproved());
System.out.println("s2 improved? (should be true):"+ s2.hasImproved());
System.out.println("s3 improved? (should be true):"+ s3.hasImproved());
System.out.println("s1 average? (should be 50.6):"+ s1.finalAverage());
System.out.println("s2 average? (should be 61.6):"+ s2.finalAverage());
System.out.println("s3 average? (should be 65.0):"+ s3.finalAverage());
}
}
