copyright 2012 Felleisen, Proulx, et. al.

CS 2510 Spring 2012: Assignment 3 - Designing Methods

Practice Problems

Work out as the following exercises from the textbook. Do not add them to your assignment submission, though you should keep them in your electronic portfolio in your own svn repository.

Problems:

  1. Problem 10.2 on page 97
  2. Problem 10.3 on page 97
  3. Problem 10.4 on page 97
  4. Problem 10.5 on page 105
  5. Problem 11.2 on page 116
  6. Problem 12.1 on page 129
  7. Problem 12.4 on page 131

Pair Programming Assignment

These problems should be checked into your pair's SVN repository by the due date.

Project naming requirements

The names of the projects and some of the project files must be exactly the same as specified in the assignment. Failure to do so makes it impossible for the graders to run your submission and results in immediate loss of at least 50% of the homework credit.

Every file you submit must have a header that identifies you and the problem it solves. So, for the first problem the header should look like this:

// Assignment 2 Problem 1
// Partner Name1
// partner1username
// Partner Name2
// partner2username
// 24 January 2012

Problem 1

Create a project with the name HW03Problem1. Your java file must be named CitiesMethods.java and the Examples class inside this file must be named ExamplesCities.

For this program use the classes you have defined in the previous assignment that represent the US cities and states.

  1. Design the method the method sameState that determines whether a city is in the given state.

  2. Design the method isSouthOfHuh that determines whether one city is located South of another city.

  3. Design the method distanceTo that computes the distance from one city to another. Use the same formula you have used in the first assignment to compute the distance.

  4. Design the method toPosn that produces a Posn that corresponds to the location of this city in a Canvas of the given width and height.

    The given Posn class allows you to use double type of data for the coordinates.

  5. Design the method totalDistance that computes the distance we have to travel from the first city in the route to the last one, going through all cities on the route in the given order.

  6. Design the method addCity that produces a modified route that adds a visit to the given city after the specified city on the route. If the specified city is not on our route, just add the given city at the end of the route.

    For example, if our trip goes from Boston to Concord to Hartford, and I ask to add Montpelier after Concord, then the new route will go from Boston to Concord to Montpelier to Hartford. However, if for the same route I ask to add Montpelier after Portsmouth, the new route will go from Boston to Concord to Hartford, and then to Montpelier.

    Note: When checking whether two cities are the same, it is sufficient to compare the names, the states, and the zip codes of the two cities.

  7. Design the method hasLoop that determines whether when traveling along the route we will return to one of the cities more than once (including at the end of the route).

Note: Make sure you follow the design recipe including showing all the templates in your code.


Problem 2

The goal of this problem is to see how Java dispatches the method invocation to the class to which the object belongs.

Create a project with the name HW02Problem2. Import into it the file ExcelCells.java. You will add your solutions to this file and submit this file for this problem.

For this problem we use classes that represent data in the cells of a spreadsheet. For each cell we record its row and column, where the cell is located, and the data IData stored. An IData is either a number int, a reference to another cell, or an IFormula. Each formula can be one of three possible functions: + (representing addition), min (producing the minimum of the two cells), or * (computing the product) and involves two other cells in the computation.

  1. Make an example of the following spreadsheet segment:

       |      A     |     B    |   C     |   D   |      E     |
     --+------------+----------+---------+-------+------------+
     1 |      8     |     3    |   4     |   6   |      2     |
     --+------------+----------+---------+-------+------------+
     2 | min(A1,E1) | +(B1,C1) | ref(A3) |       |  *(B2,D1)  |
     --+------------+----------+---------+-------+------------+
     3 |  *(A1,A2)  | +(B2,B1) |         |       | min(C2,D1) |
     --+------------+----------+---------+-------+------------+
     4 |            | +(B3,B2) |         |       | min(B4,D1) |
     --+------------+----------+---------+-------+------------+
     5 |            | +(B4,B3) |         |       |  *(B5,E4)  |
     --+------------+----------+---------+-------+------------+
    
  2. Draw this spreadsheet on paper and fill in the values that should show in each cell. Copy the data above and replace the contents of every cell with its value. Include it as a block comment in your ExamplesExcelCells class.

  3. Add a new function AbsDiff to the options for the spreadsheet formulas that computes the absolute value of the difference between the two given operands.

    Make sure you add the appropriate examples for this new type of data.

  4. Design the method eval that computes the value of a cell. Hint: follow the recipe... examples really help.

  5. Design the method countFuns that computes the number of function applications (Formulas) involved in computing the value of a cell.

  6. Design the method countPlus that computes the number of Plus applications needed to compute the value of a cell.


Last modified: Mon Jan 23 19:48:59 EST 2012