- 1. (20 points):
- You have been given a design document for a veterinary registration system
for implementation in Java. It states: "A pet has an owner, a registration
date, and a vaccination-due date. A cat is a pet that has a flag
indicating if it has been neutered, and a textual description of its
markings." Given that the
Pet
class has already been defined
and you expect the Cat
class to be used freely throughout the
application, please answer the following questions:
- 2. (20 points):
- What will happen when you try to compile the following code?
public void printArray(Object x) {
if (x instanceof int[]) {
int[] n = (int[]) x;
for (int i = 0; i < n.length; i++) {
System.out.println("integers = " + n[i]);
}
}
if (x instanceof String[]) {
System.out.println("Array of Strings");
}
}
(Circle all correct answers.)
A. |
It compiles without error. |
B. |
The compiler objects to line 2 comparing an Object
with an array. |
C. |
The compiler objects to line 3 casting an Object
to an array of int primitives. |
D. |
The compiler objects to line 7 comparing an Object
to an array of Objects . |
- 3. (20 points):
- The
GenericFruit
class declares the following method to
return float
number of calories in the average serving
size:
public float aveCalories()
Your Apple
class, which extends GenericFruit
,
overrides this method. In a DietSelection
class that
extends Object
, you want to use the
GenericFruit
method on an Apple
object. What
is the correct way to finish the statement in the following code
fragment so the GenericFruit
version of aveCalories
is called using the gf
reference?
GenericFruit gf = new Apple();
float cal = // finish this statement using gf
(Circle the correct answer.)
A. |
gv.aveCalories(); |
B. |
((GenericFruit) gf).aveCalories(); |
C. |
gf.super.aveCalories(); |
D. |
There is no way to call the GenericFruit method |
- 4. (20 points):
- How many
String
objects are created in the following code?
String a, b, c;
a = new String("1234");
b = a; c = a + b;
(Circle the correct answer.)
A. |
One |
B. |
Two |
C. |
Three |
D. |
Four |
- 5.(20 points):
- Create a program
SwapChar
that takes a string and two
integers as command-line arguments. The program should print out
a modified string obtained from the original string by swaping two
characters at positions specified by the two integers. For example,
suppose that you enter the following:
java SwapChar oat 0 2
The program should display
tao
and exit. In addition, your program should catch and handle all
exceptions that might arise because of a bad input.
Each type of exception should be handled independently, by printing an
appropriate error message which would specify what exactly was wrong
with the input. Hint: please remember that invalid array access
causes ArrayIndexOutOfBoundsException
. You might also find
useful some of the following methods:
public static int java.lang.Integer.parseInt(String s)
throws NumberFormatException;
public char java.lang.String.charAt(int index)
throws IndexOutOfBoundsException;
public void java.lang.StringBuffer.setCharAt(int index, char c)
throws IndexOutOfBoundsException;
public java.lang.StringBuffer java.lang.StringBuffer.append(char c);
Please continue your answer on the other side of page 3 --->