CS230 Data Structures, Wellesley College

Assignment 2

Due: Thursday, February 16, in class

[CS230 Home Page] [Syllabus] [Assignments] [Documentation] [CS Dept.]

Choosing the Right Computer Science Graduate School

There are many online resources that provide valuable information about graduate programs in Computer Science, and how to choose programs that best fit an individual student's interests and needs. One particularly interesting website allows you to customize the ranking of graduate programs based on your own weighting of various criteria used to judge the quality of these programs. This site was created by Geoff Davis while he was on the faculty at Dartmouth College, and is located at http://graduate-school.phds.org/rankings/computer-science

In this problem, you will implement a small version of a program that produces a customized ranking of Computer Science graduate programs. Some initial skeleton code is contained in the /home/cs230/download/GradSchools directory on puma. First copy this directory to your own CS230 subdirectory. The program consists of two code files: One to represent a School, and one to represent a group of Grad Schools.

School.java

This code file defines a new class named School to store information about a single graduate school program. A set of instance variables are already declared in the file:

private String name;     // name of school
private int rateWomen;   // rating of percentage of women PhD's
private int rateAI;     // rating of AI program
private int rateSys;     // rating of Computer Systems program
private int rateTheory;     // rating of Theory program
private int rateEffect;     // rating of effectiveness in educating research scholars
private int ratePubs;     // rating of impact of faculty publications
private int overallRating;     // overall rating that uses all six factors
private int rankValue;     // value used to rank schools

Each rating variable is assumed to be an integer between 1 and 10. This file also contains several public instance methods for accessing the instance variables, and an instance method toString() that returns a String containing the information stored in a single School object. The remainder of this file contains comments for two methods that you should complete:

(1) a constructor method that has 7 inputs corresponding to the first 7 instance variables for the School class. The final instance variables, overallRating and rankValue, can be initialized to 0.

(2) an instance method computeRating() that has 6 inputs that represent weights (integers between 0 and 5) to be associated with each of the 6 factors used to evaluate graduate programs (women PhD's, rating of AI, Computer Systems and Theory programs, effectiveness, and rating of faculty publications). IMPORTANT:This method should compute an overall rating for the school's graduate program as shown below, and assign this value to the overallRating instance variable:

   Σ (weight * factor)

factor refers to one of the 6 factors being considered, weight is the weight associated with this factor, and the sum (Σ) is computed over the set of 6 factors.

GradSchools.java

This code file defines a new class named GradSchools that stores information about multiple School objects. The instance variables, which are already declared, include a variable schools that is an array of School objects, and an integer numSchools that stores the number of School objects currently contained in the array. This file includes an instance method printGradSchools() that prints all of the information stored in the GradSchools object used to invoke this method. Note that in this printing method, when the statement System.out.println(schools[i])is executed, the toString() method defined in the School class is automatically invoked to obtain a String containing the contents of the School object stored at index i of the schools array. This string is then printed by the println() method.

In this file, first complete the definition of the following three methods:

(3) a constructor method that has a single input that is the size of the schools array to be created.

(4) an instance method addSchool() that has 7 inputs corresponding to the first 7 instance variables for the School class. This method should create a new School object and add it to the GradSchools object that is used to invoke the method. If the array in the GradSchools object is full, the method should create a new array twice the size of the existing array, copy over the elements from the existing array to the new array, update the instance variable to refer to the new array, and then add the new School object appropriately.

(5) an instance method computeRatings() that has 6 inputs that represent the weights to be associated with each of the 6 factors that are used to evaluate the graduate programs. This method should compute the overallRating for all of the School objects stored in the schools array, using the computeRating() method defined in the School class.

When writing a new program, it can be advantageous to reuse existing code, rather than developing the entire program from scratch. Unfortunately, the existing code may not fit the new context perfectly and may need to be modified. For the final method described below, your starting point will be some existing code that performs a sorting operation.

(6) The GradSchools.java code file contains two class methods sortArray() and swap(). sortArray() is a public method that has a single input that is an array of integers, and sorts these integers in increasing order. swap() is a private auxiliary method used by sortArray(). First study the existing code to determine what is the overall strategy used to sort the integers. Add comments to the code file summarizing this strategy. Try to summarize the strategy in abstract terms, rather than simply restating all of the code statements in words. For example, consider the loop in the following code segment:

int[] A = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int size = A.length;
int i, temp;
for (i = 0; i < size/2; i++) {
    temp = A[i];
    A[i] = A[size-i-1];
    A[size-i-1] = temp;
}

In the case of the for loop, you could describe its function as something like "steps through each location in the bottom half of the array A, and at each index i, first stores the contents of A in a temporary variable, then copies the contents of A from the index size-i-1 into the array at index i, and finally stores the value of the temporary variable into the array at index size-i-1," but this is just a literal restatement of the code. A better high-level description would be "reverses the order of the integers stored in the array A." The strategy could be summarized in more detail as "swaps each integer stored in the lower half of the array at index i with the integer stored in the upper half of the array at index A.length-i-1."

(7) Write an instance method rankSchools() that has a String input that specifies either a single factor, or the overall rating factor, and uses this factor to rank-order the graduate school programs. More specifically, this method should use this factor to sort the School objects in the schools array, and then print the names of the schools, in order from highest to lowest rank. For example, suppose that a GradSchools object has been created named myGradSchools. The method call

myGradSchools.rankSchools("AI");

might result in printout like the following:

Ranking of schools from highest to lowest using AI
MIT
CMU
Stanford
Univ. of Illinois
UC Berkeley
Univ. of Washington
Cornell
Princeton

The implementation of rankSchools() should use the rankValue instance variable in the School objects to assist with the sorting process. In particular, the values that should be used as the basis for rank ordering the schools can first be copied into the rankValue variable for each School object in the schools array. The School objects can then be sorted in order of increasing rankValue. For example, if the input string is "AI", then the value of the rateAI variable should be copied into the rankValue variable for each School object. If the input string is "overall", then the value of the overallRating variable should be copied into the rankValue variable. The sortArray() and swap() methods can then be modified to perform the task of sorting all of the School objects in the schools array using the rankValue to compare School objects. You will need to make several small changes to the existing sortArray() and swap() methods to adapt them to this new context, but you should not make any major changes to the structure and length of these methods. As part of your modifications to these two methods, change them from class methods to instance methods.

(8) Finally, the code file contains a main() method that includes code to test some of the methods defined in the GradSchools class. Add test cases to thoroughly test your code, including calls to the method rankSchools().

What to turn in

On the day the assignment is due, bring to class a hardcopy of your final School.java and GradSchools.java code files. Hardcopies of these two files should be stapled together with a cover page. Also, drop off an electronic copy of your code file by connecting to your GradSchools directory and executing the following command:

submit cs230 GradSchools *.*

Note: Be sure to complete the comments at the top of each code file regarding the status of the file.