CS 120 – Fundamentals of Computing I - Rose

CSSE 120 – Fundamentals of Software Development I
Exam 1:
Winter 2003-2004
Name: ______________________________ Box #: ____________ Time allowed: 120 minutes
Instructor (check one): □Muchler
□Yoder
Instructions: This test has 3 parts:
 Part 1: closed-book.
 Part 2: open-book, paper-and-pencil problems.
 Part 3: open-book, on-the-computer problems.
For Part 1 (closed-book):

You may NOT use any external resources (no book, notes, computer, etc.)

Turn in Part 1 before you begin using any external resources.
o However, you may read Parts 2 and 3 (and begin thinking about those parts)
before you turn in Part 1.
For Part 2 (open-book, paper-and-pencil) and Part 3 (open-book, on-the-computer):

You may access your computer and any materials that you brought with you (books,
notes, etc), but only after you turn in Part 1.

You may use the network ONLY to access the
CSSE 120 web site (and items to which it links).
o You must NOT use ANY form of
communication. No email, no chat, not
anything!
Problem
Points
available
1.
10
o Disable all chat tools (AIM, ICQ, etc)
before the exam starts.
2.
5
3.
10
4.
10
5.
10
6.
10
7a.
10
7b.
15
8a.
20
Total
100
For paper-and-pencil problems (Parts 1 and 2):

Write all answers on these pages legibly.
o Use additional sheets as necessary.
o If you use additional sheets, indicate on
the exam where your answer continues.
Regarding code that you write:

All the code you turn in should be correct,
efficient, and use good style.

However, due to time constraints, no
documentation is required on the paper-and-pencil parts.

Appropriate documentation is required on the on-the-computer part.
Your
score
Page 2
Part 1 (closed-book):
Name ______________________________

You may NOT use any external resources in Part 1 (no book, notes, computer, etc.)

Turn in Part 1 before you begin using any external resources.
o However, you may read Parts 2 and 3 (and begin thinking about those parts)
before you turn in Part 1.
1. (10 points: 1 point for each item). For each of the following expressions, what is its value?
Expression
"CS" + "SE" + 120
"CS" + "SE" + 100 + 20 (not same as above)
"Is this the substring?".substring(4, 7)
"I think I can!".substring(4, 10).length()
17 – (10 % 5)
5 / 2
4 + 3 * 6
(4.0 == 3.0)
5.0/2.0
( true && false ) || !( true && true )
2. (5 points). For each of these questions, give a one-sentence answer.
a. If class A extends class B, what does that say about A and/or B?
b. If class F implements interface G, what does that say about F and/or G?
c. Give one difference between a primitive type (dial) and object type (label)?
Value
Page 3
3. (10 points: 1 point for each bulleted item). This problem has four parts: two on this page
and two on the next page. All four parts use exactly the same code.
If you mess up a part, feel free to ask for a fresh copy on which to start over.
Part (a): In the section below:

For each method, place a circle around the
entire method (both header and body)


Part (b): In the section below:

Place a rectangle around each mention of a
parameter in the code
For each constructor, place a circle around
the entire constructor (both header and
body)

Underline each statement that declares
and/or defines a local variable.
Place a rectangle around each declaration of
a field

Underline each place in the code where a
field is mentioned

For each statement that calls a method, put a
big arrow pointing to the statement
public class W extends X
implements Y, Z {
private int r;
private double s;
public class W extends X
implements Y, Z {
private int r;
private double s;
public W() {
this.r = 0;
this.s = 0.0;
}
public W() {
this.r = 0;
this.s = 0.0;
}
public W(int x) {
this.r = x;
this.s = 0.0;
}
public W(int x) {
this.r = x;
this.s = 0.0;
}
public W(int x, double y) {
this.r = x;
this.s = y;
}
public W(int x, double y) {
this.r = x;
this.s = y;
}
public int getMaxR(int n) {
int max = 99;
public int getMaxR(int n) {
int max = 99;
if (n > max) {
n = max;
}
return Math.max(max, this.r);
if (n > max) {
n = max;
}
return Math.max(max, this.r);
}
}
public int getPhloop() {
int sum;
int i;
public int getPhloop() {
int sum;
int i;
i = 0;
sum = 0;
while(i < 10) {
sum = sum + Math.random();
}
return this.r * this.s * sum;
i = 0;
sum = 0;
while(i < 10) {
sum = sum + Math.random();
}
return this.r * this.s * sum;
}
}
}
}
Page 4
Part (c): To the right of the boxes:
 Write code that causes the code in the boxes
