2003 AP Computer Science A Question 1, Java

The Java class College represents information about a college or university. The class defines methods for accessing a College object's name, tuition, and the region in which it is located; for setting the tuition; and specifies constants for naming regions.

public class College { public final static String NORTHEAST = "Northeast"; public final static String SOUTHEAST = "Southeast"; public final static String NORTHWEST = "Northwest"; public final static String MIDWEST = "Midwest"; public final static String SOUTHWEST = "Southwest"; public final static String WEST = "West"; public final static String SOUTH = "South"; // returns name of this college public String getName() { // not shown } // returns region of this college public String getRegion() { // not shown } // returns tuition of this college public int getTuition() { // not shown } // set tuition for this college to newTuition public void setTuition(int newTuition) { // not shown } }

The class CollegeGroup stores information about a group of colleges/universities. Part of the CollegeGroup class declaration is shown below.

import java.util.ArrayList; public class CollegeGroup { private College[] myColleges; // myColleges.length is # colleges // precondition: there exists a College in this group // whose name is collegeName, call this // myColleges[index] // postcondition: myColleges[index].getTuition() == newTuition, i.e., // the College with collegeName has // newTuition as its tuition public void updateTuition(String collegeName, int newTuition) { // you will write this code } // precondition: low <= high // postcondition: returns ArrayList of College objects // from this group in specified region // whose tuition is between (including) // low and high, i.e., low <= tuition <= high public ArrayList getCollegeList(String region, int low, int high) { // you will write this code } }

The following chart shows an example of colleges/universities that could appear in an object of type CollegeGroup.

  Name Region Tuition
0 Colgate University Northeast $27,025
1 Duke University Southeast $26,000
2 Kalamazoo College Midwest $19,764
3 Stanford University West $25,917
4 Florida International University Southeast $10,800
5 Dartmouth College Northeast $27,764
6 Spelman College Southeast $11,455

Part A

Write the CollegeGroup method updateTuition, which is described as follows. Method updateTuition associates a new tution with the college whose name is passed as a parameter.

Complete method updateTuition below.

class CollegeGroup { //not all methods, fields shown // precondition: there exists a College in this group // whose name is collegeName, call this // myColleges[index] // postcondition: myColleges[index].getTuition() == newTuition, i.e., // the College with collegeName has its // newTuition as its tuition public void updateTuition(String collegeName, int newTuition) { } }

Part B

The table below is repeated for your convenience.

  Name Region Tuition
0 Colgate University Northeast $27,025
1 Duke University Southeast $26,000
2 Kalamazoo College Midwest $19,764
3 Stanford University West $25,917
4 Florida International University Southeast $10,800
5 Dartmouth College Northeast $27,764
6 Spelman College Southeast $11,455

Write the CollegeGroup method getCollegeList, which is described as follows. Method getCollegeList returns an ArrayList of colleges that are locted in the specified region and whose tuition is in the range between low and high, inclusive. The size of the ArrayList should be equal to the number of colleges that meet the criteria of region and tuition range.

For example, if the object colleges is an instance of the class CollegeGroup and represents the entries shown in the chart above, the call

ArrayList list = colleges.getCollegeList(College.SOUTHEAST,10000,20000); should store in list an ArrayList of two elements containing objects representing Florida International University and Spelman College (note that Duke University is not included because its tuition is not in the specified range and Kalamazoo College is not included because it is not in the specified region).

Complete the method below.

public class CollegeGroup { //not all methods, fields shown private College[] myColleges; // myColleges.length is # colleges // precondition: low <= high // postcondition: returns ArrayList of College objects // from this group in specified region // whose tuition is between (including) // low and high, i.e., low <= tuition <= high public ArrayList getCollegeList(String region, int low, int high) { } }
Owen L. Astrachan
Last modified: Wed May 14 16:39:07 EDT 2003