Suggested preparation for the exam: Study 6-8
hours spread over 2-3 days. Review the slides, example code, and assignments.
Go over all the material 2-3 times.
ICS 111 Fall 2007 Review Guide 3
Terminology: Write the
number or term of the corresponding definition (20%)
|
A.
B.
C.
D.
E.
|
F.
G.
H.
I.
J. |
K.
L.
M.
N.
O. |
P.
Q.
R.
S.
T. |
|
Program1 (20%) 1. 2. 3. 4. 5. |
Scope (15%) 1. 2. 3. 4. 5. |
||
Program3 (applet with 5 by 7 100 pixel squares – 20%)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Coding (25%): Write a complete Java program, using the ICS 111 Java Coding Standard (so use comments & naming conventions), which has three static methods: the main() method, method1(), and method2(). method1() will create and return an array of Strings. The array will store 100 Strings. Initialize the 100 elements to the following values: Store the numbers from “1” to “100” as Strings. But for multiples of 3, store “Fizz” instead of the number. For the multiples of 5, store“Buzz”. For numbers which are multiples of both 3 and 5, store “FizzBuzz”. Use a loop and “if statements” to do this. method2() will print out the array of Strings. Use a loop to do this. The main() method will call methods method1() and method2().
Terminology: Match
the descriptions with the best matching term. Write either the number or the
term on the answer sheet.
|
A. B. C. D. E. F. G. H. I. J. K. L. M. N. O. P. Q. R. S. T. |
1. algorithm 2. applets 3. application 4. array 5. array element 6. array index 7. block 8. Boolean 9. call (invoke) a method 10. case sensitive 11. catch block 12. class 13. code 14. comment 15. compile 16. constant 17. constructor 18. debugging 19. declare a variable 20. decrement 21. edit 22. exception 23. float (floating point) 24. I/O 25. if statement 26. increment 27. instantiate an object 28. integer 29. Java bytecode 30. Java language 31. keyword 32. local variable 33. loops 34. machine code 35. method 36. modulus 37. non-static methods 38. non-static variable 39. object 40. parameter 41. program 42. run (execute) 43. scope 44. static methods 45. static variable 46. string 47. try block 48. Unicode 49. variable |
Program1 (text output):
Write the output to the program on the answer sheet.
1
public class Program1 {
2
public static void main(String[] arguments) {
3 String ch1 = "E", ch2 =
"e";
4 Program1.compare(ch1, ch2);
5 Integer a = 10;
6 while(a>7){
7 Integer b = 20;
8 b++;
9 if(b>20){
10 Integer c = 30;
11 ++c;
12 }
13 a--;
14 }
15 System.out.println(a);
16 String word = "this is a
pen";
17 Integer x = 0;
18 for(Integer i = 0; i <
word.length(); i++){
19 if("i".equals(word.substring(i,
i+1))){
20 System.out.print(i);
21 }
22 else if("
".equals(word.substring(i, i+1))){
23 x++;
24 System.out.print(x);
25 }
26 else{
27 System.out.print(word.charAt(i));
28 }
29 }
30 System.out.println();
31 String [] islands =
{"Kauai", "Oahu", "Molokai",
32 "Lanai",
"Maui", "Hawaii"};
33 String s = "";
34 try{
35 for(Integer i = 0; i <
islands.length; i++){
36 s = s +
islands[i].substring(i, i+1);
37 }
38 }
39
catch(StringIndexOutOfBoundsException e){
40 System.out.println(s);
41 }
42 x = 0;
43 Integer [][] array = new
Integer[4][5];
44 for(Integer i = 0; i<
array.length; i++){
45 for(Integer j = 0; j<
array[0].length; j++){
46 array[i][j] = x;
47 x++;
48 }
49 }
50 for(Integer i = 0; i<
array.length; i++){
51 for(Integer j = 0; j<
array[0].length; j++){
52
if(array[i][j].equals(10)){
53 System.out.println(i
+ ", " + j);
54 }
55 }
56 }
57 }
58 public static void
compare(String char1, String char2){
59 String s = new
String("s");
60 if (char1.equals(char2) ==
false) {
61 s = s + s + char1 + char2;
62 }
63 if (char1.equals(char2) ==
true) {
64 s = char1 + s + s + char2;
65 }
66 else {
67 s = char1 + char2 + s + s;
68 }
69 System.out.println(s);
70 }
71 }
Scope: Write the scope (specific line
numbers) of the following variables from Program1 on the answer sheet.
1. b 2.
c 3. i (both of them) 4. array 5.
char2
Program2 (applet): Draw the outline of the rectangles on the grid on the
answer sheet.
import java.applet.Applet;
import java.awt.Graphics;
import java.awt.Color;
public class Program2 extends Applet
{
public void paint(Graphics
g){
Integer x = 4 * 3 - 23 / (26 %
4);
Integer y = x;
--x;
Integer w = 1999 / 1000;
Integer h = w;
w++;
Rectangle r1 = new Rectangle(x,
y, w, h, Color.red, g);
r1.draw();
if (10.0 > 90.0 || 100.0
<= 90.0) {
x = 4;
y = 0;
}
else {
if (2 != 3 && 2 == 3)
{
x = 1;
y = 3;
}
else {
x = 2;
y = 1;
}
}
final Boolean T = true, F = false;
if(!(T&&F)){
if((!F)||F){
w=1;
}
else{
w=2;
}
}
if(!(F&&T)){
if(!(!F)){
h=3;
}
else{
h=4;
}
}
Rectangle r2 = new Rectangle(x,
y, w, h, Color.blue, g);
r2.draw();
Integer i = 0;
x=0;
for (i = 0; i <= 2; i++) {
x++;
}
i=5;
y=10;
while (i > 0) {
i--;
y -= 2;
}
i = 11;
w = 0;
while ((i % 5) != 0) {
w += 2;
i--;
}
i=0;
h=0;
while (i < 5) {
i++;
h++;
}
Rectangle r3 = new Rectangle(x, y,
w, h, Color.green, g);
r3.draw();
Integer[] array = new
Integer[5];
for (i = 0; i < array.length;
i++){
array[i] = i + i;
}
x = array[2] + 1;
for (i = 0; i < array.length;
i++){
y = array[i];
}
y = 8 - y;
for (i = 0; i < array.length; i++)
{
array[i] = array[i]*array[i];
}
w = 18 - array[2];
h = 41 - array[3];
Rectangle r4 = new Rectangle(x, y,
w, h, Color.yellow, g);
r4.draw();
Integer [][] array2D = new
Integer[6][8];
for(i = 0; i< array2D.length;
i++){
Integer a = 0;
for(Integer j = 0; j< array2D[0].length;
j++){
array2D[i][j] = a;
a++;
}
}
x = array2D[1][0];
y = array2D[0][2];
w = array2D[5][2];
h = array2D[4][3];
Rectangle r5 = new Rectangle(x, y,
w, h, Color.orange, g);
r5.draw();
}
}
// ***********************************************
class Rectangle{
private Integer x;
private Integer y;
private Integer w;
private Integer h;
private Color c;
private Graphics g;
public Rectangle(Integer xp,
Integer yp,
Integer wp, Integer hp, Color cp,
Graphics gp){
x = xp * 100;
y = yp * 100;
w = wp * 100;
h = hp * 100;
c = cp;
g = gp;
//System.out.println(x + ", " + y+
", " + w+ ", " + h);
}
public void draw(){
g.setColor(c);
g.fillRect(x, y, w, h);
}
}