Question 1 (12 points) The following Student class is modified
from the one provided by your authors.
public class Student
{
private String first, last, homeAddress;
private double acctBalance;
public Student (String first, String last, String home)
{
this.first = first;
this.last = last;
homeAddress = home;
}
public Student(String first, String last)
{
this(first,
last, "");
}
public String toString()
{
String result;
result = firstName + " " + lastName + "\n";
result += "Home Address:\n" + homeAddress + "\n";
return result;
}
public double setBalance(double amt)
{
balance
= amt;
return
balance;
}
public void payBill(double payment)
{
balance
-= payment;
}
public String getName()
{
String
name;
name
= last + ", " + first;
return
name;
}
}
Identify whether each of the following is a
constructor, accessor, or mutator:
toString
method |
|
|
setBalance
method |
|
getName
method |
|
|
Student
method |
|
payBill
method |
|
|
|
|
Question 2 (10 points) Given the following code for ExceptionScope and
Propagation:
public
class Propagation
{
static public void main (String[] args)
{
ExceptionScope demo;
demo = new ExceptionScope();
System.out.println("Program
beginning.");
demo.level1();
System.out.println("Program
ending.");
}
}
public class ExceptionScope
{
public void level1()
{
System.out.println("Level 1 beginning.");
try
{
level2();
}
catch (ArithmeticException ae)
{
System.out.println ("An exception has occurred");
}
System.out.println("Level 1 ending.");
}
public void level2()
{
System.out.println("Level 2 beginning.");
level3 ();
System.out.println("Level 2 ending.");
}
public void level3 ()
{
int numerator = 10, denominator = 0;
System.out.println("Level 3 beginning.");
int result = numerator / denominator;
System.out.println("Level 3 ending.");
}
}
What is printed if
Propagation is executed?
|
|
|
|
|
|
|
|
|
Question 3 (10 points) For each of the following, write the requested Java statement (s) or
the requested Java expression. Assume
that all variable names have been declared unless the question explicitly says
otherwise.
charArray = aSillyString.toCharArray();
Question 4
(20 points) Given the following code, write the line number on
which each feature is found and the feature described.
|
Feature |
Line Number |
Code Example |
EX |
class header |
1 |
public class Play |
1.
|
a formal parameter |
|
|
2.
|
an actual parameter |
|
|
3.
|
An initializer list |
|
|
4.
|
primitive type |
|
|
5.
|
a method header |
|
|
6.
|
a declaration |
|
|
7.
|
an initialization |
|
|
8.
|
a method invocation |
|
|
9.
|
an access modifier |
|
|
10.
|
a return type |
|
|
1 public class Play
2 {
3
public static void main(String[] args)
4
{
5
int i;
6
double ratio;
7
double[] numbers;
8
9
numbers = {100, 2.0,0,0,0,2,-8,0,30};
10 i = -253;
11
12 for (i = 0; i <
(numbers.length-1); i++)
13 {
14 try
15 {
16 ratio = numbers[i] /
numbers[i + 1];
17 System.out.println
18 (numbers[i] +
"/" + numbers[i + 1] + "=" + ratio);
19 }
20 catch (ArithmeticException
ae)
21 {
22 System.out.println
23 ("Couldn't
calculate " + numbers[i] + "/" + numbers[i + 1]);
24 }
25 }
26
27 allDone(i);
28 }
29
30 public static void allDone(int
count)
31 {
32 System.out.println("We
finished with " + count + " values.");
33 }
34 }
Question 7 (10 points) Write the letter corresponding to the best answer for
each of the following situations on the blank provided.
______Trying to open a file
for writing if the file does not exist in the specified directory is a
a. run-time error
b. compile time error
c. no error
_____ Under what circumstances would the following
statement produce a compile time error?
myKeyboard
= new Scanner (new File (“abc”));
I.
if myKeyboard has not been declared to be a Scanner object
II.
if the file “abc” is not
in the same directory as the .class file for this code
III.
if the class in which
this line occurs does not have the statement,
import java.util.Scanner;
a. I and II
b. II and III
c. I and III
d. all of the above
______ Given the following code (which you should
assume is syntactically correct) which of the following statements will cause a
run-time Exception to be thrown?
String word = {“CS 239”);
I.
System.out.println
(word.charAt(5));
II.
System.out.println
(word.length());
III.
System.out.println
(word.charAt(word.length()));
a. I only
b. II only
c. III only
d. none of the above
_____ Which of
the following is a correct negation of the compound condition, (2 < 3) || (5
> 2)?
a. (2 > 3)
&& (5 < 2)
b. (2 > 3) ||
(5 < 2)
c. (2 >= 3)
&& (5 <= 2)
d. (2 >= 3) ||
(5 <= 2)
e. none of the
above
_____ Under
which set of circumstances will the second part of the following compound
condition not be tested. Assume that a and b have been initialized.
a. (2 != 35) && ( a < b)
b. (2 < 3) ||
(a < b)
c. (2 >= 3) || (a < b)
d. (2 < 3) &&
(a < b)
Question 8 (15
points) The code below was written by a student
from Virginia Tech. It compiles and it
runs but it has some defects. You are
to show that JMU students are smarter than Tech students.
a.
List the
contents of the array, numbers in the
box below.
|
b.
Write the
output of the program in the box to the right of the code labeled, OUTPUT.
c.
Write the
specific situation appropriate exception that the Tech student should have used
in line 17 in the box below.
|
d.
Write the
specific situation appropriate exception that the Tech student should have used
in line 29 in the box below.
|
1 |
OUTPUT |
Question 9 (10 points) The class below does not have a javadocs class
description.
a. Study the class and determine what it does.
b. Write the javadocs description for the class in the
box above the code.
c. Show the output produced when the following program is
run in the boxes below the code.
javadocs Note:
Code has no compile errors.
|
public
class Question9
{
public static void main (String [] args)
{
Scanner myStringScanner;
String
temp;
myStringScanner = new Scanner
(“adlk#aa2#w3ad#fl2$wa s#a@d!fj3a#f0we#”);
myStringScanner.useDelimiter (“#”);
while (myStringScanner.hasNext())
{
temp =
myStringScanner.next();
System.out.println
('*' + temp + '*');
}
}
}
output
|
|
|
|
|
|
|
|
|