Advanced
Programming - CS239 |
|
|
|
|
|
|
|
|
Preparing for the Final Exam What to
Expect: This exam
will be comprehensive. However, you will not be examined on packages,
graphics, or animation. You should expect three types of questions:
short answer (e.g., true/false, matching, fill-in, multiple choice), code
understanding (e.g., error identification, execution tracing), and code
development (e.g., method/class writing, test writing). What to
Study: Obviously,
you should study the lectures, labs, and readings, and you should review the
programming assignments. However, you should pay particular attention to the
labs since there are likely to be questions on the exam like those covered in
the labs. Sample
Questions for the Final Exam: The
questions below (as well as the
sample questions for exam 1, and exam
2 ) are illustrative of the kinds of questions that will be on the exam.
4.
1 Integer[] values; 5.
6.
2 values = new Integer[5]; 7.
8.
3 for (int i=0; i < values.length; i++) 9.
4 { 10.
5 values[i] = new Integer(i*10); 11.
6 }
13.
import java.util.*; 14.
15.
public class Printer 16.
{ 17.
private StringTokenizer tokenizer; 18.
19.
20.
public void print(String line) 21.
{ 22.
tokenizer = new StringTokenizer(line, ","); 23.
24.
if (tokenizer.hasMoreTokens()) 25.
{ 26.
printTokens(tokenizer); 27.
} 28.
} 29.
30.
31.
private void printTokens(StringTokenizer
tokenizer) 32.
{ 33.
String token; 34.
35.
token = tokenizer.nextToken(); 36.
if (tokenizer.hasMoreTokens()) 37.
{ 38.
printTokens(tokenizer); 39.
} 40.
41.
System.out.println(token); 42.
} 43.
44.
} carefully trace
the execution of the following: public class
PrinterDriver { public static void main(String[] args) { Printer test; test = new Printer(); test.print("Adams,Bernstein,Harris"); } }
operand1 operand2 arithmetic-operator
For example,
the following are valid expressions in postfix notation: 5 3 + Suppose we have
a Stack class that holds String objects and a method named evaluate in
the Callculator class that is passed the three parts
of a postfix expression (as String objects) and returns a String representation of the result. Trace the execution of the
following, carefully modeling memory at each step: Stack
terms; terms = new Stack(); terms.push("5"); terms.push("3"); terms.push("+"); result = Calculator.evaluate(terms.pop(),
terms.pop(), terms.pop()); terms.push(result); terms.push("4"); terms.push("*"); result = evaluate(terms.pop(), terms.pop(),
terms.pop());
47.
public class Adder 48.
{ 49.
50.
public static void main(String[] args) 51.
{ 52.
double result; 53.
54.
try 55.
{ 56.
result = add(args); 57.
System.out.println(args[0]+"+"+args[1]+":
"+result); 58.
} 59.
catch (ArrayIndexOutOfBoundsException
aioobe) 60.
{ 61.
System.out.println("Too few
operands."); 62.
} 63.
} 64.
65.
66.
67.
68.
public static double add(String[] terms) 69.
throws
ArrayIndexOutOfBoundsException 70.
{ 71.
double left, result, right; 72.
String operator; 73.
74.
left
= 0.0; 75.
right
= 0.0; 76.
77.
78.
try 79.
{ 80.
left = Double.parseDouble(terms[0]); 81.
} 82.
catch (NumberFormatException nfe) 83.
{ 84.
left = 0.0; 85.
} 86.
87.
try 88.
{ 89.
right =
Double.parseDouble(terms[1]); 90.
} 91.
catch (NumberFormatException nfe) 92.
{ 93.
right = 0.0; 94.
} 95.
96.
97.
result = right + left; 98.
99.
return result; 100.
} 101.
} carefully trace
the execution of each the following: java Adder
5 4
105.
public class Transaction 106.
{ 107.
public String type; 108.
public double amount, balanceAfterTransaction; 109.
} That is, each
time a deposit or withdrawal is made, a Transaction
object should be created and pushed onto the queue. (Note: You may use the Queue class we discussed in lecture.) |
Copyright ©2004