CMPS 12a Final Exam Review Questions 1) Declare and allocate an array of 3 characters and initialize so it contains 'c' 'a' 't' (in that order). After the following, how many 7's are stored in the heap? int[] foo = {6,7,8,9}; int[] bang = new int[foo.length]; int[] bar; bar = foo; for (int i = 0; i < foo.length; i++) { bang[i] = foo[i]; } 2) Assume we have the declaration: int[] foo = {6,7,8,9}; What is foo.length? How can foo.length change? 3) Let NUMGIRLSCOUTS be an integer constant greater than 2 and we have the declararion int[] boxesSold = new int[NUMGIRLSCOUTS]; Assume the array boxesSold is initialized to hold the number of Girl Scout cookie boxes sold by each Girl Scout (where Girl Scouts are numbered from 0 to NUMGIRLSCOUTS-1. Write a code fragments that prints: a) the total number of boxes sold b) the largest number of boxes sold by any Girl Scout c) the number of the Girl Scout selling the fewest boxes d) the numbers of all Girl Scouts selling at least 10 boxes Now assume that there is a second array: int[] boxesBefore = new int[NUMGIRLSCOUTS]; that is initialized to the number of boxes sold by each Girl Scout last year. Write a code fragement that prints: e) the numbers of all the Girl Scouts who sold at least twice as many boxes this year as last year. 4) Write a static method that takes in an integer array and adds one to each of the elements in the array. 5) What is output by the following code fragment: int[] foo = {6,7,8,9}; int iVal = 3; munge(foo, iVal); System.out.println("iVal is:" + iVal); System.out.println("foo is: " + foo[0] + foo[1] + foo[2] + foo[3]); ... static void munge(int[] array, int val) { for (int i = 1; i < array.length; i++) { foo[i] = foo[i] + val; } val = val + val; } 6) Show the contents of the array foo after the following code fragment is executed: int[][] foo = new int[3][4]; for (int row = 0; row < 3; row++) { for (int col = 0; col < 4; col++) { foo[row][col] = row + col; } } 7) Assume we have a two-dimensional array declared as: int [][] counts = new int[NUMROWS][NUMCOLS]; that has been initialized to some counts. a) Write a code fragment that, for each row, prints the total of the counts on that row. b) Write a code fragment that prints the smallest value in the array together with its row and column indices. 8) Assume that you have a Robot class with the instance methods boolean isAlive(){...} and void moveToward(int row, int col){...}. a) Write a code fragment that defines bots to be an array of 10 new Robot objects. b) Write a code fragment that moves all of the alive robots in the array towards row 7 column 7. 9) Consider the following class Change: class Change { private int pennies = 0; private int nickles = 0; private int dimes = 0; private int quarters = 0; int numQuarters() { return (quarters); } int value() { return (1*pennies + 5*nickles + 10*dimes + 25*quarters); } } a) Write an instance method addQuarter() returning void that adds a quarter to the change object. b) Write a static method makeCopy(Change coins) that creates and returns a new change object whose values are a copy of the passed object c) Assume that in the main program we have Change inPockets = new Change(); Change inHand; and that inPockets has been initialized to some collection of change. Use the methods from a) and b) above in a code fragment that sets inHand to be the collection of change inPockets plus three quarters without modifying the object inPockets. d) What is printed by the following code fragment: Change myCoins = new Change(); myCoins.addQuarter; addQ (myCoins); addOne(myCoins.numQuarters); addOne(myCoins.numQuarters); System.out.println(myCoins.numQuarters); ... static void addQ(Change coins) { coins.addQuarter(); } static void addOne(int num) { num = num + 1; } 10) Write a constructor for the Change classs that takes 4 integers for the initial number of pennies, nickles, dimes, and quarters. 11) What is the difference between a private instance variable and a public instance variable? 12) Consier the following class: class Foo { int a = 0; static int b = 0; addBoth() { a = a + 1; b = b + 1; } } What is printed by the following code fragment: foo x = new foo(); foo y = new foo(); x.addBoth(); x.addBoth(); y.addBoth(); System.out.println("the answer is: " + x.a + " " + y.a + " " Foo.b); 13) Write a class Vector to represents 2-dimensional vectors in the (x,y) plane. The class should contain private double instance variables xCoord and yCoord, and the member functions length returning the length of the vector, and setx and sety which set the instance variables. You can use the function Math.Sqrt(), and recall that the length of a vecor is the square-root of (x^2 + y^2).