This pledged exam is closed textbook, open class web site, and two pieces of paper with notes. You may not access any other code or websites including your own. Write your email id and name on every page of the test. The only window to be open on your computer is a single browser. Whenever you are writing a message method as your answer to an exercise, there is a grading preference for the use of the this variable and inspectors and mutators when appropriate. Pledge: Warm up 1. Write a complete program Shoe.java that prompts its user for the length of the user s foot in centimeters. The program reports the person s metric shoe size. Metric shoe size can be approximated as follows: 3 length + 23 size = 2, are the mathematical symbols for casting a decimal value to an integer. The following where demonstrates a sample run. Enter foot length (centimeters): 29.5 Metric shoe size: 39 import java.util.*; public class Shoe { public static void main( String[] args ) { Page 1 of 14
2. Suppose x and y are initialized object variables of the same type (e.g., they are both Color variables). Complete the initialization of boolean variable b so that it indicates whether x and y reference the same object. boolean b = ; 3. Suppose x and y are initialized object variables of the same type (e.g., they are both Color variables). Complete the initialization of boolean variable b so that it indicates whether x and y reference objects with like attributes. boolean b = ; Adding functionality to existing classes 4. Define a void message method absolute() that is to be part of the class Calculator. The method updates the total of its calculator to its absolute value. The following code segment demonstrates its usage. Calculator c = new Calculator(); c.add( -7 ); // running total is now -7 c.absolute(); // running total is now 7 public void absolute() { 5. Define a void message method square() that is to be part of the class Calculator. The method updates the total of its calculator to be the square of its current value. The following code segment demonstrates its usage. Calculator c = new Calculator(); c.add( 7 ); // running total is now 7 c.square(); // running total is now 49 public void square() { Page 2 of 14
6. Define a Rational message method invert() that is to be part of the class Rational. The method returns a new Rational that is a flipping of its rational; i.e., its numerator becomes the new Rational s denominator and its denominator becomes the new Rational s numerator. The following code segment demonstrates its usage. Rational a = new Rational( 3, 5 ); // a is 3 / 5 Rational b = a.invert(); // b is 5 / 3 public Rational invert() { 7. Define a void message method reduce() that is to be part of the class Rational. The method takes one int parameter v. If v is a factor of both its numerator and the denominator, then the method divides both of them by v. If instead v is not a common factor, then no changes are made to its numerator and denominator. The following code segment demonstrates its usage. Rational a = new Rational( 6, 9 ); // a is 6 / 9 a.reduce( 7 ); // a is 6 / 9 as 7 is not a common factor a.reduce( 3 ); // a is 2 / 3 as 3 is a common factor public void reduce( int v ) { Page 3 of 14
8. Define a void message method swap() that is to be part of the class Rational. The method takes one Rational parameter r. The method swaps the numerator and denominator of its rational with Rational r. The following code segment demonstrates its usage. Rational a = new Rational( 6, 9 ); // a is 6 / 9 Rational b = new Rational( 3, 4 ); // b is 3 / 4 a.swap( b ); // a is 3 / 4 and b is 6 / 9 public void swap( Rational r ) { 9. Define a boolean message method isolderthan() that is to be part of the class Song. The method takes one Song parameter s and returns whether its song has an earlier recording year than Song s. The following code segment demonstrates its usage. Song s1 = new Song( n1, p1, y1 ); Song s2 = new Song( n2, p2, y2 ); boolean b = s1.isolder( s2 ); // is s1 recorded earlier than s2 public boolean isolderthan( Song s ) { Page 4 of 14
10. Define a boolean message method atmorethan() that is to be part of the class Movie. The method takes one Movie parameter m and returns whether its movie is playing at more theaters than Movie m. The following code segment demonstrates its usage. Movie m1 = new Movie( "Water for Elephants" ); Movie m2 = new Movie( "Thor" );... boolean b = m1.atmorethan( m2 ); // is m1 at more theaters than m2 public boolean atmorethan( Movie m ) { Library methods 11. Write an int library method factorial() that takes one int parameter n. The loop- based method returns n!; i.e., 1 * 2 * 3 * * n. The following code segment demonstrates its usage. int v = factorial( 5 ); // v is 120; i.e., 1 * 2 * 3 * 4 * 5 public static int factorial( int n ) { Page 5 of 14
12. Write an int library method choose() that takes two int parameter n and k. The method returns n! / (k! * (n - k)! ) The following code segment demonstrates its usage. int a = choose( 2, 2 ); // is 1 int b = choose( 3, 2 ); // is 3 int c = choose( 4, 2 ); // is 6 public static int choose( int n, int k ) { 13. Write a recursive int library method gcd() that takes two int parameter x and y and returns their greatest common divisor. If y is zero, the greatest common divisor of x and y equals x. If y is not zero, the greatest common divisor of x and y equals the greatest common divisor of y and r, where r is the remainder of x divided by y. The following code segment demonstrates its usage. int n = gcd( 72, 54 ); // is 18 public static int gcd( int x, int y ) { Page 6 of 14
14. Write a recursive int library method choose() that takes two int parameter n and k and returns the number of ways subsets of size k can be made from a set of size n. If k is 0, then the number of ways of making the subsets is 1. Otherwise, if n is 0, then the number of ways of making the subsets is 0; Otherwise, the number of ways of making the subsets is the sum of two quantities: o o The number of ways subsets of size k 1 can be made from a set of size n 1; The number of ways subsets of size k can be made from a set of size n 1. The following code segment demonstrates its usage. int a = choose( 2, 2 ); // is 1 int b = choose( 3, 2 ); // is 3 int c = choose( 4, 2 ); // is 6 public static int choose( int n, int k ) { Library methods for array manipulation 15. Write an int library method product() that takes one int[] parameter x. The method returns the product of the element values. The following code segment demonstrates its usage. int[] a = { 3, 1, 4, 1, 5, 9 }; int p = product( a ); // p is 540; i.e., 3 * 1 * 4 * 1 * 5 * 9 public static int product( int[] x ) { Page 7 of 14
16. Write a boolean library method gotit() that takes a single int[] parameter x and one int parameter v. The method returns whether any of the elements in x are equal to v. The following code segment demonstrates its usage. int[] a = { 3, 1, 4, 1, 5, 9 }; boolean b1 = gotit( a, 4 ); // b1 is true, because x has a 4 boolean b2 = gotit( a, 8 ); // b2 is false, because x does not have a 8 public static boolean gotit( int[] x, int v ) { 17. Write a boolean library method isincreasing() that takes a single int[] parameter x. The method returns whether the element values for array x are in strictly increasing order. int[] a = { 3, 1, 4 }; int[] b = { 1, 3, 3, 4 } int[] c = { 1, 3, 4, 5, 9 }; boolean b1 = isincreasing( a ); // is false as 3 >= 1 boolean b2 = isincreasing( b ); // is false as 3 >= 3 boolean b3 = isincreasing( c ); // is true as 1 < 3 < 4 < 5 < 9 public static boolean isincreasing( int[] x ) { Page 8 of 14
18. Write a int[] library method glue() that takes a two int[] parameters x and y (the arrays can have different numbers of elements). The method returns a new int[] array whose length is the sum of the lengths of arrays x and y. The initial elements in the new array are to come from x and the remaining elements are to come from y. Suggestion: use two while loops. int[] a = { 3, 1, 4 }; int[] b = { 1, 5, 9, 2 }; int[] c = glue( a, b ); // c is 3, 1, 4, 1, 5, 9, 2 public static int[] glue( int[] x, int[] y ) { 19. Write an int library method count() that takes one int[][] parameter x and one int parameter v. The method returns the number of elements in the two- dimensional array that equal v. The following code segment demonstrates its usage. int[][] a = { { 3, 1, 4, 1 }, { 5, 9, 2, 6 } }; int n1 = count( x, 1 ); // n1 becomes 2 int n2 = count( x, 12 ); // n2 becomes 0 public static int count( int[][] x, int v ) { Page 9 of 14
Class Pixel implementation see handout for specification 22. public static boolean legallevel( int n ) { 23. public Pixel() { 24. public Pixel( int r, int g, int b ) { 25. public Pixel( int n ) { Page 10 of 14
26. public void setred( int r ) { 27. public void setgreen( int g ) { 28. public void setblue( int b ) { 29. public void setrgb( int r, int g, int b ) { 30. public int getred() { Page 11 of 14
31. public int getgreen() { 32. public int getblue() { 33. public Color tocolor() { 34. public int toint() { 35. public boolean samered( Pixel p ) { Page 12 of 14
36. public boolean samegreen( Pixel p ) { 37. public boolean sameblue( Pixel p ) { 38. public boolean same( Pixel p ) { 39. public Pixel clone() { Page 13 of 14
40. public String tostring() { 20. True or false True False I am part of the class picture. True False Since the last test I have attended every class or had an excused absence. True False I did the Collab survey for this class. True False I did the exit survey for this class. Page 1 / 12 Page 8 / 8 Page 2 / 16 Page 9 / 8 Page 3 / 8 Page 10 / 12 Page 4 / 8 Page 11 / 15 Page 5 / 8 Page 12 / 15 Page 6 / 8 Page 13 / 12 Page 7 / 8 Page 14 / 5 / 143 Page 14 of 14