Michele Weigle - COMP 14 - Spr 04 COMP 110 Midterm review Luv Kohli October 27, 2008 MWF 2-2:50 pm Sitterson 014
Michele Weigle - COMP 14 - Spr 04 Announcements Program 3 due Friday, October 31, 2pm
Michele Weigle - COMP 14 - Spr 04 Questions? Any other questions?
Michele Weigle - COMP 14 - Spr 04 Today in COMP 110 Take out a sheet of paper Write your name on it Take about 5 minutes to write down how you think you did on the midterm, and why
Midterm scores Average score: ~76
Q3 a. double var1 = 9 / 2; b. int var2 = (int) (6.6 / 3.0) 9 / 2 is integer division, resulting in 4 division is done before the automatic typecast to double. var1 is set equal to 4, or 4.0 b. int var2 = (int) (6.6 / 3.0) 6.6 / 3.0 is 2.2 (int) 2.2 is 2 var2 is set to 2 c. float var3 = (float) 5 / 2 (float) 5 / 2 causes 2 to be typecast to float (float) 5 / (float) 2 is 2.5 var3 is set to 2.5
Q4 Accepted most reasonable things as long as they were not too far off
Q8 3 * 4 * 3 = 36 3 iterations 4 iterations 3 iterations int i = 0; while (i < 3) { for (int j = 1; j <= 4; j++) for (int k = 3; k > 0; k--) System.out.println(i + “,” + j + “,” + k); } i++; 3 * 4 * 3 = 36 3 iterations 4 iterations 3 iterations
Q13: Tracing code the magical snail quickly eats the strange airplane NAM NAM NAM!!!
Q15: Remove middle words Y o u w i l n t g ! 1 2 3 4 5 6 7 8 9 10 11 int firstSpace = str.indexOf(“ ”); int lastSpace = str.lastIndexOf(“ ”); String strNoMiddle = str.substring(0, firstSpace) + str.substring(lastSpace); Y o u w i l n t g ! 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 Y o u g ! 1 2 3 4 5 6
Q16: Determining if a string is a palindrome Odd-length string r a c e 1 2 3 4 5 6
Q16: Determining if a string is a palindrome Even-length string r e d 1 2 3 4 5
Q16: Determining if a string is a palindrome ‘u’ != ‘e’ Not a palindrome r u d e 1 2 3 4 5
Q16: Determining if a string is a palindrome Need a loop What is being repeated? Character comparison What characters are we comparing? First half of string to reverse of second half of string What is our loop terminating condition? We find a pair of characters that do not match
Q16: using a for loop r a c e 1 2 3 4 5 6 i str.length() – 1 - i public boolean isPalindrome(String str) { for (int i = 0; i < str.length() / 2; i++) if (str.charAt(i) != str.charAt(str.length() – 1 – i)) return false; } return true; r a c e 1 2 3 4 5 6
Q16: Can also do it with a while loop public boolean isPalindrome(String str) { int first = 0; int last = str.length() - 1; while (first < last) if (str.charAt(first) != str.charAt(last)) return false; } first++; last--; return true;
Michele Weigle - COMP 14 - Spr 04 Wednesday Static variables and methods