to be executed.
public class W extends X
implements Y, Z {
private int r;
private double s;
public W() {
this.r = 0;
this.s = 0.0;
}
public W(int x) {
this.r = x;
this.s = 0.0;
}
public W(int x, double y) {
this.r = x;
this.s = y;
}
public int getMaxR(int n) {
int max = 99;
if (n > max) {
n = max;
}
return Math.max(max, this.r);
}
public int getPhloop() {
int sum;
int i;
i = 0;
sum = 0;
while(i < 10) {
sum = sum + Math.random();
}
return this.r * this.s * sum;
}
}
For example:
W w1 = new W();
Page 5
Part 2 (open-book, paper-and-pencil):

Turn in Part 1 before you begin using any external resources (book, notes,
computer, etc) on Part 2.

Note the rules for Part 2, as stated on the cover page of this exam.
4. (10 points). Consider a telephone. Briefly answer the 4 key questions for designing a
computational community for a telephone system. Limit your answer to the following space.

What is the desired behavior of the system?

Identify any four entities which interact to produce this behavior.

Specify how one of these entities works. (What goes inside each entity?)

Pick one entity that interacts with another entity and describe the interaction.
Page 6
5. (10 points: 2 points for each item). Consider the following statements.
String
myString;
String
yourString;
boolean isAcute;
int
longestString;
double
angle;
// angle is in degrees.
Suppose that all of the above variables are subsequently initialized (i.e., given appropriate
starting values). Now, answer the following questions:
a. Write a Java expression whose value is angle expressed in radians. (Hint: /180)
b. Write a Java statement or sequence of statements that sets the value of isAcute to
true if the value of angle is between 90 and -90; otherwise it sets the value of
isAcute to false.
c. Write a Java statement or sequence of statements that sets longestString to the
longer of the two strings, myString and yourString.
d. Write a sequence of Java statements that toggles the value of isAcute. (e.g. if
isAcute is true the statement will make it false.)
e. Write a Java statement that increases the value of angle by 45.
Page 7
(10 points: 5 points for each item). Consider the following statements.
int x, y;
int max, min;
Suppose all both of the above variables are subsequently initialized (i.e., given appropriate
starting values). Now, answer the following questions:
f. Write a sequence of Java statements that puts the larger of the values of x and y in max
and the smaller in min.

For example, suppose that the value of x is 5 and the value of y is 10. After
executing your sequence of statements, the value of max should be 10 and the value
of min should be 5. Important: your code must work for any initial values of x and
y, not just for 5 and 10.
g. Write a Java statement or sequence of statements that prints the numbers min through
max, one per line.

For example, suppose the value of min is 5 and the value of max is 10, then the
values
5
6
7
8
9
10
will be printed.
Page 8
7. (25 points: 15 points, 10 points). Consider the following interface:
public interface Statistic {
// Accept nextNumber as another number for the Statistic.
public void acceptNumber(double nextNumber);
// Return the current value of the Statistic,
// based on the numbers that it has accepted so far.
public double getStatistic();
}
In this problem, you will write two classes that implement Statistic.
Recall that your code should be correct, efficient, and use good style. However, due to time
constraints, no documentation is required on this paper-and-pencil part of the exam.
a. (15 points). On the next page, write a class called Sum such that:

Sum implements Statistic

Sum has a constructor with a single parameter (of type double) that is the first
number accepted by the Sum object.

You might ask (through getStatistic) a Sum object to return the sum of the numbers
that it has accepted (through its constructor and acceptNumber) so far.
For example the code segment
to the right should set result1
through result4 to:

