LECTURE 10 NOVEMBER 2004 ASSIGNMENTS 11/12 CodeLab #10 MIDTERM 2 11/11 5-7 PM 1226 Engr #2 pencils and student ID Review session 6-8 PM CSS 1325 MIDTERM 2 REVIEW Pig Latin Translator Exception Game CONDITIONALS 1) Use getInt(), and if entry is negative, ask again - note: if they enter a negative twice, we can't help it 2) Fill in the following method: public boolean areAnyTwoTrue(boolean a, boolean b, boolean c) return (a&&b || b&&c || a&&c); } 3) Evaluate the following expressions: 6 == 6 || x < y: true 2 < 4 && (false || 5 <= 4): false x <= y && !(z != z) || x > y: false 4) Simplify the following expressions: x < y || z < y && y <= z: x < y !(x <= y && y != z): x > y || y == z !(a == b || c == d && d > 1): a != b && c != d || d <= 1 done==true: done done==false: done REPETITION STRUCTURES 1) What's the value of x after these loops? int x=0, i=0; int x = 0; while (i < 6) { do { x += i; x -= 10; i += 2; } while (x > 0); } x == 6 2) What's wrong with these loops? int i=10, sum=0; do { while (i > 0) { takeTurn(); sum += i; boolean done = isGameOver(); i++; } while (!done); } Never ends. done only has scope inside the while... it doesn't exist for the condition test! 3) Write some code that corresponds to the following diagram: even odd even odd even odd int i = 0; while (i<6) { if (i%2 == 0) { S.o.p("even"); } else { S.o.p("odd"); } i++; } A. Translate the following loop into the other two formats: int x = getInt(); while (x > 0) { arena.add(new Robot(...)); x--; } - watch for posttest problems, and show for-header variations - which one is the best choice? (least repetition, easiest to get) STRING MANIPULATION public static boolean isPalindrome (String o) { String r = ""; for (int i = o.length() - 1; i >= 0; i--) { char c = o.charAt(i); r = r + c; } return o.equals(r); } - also need to handle case mismatches - r = r.toLowerCase() - o.equalsIgnoreCase(r) CLASS CONCEPTS 1) A class is instantiable if you can create objects of that class. (T) 2) Data members must be declared inside the constructor. (F) 3) A visibility modifier describes who is allowed to see and directly change the value of a data member. (T) 4) Every parameter has both a type and a name. (T) 5) Every argument should be passed with both a type and a name. (F) 6) If a method has no return value, you can leave the return type blank. (F) 7) Instance methods can only be called on objects, while class methods can be called on classes. (T) 8) The purpose of a constructor is to initialize all the instance data. (T) 9) It is possible to have two methods with the same name in the same class. (T) 10) It is possible to have two data members with the same name in the same class. (F) 11) The reserved word "this" refers to the class that it appears in. (F) 12) An instance data member called "x" can be referred to as "this.x". (T) 13) Class constants are both static and final. (T) 14) If a data member is private, there is no way it can ever be changed from outside the class. (F) 15) A parameter is a local variable. (T) 16) If you assign a value to a parameter, you may change the value of the original argument. (F) 17) Local variables are garbage-collected after their method ends. (F) 18) Method composition is when you declare one method inside another. (F) 19) When you pass an object as an argument, the only thing copied into the parameter is the reference to that object. (T) 20) If you do not write any constructors, you get an empty default one. (T) 21) Dot notation is optional when you call an instance method from another instance method in the same class. (T) 22) Like the Math class, a main class cannot be instantiable. (F)