CS100M Spring 2001 Review Questions R4 for the Final (test T4) =============================================================================== R4.1 =============================================================================== A *closed* *interval* is a contiguous set of the real numbers: [a..b] = { x | a <= x <= b } Special cases are the empty set (e.g. $[2..1]$) and a single point (e.g. $[1..1]$). Suppose we want the following operations: ___ create an interval ___ compute the intersection of two intervals ___ test containment (whether the second interval is a subset of the first) ___ display/describe an interval Further suppose: ___ we do not want any other operations on intervals except for combinations of the operations above. Bonus: Implement general intervals, i.e. allow all four of these cases: [a..b] = { x | a <= x <= b } ]a..b[ = { x | a < x < b } ]a..b] = { x | a < x <= b } [a..b[ = { x | a <= x < b } a) What are some test cases that should be tried to see that the code works? b) ___ Write Matlab functions to implement the 4 operations on closed intervals and explain either ___ how you disallow other operations or ___ why you are unable to disallow other operations. ___ Also write a small script that uses all 4 operations. c) ___ Write a Java class to implement closed intervals with only the 4 operations listed above. ___ Include a main method that uses all 4 operations. =============================================================================== R4.2 =============================================================================== a) Fill in the blanks below to complete Java method $min$. All matrices are ___ non-empty, ___ rectangular, ___ row-major, and hold ___ integers. ___ Pay attention to comments! public class R4_2 { // minimum and its position (first occurrence) of each column of matrix $x$: // java $m=min(x);$ approximates matlab $[val,pos]=min(x); m=[val;pos];$ public static int[][] min( int[][] x ) { // inv: $m[0..1][0..col-1]$ holds minimums and positions // for $x[:][0..col-1]$ int col = _______________ ; ____________________ m = ______________________________________________ ; while ( ___________________________________________ ) { // update $m$ for current (unprocessed) column in $x$. // inv: $pos$ is the row of the minimum in rows $0..row-1$ of // current column int pos = _______________ ; int row = _______________ ; while ( _______________________ ) { if ( _________________________________________ ) // new minimum? pos = _________________ ; row += ______________ ; // advance to next row } _____________ = _______________________ ; // record minimum _____________ = _______________________ ; // record position col += __________________ ; // advance to next column } return _________________ ; } // here is the java equivalent (but output is not formatted) of the matlab // code $[val,pos] = min([27 -18 24 ; 26 4 17 ]); disp([val' pos'])$ public static void main(String[] args) { int[][] m=min(new int[][] { new int[] {27,-18,24}, new int[] {26,4,17} }); // print one column of $m$ per line of text // inv: already did columns $0:i-1$ int i = 0; while ( i < m[0].length ) { System.out.println( m[0][i] + " " m[1][i] ); i += 1; } } } b) Suppose Matlab did not have $min$ or $max$ or $sort$. Write Matlab function $min$: duplicate the functionality of the built-in $min$ for vectors and matrices. =============================================================================== R4.3 =============================================================================== Consider a class $Creature$ for creatures that have a $name$, can $speak$, and can $act$ and a $main$ method that creates four $Creature$s and has each $act$ five times: public class Creature { protected String name; // Creature's name // constructor: initialize name to n public Creature(String n) { name = n; } // speak, i.e. vocalize public void speak() { System.out.println(name + " speaks"); } // perform an action public void act() { speak(); } } // create 4 Creatures and repeatedly (5 times) have each act one at a time public class Target { public static void main(String args[]) { Creature[] c = { // 4 Creatures new Creature("Butch"), new Creature("Fluffy"), new Creature("Sleepy"), new Creature("Clarus") }; // repeat 5 times: each Creature acts for (int times=5; times>0; times--) for (int i = 0; i0$ is *perfect* if it is equal to the sum of all its proper divisors $d$ (proper means $0