Assignment 1, CSC102, Spring 2012
1 Guidelines
1.1 Contracts & Test Cases
For this and all remaining assignments, every function you develop must come with the following things:
A commented header line that expresses the result of the function in terms of its inputs, written in English. Be as precise as you can within the space of a line or two.
Test cases. A function without test cases is incomplete. Write the test cases first, please. In this assignment, one good test case should suffice for each problem.
1.2 Naming Conventions and argument order
For this and all remaining assignments, method and class names must be exactly as they appear in the assignment. Furthermore, arguments to methods must be in the same order that they appear in the English-language specification. This is for the simple reason that the automated test cases won’t work otherwise.
2 The Assignment
For this assignment, all of your functions and test cases should be in a single class, called Assignment1. This assignment should be in the package ’assignment1’. Also, all of your methods must be static.
Develop the static method totalReturn, that computes the return of an investment compounded over a number of years. It consumes a double representing the annual yield, in percentage points, and an int representing the number of years invested. It returns a double representing the multiple applied to an initial investment. So, if the yield is 4% and the term is 1 year, the multiplier should be 1.04. (Hint: over two years, the multiplier should not be 1.08. But you knew that already, right?)
Develop the static method convert3. It consumes an integer for the hundreds place, an integer for the tens place, and an integer for the ones place, and returns the integer formed by assembling these. You may assume that your inputs are in the range [0,9].
So, for instance, calling this method with the numbers 1, 0, and 0 would produce the number 100. Note that this is a bad test case (why?). Your test cases should be better than this.
Develop the static method generateID, that consumes a string representing an employee’s first name and a string representing an employee’s last name, and produces a string of the form "Last, First". Note that you can use the + operator to append strings.
Develop the static rectanglePack method, that estimates how many rectangles of a given length and width will fit into a larger rectangle of a given length and width. You’ll need the Math.max and Math.floor functions. You don’t need to do the best possible job of packing; Just assume a simple single-orientation packing strategy.
Develop the static rectPackEfficiency method, that returns the efficiency of the packing strategy of the prior function. It accepts the length and width of the small rectangles and the length and width of the larger rectangle into which they’re being packed, and returns a number between 0 and 1 indicating what fraction of the space is consumed by the packing strategy given by the rectanglePack method. Don’t re-implement earlier functionality! Re-use the earlier method! (Hint: if your function always returns 1.0, you might want to think harder about the last problem.)