CS230 Data Structures Problem Set 1, Assignments of Data Structures and Algorithms

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

Pre 2010

Uploaded on 08/17/2009

koofers-user-m7i-1
koofers-user-m7i-1 🇺🇸

9 documents

1 / 7

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CS230 Data Structures Handout # 4
Prof. Lyn Turbak Friday, September 6, 2002
Wellesley College
Problem Set 1
Due: Thursday, September 12
Submission:
Problem 1 is a pencil-and-paper problem that only needs to appear in your hardcopy submis-
sion.
Your hardcopy submission for Problem 2 should be (1) the file Mortgage.java and (2) a
printout of the output requested in the problem.
Your hardcopy submission for Problem 2 should be (1) the file FindPrincipal.java and (2)
a printout of the output requested in the problem.
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:
1. Draw an object diagram that depicts the state of the program immediately after the statement
boolean [ ] bools1 = comparisons(pairss);
is executed.
2. Draw an object diagram that depicts the state of the program immediately after the statement
boolean [ ] bools2 = comparisons(pairss);
is executed.
Recall that an object diagram shows the state of the objects in Ob jectLand. 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:
1
pf3
pf4
pf5

Partial preview of the text

Download CS230 Data Structures Problem Set 1 and more Assignments Data Structures and Algorithms in PDF only on Docsity!

CS230 Data Structures Handout # 4 Prof. Lyn Turbak Friday, September 6, 2002 Wellesley College

Problem Set 1

Due: Thursday, September 12

Submission:

  • Problem 1 is a pencil-and-paper problem that only needs to appear in your hardcopy submis- sion.
  • Your hardcopy submission for Problem 2 should be (1) the file Mortgage.java and (2) a printout of the output requested in the problem.
  • Your hardcopy submission for Problem 2 should be (1) the file FindPrincipal.java and (2) a printout of the output requested in the problem.

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:

  1. Draw an object diagram that depicts the state of the program immediately after the statement

boolean [ ] bools1 = comparisons(pairss);

is executed.

  1. Draw an object diagram that depicts the state of the program immediately after the statement

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.

  • A class method named print that takes the same parameters as mortgage, but prints out the parameter information, monthly mortgage payment, and 30-year total mortgage payment in the following format: principal = 500000.0; interest = 7.25; years = 30.0; mortgage = 3410.881400280954; total = 1227917.
  • A class method main that for a principal of $500,000 and 30 year mortgage, prints out one line in the above format for every interest rate between 5% and 10% in increments of 0.25. The main method should take a single argument of type String [] (which is ignored in this and many other main methods).

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:

  • Your Mortgage class should contain only class methods. It should not contain class variables, constructor methods, instance variables, or instance methods.
  • To print out Mortgage.java, do one of the following:
    • Go to an Emacs editor buffer displaying Mortgage.java and select one of the menu options File>Print Buffer or File>Postscript Print Buffer.
    • Go to a shell, cd to the Mortgage directory, and execute the command lpr Mortgage.java.
  • To print out output of executing the main method:
    • If you have executed the main method within a shell running within Emacs, you simply select one of the menu options File>Print Buffer or File>Postscript Print Buffer for the shell buffer.
    • If you are in a shell outside of Emacs, then you first need to save the output of the command java Mortgage to a file. You can do this using Unix file redirection as follows:

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.

CS230 Problem Set 1

Due Thursday September 12

Name:

Date & Time Submitted:

Collaborators (anyone you worked with on the problem set):

By signing below, I attest that I have followed the collaboration policy as

specified in the Course Information handout.

Signature:

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.

Part Time Score

General Reading

Problem 1 [35]

Problem 2 [35]

Problem 3 [30]

Total