



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Instructions and problem sets for cs230 data structures class at wellesley college. It includes two programming problems: one on object diagrams and the other on mortgage calculations using java. The first problem requires students to draw object diagrams based on the given code, while the second problem asks students to write java code to calculate mortgage payments and find the corresponding principal. The document also provides instructions on how to submit the assignments.
Typology: Assignments
1 / 7
This page cannot be seen from the preview
Don't miss anything!




CS230 Data Structures Handout # 4 Prof. Lyn Turbak Friday, September 6, 2002 Wellesley College
Submission:
Remember to include a signed cover sheet (found at the end of this problem set description) at the beginning of your hardcopy submission. Your softcopy submission should be your entire Mortgage directory.
Problem 1 [35]: Object Diagrams In this problem, you will draw some object diagrams to illustrate your understanding of the Java Object Model and the important notion of sharing. Study the code for the Pair class in Fig. 1. You are asked to draw object diagrams that are “snapshots” of the state of the Pair application at two “snapshots” during its execution:
boolean [ ] bools1 = comparisons(pairss);
is executed.
boolean [ ] bools2 = comparisons(pairss);
is executed.
Recall that an object diagram shows the state of the objects in ObjectLand. There are two kinds of objects: (1) class instances, which are drawn as boxes labelled by their class name that contain instance variables labelled by their names and (2) array objects, which are drawn as boxes labelled by their array type that contain variables labelled by their index. Variables themselves are drawn as boxes that contain either primitive data (e.g., numbers, booleans, characters) or references (pointers) to other objects. Sharing is indicated by having two pointers that point to the same object. Your object diagrams should not show execution frames, but they should include the following local variables of the test method: pairss, a, bools1, and bools2. These variables may be shown “floating” in the object diagrams. The diagrams should also show all objects that are reachable from these four variables. All such objects (both instances and arrays) should be labelled with their type, and should be drawn as boxes that contain labelled variables.
Notes:
public class Pair {
// Instance variables: public int left; public int right;
// Constructor method: public Pair (int l, int r) { left = l; right = r; } // Instance method: public boolean equals (Pair p) { return (left == p.left) && (right == p.right); }
// Class methods: public static boolean [ ] comparisons (Pair [ ] [ ] pairArrays) { boolean [] bools = new boolean [8]; for (int i = 0; i < 4; i++) { bools[i] = (pairArrays[i][0] == pairArrays[i][1]); } for (int j = 0; j < 4; j++) { bools[j+4] = (pairArrays[j][0].equals(pairArrays[j][1])); } return bools; }
public static void test() { // Note: new Pair [4] [ ] creates a 4-slot array // whose slots hold objects of type Pair [ ]. Pair [ ] [ ] pairss = new Pair [4] [ ]; Pair [ ] a = new Pair [3]; a[0] = new Pair (1,2); a[1] = a[0]; a[2] = new Pair (3,4); pairss[0] = a; pairss[1] = a; pairss[2] = new Pair[3]; pairss[2][0] = new Pair(1,2); pairss[2][1] = pairss[0][1]; pairss[2][2] = pairss[1][2]; pairss[3] = pairss[2]; boolean [ ] bools1 = comparisons(pairss); // *** Object Diagram #1 should depict this point *** pairss[1][1] = a[2]; pairss[3][0] = pairss[3][1]; boolean [ ] bools2 = comparisons(pairss); // *** Object Diagram #2 should depict this point *** }
public static void main (String [ ] args) { test(); }
}
Figure 1: A sample Java class.
For this problem, your hardcopy submission should contain a code listing for the Mortgage class and a printout of the output of executing the main method of this class. Your softcopy submission for this problem (and Problem 3 as well) should be your Mortgage directory.
Notes:
java Mortgage > out.txt
The above command sends the printed output of java Morgage to the file out.txt rather than to the console. Once the data is in a file, use lpr out.txt to print it.
Problem 3 [30]: It’s the Principal of the Thing The mortgage method in Problem 2 tells you the monthly mortgage given the principal, interest rate, and the number of years. But suppose instead that you have determined the monthly mortgage M that best fits your budget and want to determine the principal P for the most expensive house you can afford for that mortgage (for a given interest rate and number of years). One approach is to algebraically manipulate the formula from Problem 1 to express P as a function of M , interest, and the number of years. But you are rusty on your algebra, so you decide not to take that tack. An alternative approach is to use the mortgage method from Problem 1 as a way of evaluating a sequence of guesses at the desired principal P. The principal P you seek is the one for which
mortgage returns M. You can use the binary search idea from the HiLo game as a way of effectively guessing P. In particular, suppose you know that P is in the closed interval [lo, hi]. Then because mortgage is a monotonically increasing function, you can use the result of applying mortgage to the midpoint of lo and hi to narrow P to be in one half of this interval. By successively halving the interval, you can quickly converge to a result P ′^ whose mortgage M ′^ is within one penny of M. At that point, you can declare P ′^ as the desired principal.
public class FindPrincipal {
public static double find (double mortgage, double interest, double years) { return findBetween(mortgage, interest, years, 0, upperBound(mortgage, interest, years)); }
public static double findBetween (double mortgage, double interest, double years, double lo, double hi) { // Flesh this out. }
public static double upperBound (double mortgage, double interest, double years) { // Flesh this out. }
public static void testFind (double m, double i, double y) { System.out.println("find(" + m + ", " + i + ", " + y + ") = " + find(m,i,y)); }
public static void main (String [] args) { // Flesh this out. } }
}
Figure 2: A sample Java class.
Implement this idea by fleshing out the missing parts of the FindPrincipal class in Fig. 2, which you should implement in a file FindPrincipal.java within your Mortgage folder. The find method defers to findBetween to find a principal in the interval [0, u], where u is an upper bound on the desired principal P – i.e., u is guaranteed to be greater than or equal to P. The find method should use binary search to converge to a principal P whose mortage is within one dollar of the mortgage parameter to findBetween. (Use Math.abs to calculate absolute values.) The upperBound method determines an upper bound u for P. It should implement the following strategy: starting with the principal 1, successively double it until reaching a principal Q whose mortgage is greater than the given mortgage limit. Q is clearly an upper bound for P. The main method should test your code by determining the maximum house price that can be purchased
Problem Set Header Page Please make this the first page of your hardcopy submission.
In the Time column, please estimate the time you spend on the parts of this problem set. Please try to be as accurate as possible; this information will help me design future problem sets. I will fill out the Score column when grading your problem set.