4.0 (sum so far)

9.0 (sum to that point)

19.0 (sum so far)

20.0 (sum to that point)
double result1, result2, result3, result4;
Sum s = new Sum(4.0);
result1 = s.getStatistic(); // getStatistic returns 4.0
s.acceptNumber(5.0);
result2 = s.getStatistic(); // getStatistic returns 9.0
s.acceptNumber(10.0);
result3 = s.getStatistic(); // getStatistic returns 19.0
respectively. Of course, your
code should work correctly for
s.acceptNumber(1.0);
all numbers, not just those in
result4 = s.getStatistic(); // getStatistic returns 20.0
the example, and it should work
correctly no matter how many
times that acceptNumber and getStatistic are called.
Page 9
public class Sum implements Statistic {
}
Page 10
b. (10 points). On the next page, write a class called Olympic such that:

Olympic implements Statistic

Olympic has a constructor with two parameters (of type double) that are the first
two numbers accepted by the Olympic object.

You might ask (through getStatistic) a Olympic object to return the Olympic
average of the numbers that it has accepted (through its constructor and
acceptNumber) so far. An Olympic average is the average of all the numbers with
the lowest number removed.
For example the code
segment to the right
should set result1
through result4 to:

5.0 (drop the 4.0,
average is 5.0)

5.5 (still drop the
4.0, average 5.0 and
6.0.)
double result1, result2, result3, result4;
Olympic avg = new Olympic (4.0, 5.0);
result1 = avg.getStatistic(); // getStatistic returns 5.0
avg.acceptNumber(6.0);
result2 = avg.getStatistic(); // getStatistic returns 5.5
avg.acceptNumber(3.0);
result3 = avg.getStatistic(); // getStatistic returns 5.0

5.0 (drop the 3.0,
average 4.0, 5.0, and
6.0)

4.5 (drop the 2.0, average 3.0, 4.0, 5.0, and 6.0)
avg.acceptNumber(2.0);
result4 = avg.getStatistic(); // getStatistic returns 4.5
respectively. Of course, your code should work correctly for all numbers, not just those
in the example, and it should work correctly no matter how many times that
acceptNumber and getStatistic are called.
Hint: remember the smallest value entered so far.
Write your Olympic class on the next page, not here!
Page 11
public class Olympic implements Statistic {
}
Page 12
Part 3 (open-book, on-your-computer):

Turn in Part 1 before you begin using any external resources (book, notes,
computer, etc) on Part 3. Note the rules for Part 3, as stated on the cover page.
8. (20 points). Do this problem on your computer. To do so:
Step 1: CVS ~ Checkout and then open the BallWorld project:
o Its Repository folder is .../Exam1Project where ... indicates the usual
path to your portion of the turnin folders for this class on AFS.
o Its Module is BallWorld. It is the same BallWorld project that you used in
class; just its repository has changed.
Step 2: Design, implement, test and document appropriately (with Javadoc comments,
per our Javadoc rules in JavaEyes, Part 4) the class described below.
Step 3: Raise your hand to get the attention of an instructor or assistant.
o That person will grade your Java on the spot (by asking you to run it, and by
looking at your code and html documentation briefly).
Step 4: CVS ~ Add Contents and then CVS ~ Commit your Exam project.
o For Step 4, get help from your instructor or an assistant as needed.

Important: If you don’t understand what the ball is supposed to do, ask your instructor
or an assistant.

Important: If you have any compile-time errors that you cannot resolve, you may twice
ask for help from your instructor or an assistant. After that, you are on your own.
The Wrap ball is like the Bouncer ball except when it hits a wall it wraps around to the other
side. That is, if it is heading for the right wall, it will hit the right wall and then appear at the left
wall. Or, if it is heading for the top it will hit and appear at the bottom. In either case it never
changes its direction.
To help you get started, we have given you a start on the Wrap class in the file Wrap.java. Add
the code needed to this file.
Hint: Set your initial velocity to (Math.random()-0.5)*10.