Chapter 2: "Objects, Control Structures, Arrays" Data Structures with Java, Rick Mercer Answers to Self-Check Questions 2-1 finish the assertions BankAccount b1 = new BankAccount("Kim", 0.00); BankAccount b2 = new BankAccount("Chris", 500.00); assertEquals( "Kim" , b1.getID()); //filled in "Kim" assertEquals( "Chris" , b2.getID()); //filled in "Chris b1.deposit(222.22); b1.withdraw(20.00); assertEquals( 202.22 , b1.getBalance(), 0.001); //filled in 202.22 b2.deposit(55.55); b2.withdraw(10.00); assertEquals( 545.55 , b2.getBalance(), 0.001); //filled in 545.55 2-2 explain error -a- missing value as second parameter -b- missing " = new BankAccount" -c- "Account" should be "BankAccount" -d- there are no parameters -e- the parameter should be a double -f- "Deposit" is not a method (deposit is the method) -g- missing object reference -h- b4 has not been instantiated -i- there is no method called name parameter symbol for the "getBalance()" method -j- missing "()" after method call 2-3 value of positionOfG = 6 2-4 value of 3 = "ef" 2-5 value of len = 8 2-6 output = Wheatley, Kay 2-7 int midInt = str.length() / 2; String mid = "" + str.charAt(midInt); 2-8 a,b,c, and f have errors. d -> 3 e -> "y S" 2-9 -a- String -b- concat -c- 1 -d- String -e- none 2-10 return values or invalid -a- abcxyz -b- invalid - no paramter passed -c- invalid - parameter is not a String -d- invalid - two parameters passed when only one needed -e- abcwx yz -f- invalid - concat is not a static method 2-11 fill in blanks String s = "abc"; assertEquals( "abc!" , s.concat("!")); //added "abc!" assertEquals( "abccba" , s.concat("cba")); //added "abccba" assertEquals( "abc123" , s.concat("123")); //added "abc123" 2-12 isOdd method public boolean isOdd (int num){ return num % 2 != 0; //Java has does not handle negatives correctly with the mod operator. } 2-13 averageThree method public double averageThree(double one, double two, double three) { return (one+two+three)/3.0; } 2-14 firstLast method public String firstLast(String str){ String result = "" + str.charAt(0) + str.charAt(str.length()-1); return result; } 2-15 which assertion would pass? == assertion "a" 2-16 which would pass? == "b" and "c" 2-17 assertions for isLeapYear. Your answer may vary: Check 4 end of century's where 1 is and three are not. Check some that are not end of century that are evenly divisible by 4 with others that are not. assertTrue(isLeapYear(1600)); assertTrue(isLeapYear(2000)); assertTrue(isLeapYear(2008)); assertTrue(isLeapYear(2400)); assertFalse(isLeapYear(2100)); assertFalse(isLeapYear(2005)); assertFalse(isLeapYear(2006)); assertFalse(isLeapYear(2007)); 2-18 isLeapYear method. public boolean isLeapYear(int year) { if(year % 4 == 0)//general leap year case { if(year % 100 != 0 || year % 400 == 0)/*if the year is not divisible by 100 or is also disvisible by 400*/ return true; } return false;//everything else falls through to here } 2-19 assertions for largest method assertEquals(4, largest(1, 2, 4)); assertEquals(7, largest(7, 6, 5)); assertEquals(7, largest(5, 7, 5)); assertEquals(1, largest(-3, -2, 1)); assertEquals(0, largest(-2, 0, -1)); assertEquals(5, largest(5, 5, 5)); 2-20 largest method public int largest(int one, int two, int three) { return Math.max(Math.max(one, two), three); } 2-21 letterGrade method public String letterGrade(int grade) { if (grade < 0 || grade > 100) return "?"; else if (grade < 60) return "F"; else if (grade < 70) return "D"; else if (grade < 80) return "C"; else if (grade < 90) return "B"; else return "A"; } 2-22 write the output -a- 1 2 3 4 -b- 1 2 3 4 5 -c- -3 -1 1 3 -d- 5 4 3 2 1 -e- before after -f- an error (j not instantiated) 2-23 for loop 10 to 1 for (int i = 10; i >= 1; i--) { System.out.print(i + " "); } 2-24 for loop output -a- 2 4 6 8 10 -b- compiler error - need "int counter = 10;" if counter is initialized prior, then: 1 2 3 4 5 6 7 8 9 10 2-25 number of times "Hello" is printed -a- 20 times -b- zero -c- infinite -d- infinite trace through answers 2-26 == 29 (correct) 2-27 == 4 (correct) 2-28 == -999999994 (incorrect) 2-29 b-when the input is entered in ascending order Also, when a valid int excedes the range of -999999999 and 999999999 2-30 how to correct? Initialize hi and low to the 1st number in the list, and compare all other numbers to both ///////////////////////////////////////////////////////////////// Arrays 2-31 value == 0 2-32 100 values can be stored 2-33 index 0 accesses the first element 2-34 index 99 accesses the last element 2-35 fill the array for(int i = x.length-1; i>=0; i--){ x[i] = 100-i; } 2-36 print array elements on separate lines for(int i = 0; i 0 && n <= nums.length){ for (int i = 0; i