CS230 Assignment 3

Assignment 3

 

DUE: Thursday Sep 26 at 11:59pm

  • Shared Google Doc for finding/recording partners
  • How to turn in your assignment: To Gradescope, submit Histogram.java and Task1Test for Task 1 and School.java, GradSchools.java, and Task2Test for Task 2. Also submit a zip file of all materials to the CS Server: put it in your public_html/cs230-assignments/ps03 folder in your account on the CS Server.
  • Cover page - submit the form.
  • For Task 2 and only Task 2, you are required to work with a partner as part of a two-person team. Please consult the course collaboration policy for futher details.
  • For every task, include a test transcript file showing testing of your solution, e.g., the contents of the BlueJ Terminal Window demonstrating correct executions of your program on various sample data (don't forget edge cases!)
  • Remember that for this and all assignments, you could use a 48 hour extension without asking for permission. However, if those two extra days are not sufficient for you to complete the assignment, you are required to contact the instructor and discuss a plan for completing the assignment before the assignment is due. We will work together to make sure that plan is a reasonable and effective so that it supports both your learning and your health.
 

Goals:

Practice with:

  • Manipulating arrays of primitive data types and of objects. (No use of Java's Arrays package is permitted.)
  • Using looping constructs appropriately for processing array elements.
  • Getting a better understanding of testing and debugging arrays.
 

Task 1: A Histogram of randomly generated numbers

In this first task you will experiment with creating arrays and using them to count the values they contain. Write a program called Histogram.java that does the following:

  • create an array data of size MAXDATA (e.g., 500) and fill it up with random integers between 1 and MAXRANGE (e.g. 100)
  • create another array frequencyRanges of size MAXRANGE. This array will count the number of times a particular integer in data was randomly generated. While you debug, you may want to have some printing statements that will show you that you compute the frequencies correctly. (Hint: Which example we saw in class does this remind you of?)
  • finally, print the histogram allocating a line per 10 frequencies. Your printout should look like that:


01-10 |+++++
11-20 |+++++
21-30 |++++
31-40 |+++++
41-50 |++
51-60 |++++++++
61-70 |+++++++
71-80 |++++
81-90 |+++
91-100 |+++++++

  • Since this is a short problem of number crunching, you do not need to create explicitly any objects of the class Histogram. (Hint: In this case, your methods should be.. instance? static?)
  • You are encouraged to break down your main into functions that clarify the process as described above.
  • Testing a program that uses random data is tricky. Hint: Test with fewer data (e.g 5 or 10, not 500) and print them so that you can check that they end up counted in the right frequency and that are marked by a + in the right frequency range.
 

Task 2 (Pair programming): Choosing the top CS graduate school

For this task and only this task, you are required to work with a partner.

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 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. In this problem, you will implement a small version of a program that computes the top-ranking graduate school based on some factor (academics, research, publication, or overall).

Subtask 1: School.java

Define a class, School, to store information about a single graduate school program. A School instance has the following properties:

  • its name (a String)
  • its academic programs
  • its effectiveness in educating research scholars
  • the impact of its faculty publications

In the definition of the School class, each of the above properties should be represented as a private instance variable. You should enforce that the value of each of the ratings above is an integer in the range [1 .. 10]. Add two more instance variables to your 'School' class:

  • overall rating (an integer) for a school is computed based on the ratings of the three individual rankings of the school (weighted sum of them, see below).
  • rank value (an integer) to compare Schools and simplify the computing of the top school in some category (academic, research, publications of overall). Step 6 of Subtast 2 has more information on how this variable is used.

Your task: (Hint: Add the code in the following order!)

  1. Set up the instance variables, according to the description above.
  2. Provide a constructor to create instances of School objects, given values for the four properties.
  3. Provide means for printing an instance of type School.
  4. Provide getters (accessors) and setters (mutators) for all the private instance variables in the 'School' class. All but the next one are straightforward.
  5. Provide a method, setOverallRating(), that computes the overall a School instance and sets the appropriate instance variable (overall rating). Different users may value each of three ranking factors (academic programs, research and publications) differently, so this method should take 3 arguments, one per factor. In particular, the method should take as arguments three weights, with values in the integer range [1 .. 5], to be associated with each of the three evaluation factors. Then, it should compute the overall rating as the weighted sum, Σ(weight * factor), of the three evaluation factors, and set the overall rating accordingly.
  6. As usual, add a main() method in this class to test your work.

Subtask 2: GradSchools.java

Define a new class, GradSchools, that stores information about a collection of School objects. Use an array as the container for the School objects. Once the user enters her weights for Academics, Research and Publications, the program computes the overall rating for all schools in the collection, as the weighted sum of the three rating factors. Then, it sorts the schools according to each one of the ranking factors, and prints the results. (See the Sample run provided at the end of this document.)

Your task: (Hint: Add the code in the following order!)

  1. Set up the appropriate instance variables in the GradSchools class.
  2. Provide a constructor for the GradSchools class.
  3. Provide means for printing an instance of type GradSchools. (Hint: This method should implicitly use the printing method in the School class!)
  4. Define a method, addSchool(), that adds a School instance to the collection. If the school already exists in the collection, it will update its information.
  5. Define a method, computeAllRatings() that computes and sets the overall rating for all schools in the collection, based on the three weights that are passed as parameters to it.
  6. Provide a method findTopSchool(String factor) to find the top schools in the collection, based on its input factor. (If there is a tie, choose arbitrarily any of them.) The input to the method should be a String, specifying one of the three ranking factors (Academics, Research, Publications), or the overall rating (Overall), to be used as the criterion for ranking.
    The findTopSchool() method stores each school's rating value for the desired factor into the school's rank instance variable. Then, uses the rank value to find the 'School' object in the array with the highest rank value. At the end, the method prints the names of the top-ranked school and the factor it is based on.

7- Finally, add a main() method with code to test the methods defined in the GradSchools class. Add test cases to thoroughly test your code, including calls to the method findTopSchool().

Use command-line arguments to input the 3 weights and enforce them to be in the range 1..5. If you do not put 3 values in the command line, you get a response "Please provide 3 weights (1..5) for Academics, Research and Publications." In you do, here is an example of what you may get (for a particular selection of schools)

Sample run from the command-line

TERMINAL_PROMPT$ java GradSchools 1 1 1
There are 4 schools in the database:
SCHOOL: AcademicSch
Academics: 10 Research: 5 Publications: 3 Overall: 18
Current rank: 0
SCHOOL: ResearchSch
Academics: 3 Research: 10 Publications: 5 Overall: 18
Current rank: 0
SCHOOL: PubsFactory
Academics: 5 Research: 3 Publications: 10 Overall: 18
Current rank: 0
SCHOOL: BalancedSch
Academics: 7 Research: 7 Publications: 7 Overall: 21
Current rank: 0

Finding the top school in each category, with weights: Acad. 1 Res. 1 Pubs. 1
Based on Academics: SCHOOL: AcademicSch
Academics: 10 Research: 5 Publications: 3 Overall: 18
Current rank: 10

Based on Research: SCHOOL: ResearchSch
Academics: 3 Research: 10 Publications: 5 Overall: 18
Current rank: 10

Based on Publications: SCHOOL: PubsFactory
Academics: 5 Research: 3 Publications: 10 Overall: 18
Current rank: 10

Based on Overall: SCHOOL: BalancedSch
Academics: 7 Research: 7 Publications: 7 Overall: 21
Current rank: 21

TERMINAL_PROMPT$ java GradSchools
Please provide 3 weights (1..5) for Academics, Research and Publications