Upload
franklin-wilkins
View
216
Download
1
Embed Size (px)
Citation preview
Review – Final Test
Chapters 8,10,11
• Locate error in following statement
tryi = Integer.pareseInt(str);
catch(NumberFormatException e)System.out.println(“Input is not
integer”);
try and catch blocks must be enclosed within curly braces. A corrected version:
• Locate the error in the following statement and show how to fix itswithch(channel){
case 1: System.out.println(“Channel 1”);case 3: System.out.println(“Channel 2”);case 5: System.out.println(“Channel 3”);
}
• There should be a break statement at the end of each case:
switch (channel) { case 2: System.out.println(“channel 1"); break; case 5: System.out.println(“channel 2"); break; case 11: System.out.println(“channel 3"); break;
• Locate error in following statement:
do i /= 2
while (i > 10);
• There's no semicolon at the end of the loop body. A corrected version:
do i /= 2;
while (i > 10);
• Which one of the following statements are not equivalent to other two:
(a)while(i<10) System.out.println(i);(b)for(;i<10;) System.out.println(i);(c)Do System.out.println(i); while(i<10);
• (c). All three statements are potentially infinite loops. (c) is different from the other two, however, because it always prints the initial value of i, even if i is greater than or equal to 10.
• What will be printed when the following code is executed? int a = 1, b = 0, c; try { c = a / b; System.out.println("Division completed"); } catch (ArithmeticException e) { System.out.println("You can't catch me!"); } System.out.println("Time to move on");
• You can't catch me! • Time to move on
• Which one of the following is a legal type for the controlling expression in a switch statement:
• (a) char, • (b) double, or • (c) String, or • (d) none of the above?
• (a) char
• What is the role of the break statement inside switch statements:
• (a) required at the end of each case; • (b) not required at the end of each case, but
usually needed; or • (c) not required at the end of each case, and
usually not needed?
• (b) not required at the end of each case, but usually needed
• For each of the following situations, indicate which type of statement would be more appropriate: an if statement or a switch statement. Assume that each test will be accompanied by a different action.
• (a) Testing whether a String variable is equal to one of fifty different state abbreviations (such as "GA" or "AL").
• (b) Testing whether an int variable is between 0 and 59, between 60 and 69, or between 70 and 100.
• (c) Testing whether an int variable matches one of 50 different TV channels.
• (d) Testing whether a double variable matches one of the values 0.0, 0.5, 1.0, 1.5, ..., 10.0.
• (e) Testing whether the length of a String variable matches one of the values 1, 2, 3, ..., 10.
(a) if statement(b) if statement(c) switch statement(d) if statement(e) switch statement
• For each of the following properties, answer – I if the property applies only to instance methods, – C if it applies only to class methods, – B if it applies to both, and – N if it applies to neither.
• (a) Can be public or private(b) Can access instance variables(c) Can access class variables(d) Can have parameters(e) Can call instance methods in the same class(f) Can call class methods in the same class(g) Can be overloaded(h) Can use the keyword this(i) Must be called by an object(j) Calls must always include a dot (.)
• B (a) Can be public or privateI (b) Can access instance variablesB (c) Can access class variablesB (d) Can have parametersI (e) Can call instance methods in the same classB (f) Can call class methods in the same classB (g) Can be overloadedI (h) Can use the keyword thisI (i) Must be called by an objectN (j) Calls must always include a dot (.)
• Given the Account class, create a Checking Account class by extending Account class. The CheckingAccount will store the number of checks written against the account. It will need a method that returns the number of checks written so far and a method that writes a check for the given amount.
public class Account {private double balance;
public Account(double initialBalance) { balance = initialBalance; }public void deposit(double amount) { balance += amount; }
public void withdraw(double amount) { balance -= amount; }}
public class CheckingAccount extends Account { private int checksWritten = 0; public CheckingAccount(double initialBalance){
super(initialBalance); } public int getChecksWritten() {
return checksWritten; } public void writeCheck(double amount) {
withdraw(amount); checksWritten++;
} }
• For a Complex class that stores a complex number with real and imaginary parts in instance variables realPart and imaginaryPart respectively, write a an equals method that for the complex class. For two complex numbers to be equal, they must have the same real part and the same imaginary part.
public boolean equals(Object obj) { if (!(obj instanceof Complex))
return false; Complex c = (Complex) obj; return realPart == c.realPart && imaginaryPart == c.imaginaryPart; }