============================================================ Question 1: Box-scope diagram //------------------------------------ class Frippy { int x; Frippy F; } class Boingo { String a; Frippy b; } public class Problem1 { public static void main(String[] args) { Frippy F1 = new Frippy(); Frippy F2 = new Frippy(); Boingo B1 = new Boingo(); Boingo B2 = new Boingo(); B2.a = "grok"; B1.b = new Frippy(); F2.F = B1.b; B1.b.F = F1; ++B1.b.F.x; Boingo B3; F2 = null; // ****draw the box/scope diagram up to this point*** } } class Problem1 +-------------------------------------------------------------------+ | +---+ +---+ +---+ +---+ +---+ +---+ | | args | | F1 | | F2 | | B1 | | B2 | | B3 | | | | +---+ +---+ +---+ +---+ +---+ +---+ | +-------------------------------------------------------------------+ class Frippy +--------------------------------------------------------------------------+ | | | +----------+ +----------+ +----------+ +----------+ +----------+ | | | +----+ | | +----+ | | +----+ | | +----+ | | +----+ | | | | x | | | | x | | | | x | | | | x | | | | x | | | | | | +----+ | | +----+ | | +----+ | | +----+ | | +----+ | | | | | | | | | | | | | | | | +----+ | | +----+ | | +----+ | | +----+ | | +----+ | | | | F | | | | F | | | | F | | | | F | | | | F | | | | | | +----+ | | +----+ | | +----+ | | +----+ | | +----+ | | | +----------+ +----------+ +----------+ +----------+ +----------+ | | | +--------------------------------------------------------------------------+ class Boingo +--------------------------------------------------------------------------+ | | | +----------+ +----------+ +----------+ +----------+ +----------+ | | | +----+ | | +----+ | | +----+ | | +----+ | | +----+ | | | | a | | | | a | | | | a | | | | a | | | | a | | | | | | +----+ | | +----+ | | +----+ | | +----+ | | +----+ | | | | | | | | | | | | | | | | +----+ | | +----+ | | +----+ | | +----+ | | +----+ | | | | b | | | | b | | | | b | | | | b | | | | b | | | | | | +----+ | | +----+ | | +----+ | | +----+ | | +----+ | | | +----------+ +----------+ +----------+ +----------+ +----------+ | | | +--------------------------------------------------------------------------+ class String +----------------------+ | | | | | | +----------------------+ ============================================================ Question 2: constructors, passing parameters Complete class $FatInteger$ below by writing the requested constructors. Predict the output given class $Swap$ below. (i.e., you need to determine which of the 4 swap methods work.) //------------------------------------ // Class $FatInteger$ is a "wrapper class". Its purpose is to allow us to // treat an $int$ as an object by simply storing the $int$ in an object. // It provides constructors to create a $FatInteger$ from no argument, // an integer, or another $FatInteger$. It also allows us to set the value // of a $FatInteger$ from another $FatInteger$, and provides a $toString()$ // method. class FatInteger{ private int n; // the interger inside the object // Constructor for default case ($n$=0), no argument FatInteger(){ } // Constructor: assign to $n$ the value of an integer argument FatInteger( ){ } // Constructor: assign to $n$ the value stored in the $FatInteger$ argument FatInteger( ){ } // Set the value of a FatInteger based on another FatInteger void setValue(FatInteger other){ this.n = other.n; } // The toString() method for printing public String toString(){ return "" + n; } } class Swap{ public static void main(String arg[]){ // Call various methods to attempt to swap the variables that are // passed to the methods. You determine which of them succeed. // Predict the output from each of the four print statements. int x = 5, y = 7; swap1(x,y); System.out.println("After swap1, x = " + x + ", y = " + y); FatInteger p = new FatInteger(6); FatInteger q = new FatInteger(8); swap2(p,q); System.out.println("After swap2, values in p, q are " + p + ", " + q); FatInteger r = new FatInteger(15); FatInteger s = new FatInteger(17); swap3(p,q); System.out.println("After swap3, values in r, s are " + r + ", " + s); FatInteger t = new FatInteger(16); FatInteger u = new FatInteger(18); swap4(t,u); System.out.println("After swap3, values in t, u are " + t + ", " + u); } static void swap1(int a, int b){ int temp; temp = a; a = b; b = temp; } static void swap2(FatInteger a, FatInteger b){ FatInteger temp; temp = a; a = b; b = temp; } static void swap3(FatInteger a, FatInteger b){ FatInteger temp; temp = new FatInteger(a); a = new FatInteger(b); b = new FatInteger(temp); } static void swap4(FatInteger a, FatInteger b){ FatInteger temp = new FatInteger(); temp.setValue(a); a.setValue(b); b.setValue(temp); } } ============================================================ Question 3: Returning multiple values in methods via objects Write the code for method $calcStats$ to calculate some statistics given 3 numbers. Then fill in the missing code in method $main$ where you call the $calcStats$ method. If necessary, check out the example in the book on page 227 (in the grey box) for additional help. //------------------------------------ // class StatValues{ // instance variables double average; double minimum; double maximum; // constructor that does nothing public StatValues(){} } class Statistics{ // main method public static void main(String[] args){ // assign 3 double variables num1=12.32; num2=66.01; num3=32.39; // call method $calcStats$ to get the average, minimum, // and maximum for the 3 numbers above _____________________________________________; // Output System.out.println("My avg: "+mystat.average); System.out.println("My min: "+mystat.minimum); System.out.println("My max: "+mystat.maximum); } // Determine the average, minimum, and maximum of exactly 3 $double$ // numbers. Return a reference to an object of class $Statvalues$ . // (Once you know how arrays work you can easily change the code // to determine these statistics for any number of numbers.) public void calcStats(double a,double b,double c){ StatValues result; // reference variable for results } } // What are the necessary changes if you make method $calcStats$ $private$? ============================================================ Question 4: for-loop, encapsulation Part a: Complete class $PersonWOutEncaps$ by writing method $addstrings$ //------------------------------------ class PersonWOutEncaps { // instance variables public String firstname; public String lastname; // constructor Person() { }; // Add blank spaces between 2 Strings. The number of spaces is passed as // an Integer. E.g., if you pass // 3,"Name1","Name2" // to the method it would return the String "Name1 Name2". public String addstrings(int spaces, String start, String stop) { } // method addstrings } // class PersonWOutEncaps Part b: Given the complete class $PersonWOutEncaps$ and the following class $WOutEncaps$, make all changes necessary to implement encapsulation in both classes and call the new classes $PersonWithEncaps$ and $WithEncaps$. //------------------------------------ public class WOutEncaps { public static void main(String args[]) { // instantiate nic using constructor Person() Person nic = new Person(); // assign values to instance variables of nic nic.firstname="Nici"; nic.lastname ="Waldhausen"; // Retrieve individual values from nic System.out.println(nic.firstname); System.out.println(nic.lastname); // Determine fullname String f=nic.firstname; String l=nic.lastname; String fullname = addstring(2,f,l); System.out.println(fullname); } // method main } // class WOutEncaps ============================================================ Question 5: instance methods, test equality Part a: Complete the class $Person$ with instance variables $name$, $age$, and $friend$ a constructor to initialize the $name$ and $age$ and instance methods $describe$ Print the Person's name, age, and one of the following: "self sufficient" if the Person's friend is himself/herself; "no friend" if the Person does not have a friend; or the name of the Person's friend. $makefriend$ Get another Person, passed in the argument, as a friend. $befriend$ Become the friend of another Person passed in the argument //------------------------------------ class Person{ String name; int age; Person friend; // Constructor to initialize $name$ and $age$ based on passed arguments // Print description of a Person void describe(){ } // Make a friend void makefriend( ){ } // Become another Person's friend void befriend( ){ } } // class Person Part b: Write a method $main$ in class $Xfiles$ using the methods in class $Person$ to create the people and relationships below and print a description of each $Person$. Scully, 31, has friend Mulder Mulder, 31, has friend Scully Skinner, 49, is his own best friend CSM, 103, has no friend ============================================================ Question 6: variable/return types, while-loop, this, comparing references An exercise path is composed of stations forming a directed loop: --> S --> S --- | | S denotes station | | ---- S <-- S <- Each station has a name and an integer level of difficulty ranging from 1 to 4, inclusive. A person can start at any station and can visit any station at most once. At any station, (do the exercise and) roll a 4-sided dice. If the dice comes up equal to the degree of difficulty of the current station, stop. Otherwise go to the next station, (exercise and) repeat decision process. Complete class $Station$ below. Objects of class $Station$ have the fields: $name$, $difficulty$, $nextstop$ and the following methods: go_on() Decide if condition is met for going to next $Station$ (return $boolean$ value) itinerary() Print the names of the start and end stations Must call method $go_on()$ Method $main$ below is complete and serves as an example of the usage of class $Station$. This example instantiates 3 $Station$s, forms a directed loop, and calls method $itinerary()$ of the chosen starting $Station$. //------------------------------------ class Station { String name; // station name int difficulty; // degree of difficulty [1..4] Station nextstop; // next station // Choose to go on unless 4-sided dice comes up equal to current // degree of difficulty. boolean go_on() { // Hint: $(int) (Math.random()*4)+1)$ returns 1, 2, 3, or 4 } // Print names of start station and last station at which exercise is done void itinerary() { Station current=this; // current station, do exercise } } //class Station public class ExercisePath { public static void main(String[] args) { // Set up exercise path Station sp = new Station(); Station ss = new Station(); Station sj = new Station(); sp.name = "Push-ups"; sp.difficulty = 3; sp.nextstop = ss; ss.name = "Sit-ups"; ss.difficulty = 2; ss.nextstop = sj; sj.name = "Jumping jacks"; sj.difficulty = 1; sj.nextstop = sp; // Start at Station ss this time and print exercise itinerary ss.itinerary(); } } //class ExercisePath