CSSE 120 – Fundamentals of Software Development I
Exam 1 ANSWERS:
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.
15
7b.
10
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
Value
"CS" + "SE" + 120
CSSE120
"CS" + "SE" + 100 + 20 (not same as above)
CSSE10020
"Is this the substring?".substring(4, 7)
his
"I think I can!".substring(4, 10).length()
6
17 – (10 % 5)
17
5 / 2
2
4 + 3 * 6
22
(4.0 == 3.0)
false
5.0/2.0
2.5
( true && false ) || !( true && true )
false
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?
A inherits (that is, has) the fields and methods of B
b. If class F implements interface G, what does that say about F and/or G?
F must implement (that is, provide bodies for) all the methods specified in G
c. Give one difference between a primitive type (dial) and object type (label)?
Differences include: Object-type names are references to things (that is,
they simply label the thing), while primitive-type names “are” the thing
itself (like a dial that points to its value). You can ask objects (that is, things
of object-type) to do stuff for you, while primitive-type things are inert. All
primitive types begin with lower-case letters, while object types begin (by
convention) with upper-case letters.
Page 3
3. (10 points: 1 point for each bulleted item). This problem has three parts: two on this page
and one on the next page. All three 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 = 0;
int i = 0;
public int getPhloop() {
int sum = 0;
int i = 0;
while(i < 10) {
sum = sum + Math.random();
i++;
}
return this.r * this.s * sum;
while(i < 10) {
sum = sum + Math.random();
i++;
}
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;
}
For example:
W w1 = new W();
W w2 = new W(45);
W w3 = new W(45, 4.0);
public int getMaxR(int n) {
int max = 99;
if (n > max) {
n = max;
}
return Math.max(max, this.r);
}
public int getPhloop() {
int sum = 0;
int i = 0;
while(i < 10) {
sum = sum + Math.random();
i++;
}
return this.r * this.s * sum;
}
}
w1.getMaxR(3);
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.
Here is one answer (from a student) that focused on the telephone itself (except
for the last part, where the student broadened the system). Other answers
focused on the entire system – those answers were also fine.
What is the desired behavior of the system?
The telephone should be able to take in a number, convert it to binary
code that can be transmitted via a cable. It should also be able to convert
voice waves to a signal that can be transmitted as well. Also, it should be
able to hang up / pick up a call.
Identify any four entities which interact to produce this behavior.
A number button
The “hang-up” latch
The receiver
The speaker in the earpiece
Specify how one of these entities works. (What goes inside each entity?)
A number button:
– Realizes it is pressed
– Sends 2 frequencies (specific to where that button is located)
Pick one entity that interacts with another entity and describe the interaction.
A receiver in one phone needs to send its information to a speaker of
another phone. The speaker converts its signal into a voice we can hear.
Page 6
5. (10 points: 2 points for each item). Consider the following statements.
String
myString;
String
yourString;
boolean isAcute;
String
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)
angle * Math.PI / 180.0
or
angle * 3.14159 / 180.0
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.
isAcute = (angle > -90
&&
angle < 90);
or (equivalent but more verbose)
if (angle > -90 && angle < 90) {
isAcute = true;
} else {
isAcute = false;
}
c. Write a Java statement or sequence of statements that sets longestString to the
longer of the two strings, myString and yourString. If the strings are the same
length you can assign it to either.
if (myString.length() > yourString.length()) {
longestString = myString;
} else {
longestString = 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.)
isAcute = !isAcute;
or the equivalent, more verbose, IF statement
e. Write a Java statement that increases the value of angle by 45.
angle = angle + 45.0;
or
angle += 45.0;
Page 7
6. (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:
a. 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.
if (x > y) {
min = y;
max = x;
} else {
min = x;
max = y;
}
or, equivalently:
min = Math.min(x, y);
max = Math.max(x, y);
b. 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.
int k = min;
while (k <= max) {
System.out.println(k);
++ k;
}
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 {
private int sum;
public Sum(double firstNumber) {
this.sum = firstNumber;
}
public void acceptNumber(double nextNumber) {
this.sum += nextNumber;
}
public double getStatistic() {
return this.sum;
}
}
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 {
private double sum;
private int howMany;
private double smallest;
public Olympic(double first, double second) {
this.sum = first + second;
this.howMany = 2;
if (first < second) {
this.smallest = first;
} else {
this.smallest = second;
} // or:
this.smallest = Math.min(first, second);
}
public void acceptNumber(double nextNumber) {
this.sum += nextNumber;
++ this.howMany;
if (nextNumber < this.smallest) {
this.smallest = nextNumber;
}
}
public double getStatistic() {
return (this.sum – this.smallest)
/ (this.howMany – 1);
}
}
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 Exam1 project (it’s the same as BallWorld):
o Its Repository folder is the same repository that you have been using.
o Its Module is Exam1.
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 Exam1 project.
o For Step 4, get help from your instructor or an assistant as needed.
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.
Important: If you don’t understand what the ball is supposed to do, ask your instructor
or an assistant.
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.
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.
Hint: Set your initial velocity to (Math.random() - 0.5) * 10.
In the answer that follows, I have omitted parts of the class for which no changes
were needed.
/**
* An Animate Ball that wraps around the screen.
*
* @author David Mutchler.
*/
public class Wrap implements Ball, Animate {
double x;
double y;
double xVelocity;
double yVelocity;
double radius;
World world;
Page 13
/**
* Initialize the Ball's position and radius (fixed)
* and velocity (random).
*/
public Wrap() {
this.x = 0.0;
this.y = 0.0;
this.radius = 25.0;
this.xVelocity = (Math.random() - 0.5) * 10;
this.yVelocity = (Math.random() - 0.5) * 10;
}
public double getX() {
return this.x;
}
public double getY(){
return this.y;
}
public double getRadius() {
return this.radius;
}
public void setWorld(World theWorld) {
this.world = theWorld;
}
public void act() {
this.x += this.xVelocity;
this.y += this.yVelocity;
// Wrap in x-direction
if (this.x > this.world.getMaxX()) {
this.x = this.world.getMinX();
} else if (this.x < this.world.getMinX()) {
this.x = this.world.getMaxX();
}
// Wrap in y-direction
if (this.y > this.world.getMaxY()) {
this.y = this.world.getMinY();
} else if (this.y < this.world.getMinY()) {
this.y = this.world.getMaxY();
}
}
}
© Copyright 2026 Paperzz