CS 110 Fundamentals of Computing - Exam 1
Professor Bolker (day section)
Closed book and notes
ANSWERS
Attached you will find the source
code for class TextLine
. Use it to answer some of the questions that
follow. You need not turn in that source code, so feel free to pull it loose so
that it’s easier to refer to. Please keep the rest of the pages stapled
together.
1) (20%)
In this fragment of TextLine.java
(with line numbers)
33 public void paintOn( Screen s, int x, int y )
34 {
35 for (int i = 0; i < text.length(); i++) {
36 s.paintAt( text.charAt(i), x+i, y );
37 }
38 }
a) How
many tokens are there on line 35? 19
Carefully circle them.
++ is one token
b) What are the java keywords on lines 33-38?
public, void, int, for
c) Find all the places on line 36 where a
message is sent to an object. In each case identify the name and type of the
object receiving the message, the file where you would you expect to find the
code for the method that will respond to the message, and write the declaration
for that method, showing its signature.
A charAt message is sent to object text of type String. The code for the charAt method should be
in file String.java, with declaration public char charAt(int spot) (Of course there is no way to know what name
the programmer chose for the integer parameter.)
A paintAt message is sent to object s of type Screen. The code for the paintAt method should be
in file Screen.java, with declaration public void paintAt(char c, int x, int y)
2) (15%) Use the grid attached to this exam
(ruled so that character positions are easy to identify) to show the exact,
complete output from the command shown. There are two copies of the grid so you
can fix mistakes without erasing. Circle the one that’s your final answer
Note: the grid is not a picture of a Screen. It’s the window on your monitor where
the program is running. It is larger than what you will need to show
what’s printed by the program.
A |
: |
\ |
> |
j |
a |
v |
a |
|
T |
e |
x |
t |
L |
i |
n |
e |
|
|
|
|
|
|
T |
e |
x |
t |
L |
i |
n |
e |
|
u |
n |
i |
t |
|
t |
e |
s |
t |
|
|
|
|
|
+ |
+ |
+ |
+ |
+ |
+ |
+ |
+ |
+ |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
+ |
H |
e |
l |
l |
o |
, |
|
+ |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
+ |
|
|
w |
o |
r |
l |
d |
+ |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
+ |
|
|
|
|
|
|
|
+ |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
+ |
+ |
+ |
+ |
+ |
+ |
+ |
+ |
+ |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
3) (7%) Find an example of delegation in TextLine.java. Explain what is happening.
On line 59 return text.length(); asks a String object how long it is, in order
to return the answer to whatever client sent the getLength() message.
One can also view
line 48 as this TextLine object delegating work to itself.
4) (10%) Explain the use of this on line 22 of TextLine.java and find a place where an optional this has been omitted but could be used.
The left hand side
of the assignment statement on line 22 needs this so that it refers to the field text declared
on line 12 instead of the parameter text
declared on line 20.
Line 48 could read this.paintOn(
s, 0, 0 );
Line 36 could read s.paintAt(this.text.charAt(i), x+i, y);
5) (10%)
Describe how the author of the TextLine class used two important Java coding
conventions.
Variables
have been named using the startLowerInternalCaps
convention. Indenting (prettyprinting) shows program
structure. Public methods have javadoc comments.
Braces around blocks are vertically aligned appropriately. Class name begins
with a capital letter.
6) (15%)
Draw box-and-arrow pictures showing variable t in the main method of TextLine.java
and its value
o
just
after the declaration on line 72
″world!″
TextLine
o
just
after line 73 has executed
7) (15%)
Write code at the //method body
comment that will
make this method do what its javadoc says it should do:
/**
* Print out a range of numbers, one to a line.
*
* Example: the message printRange(2,4) should print
*
* 2
* 3
* 4
*
* and return the value 3.
*
* @param start the start of the range.
* @param end the end of the range.
*
* @return the number of numbers printed.
*/
public int printRange( int start, int end )
{
// method body
for (int i = start; i <= end; i++ ) {
System.out.println( i );
}
return end-start+1;
}
8) (10%) How would you test the method you wrote
in the previous problem? (You may assume that what you wrote will compile.) You
may write either pseudocode or real Java. If you choose to write the Java it
must be correct. If you choose to write pseudocode it must be clear, and easy
to turn into code. Use the other side of this page for your answer.
This is probably the
hardest question on the exam. Testing is really hard. Here is some pseudocode:
Declare
public class PrintRange in which printRange
is a method
In main() in class PrintRange
Create
a Terminal t
Create
a PrintRange instance pr
// a
self documenting test
Use Terminal
t to print out the example in the javadoc, as it appears
there
t.println(
“printRange should return 3, did return ” + pr.printRange(2,4));
//
allow the user to enter interactive tests
loop while user says she wants to continue
prompt for start and end values
warn that if end < start weird things may happen!
send printRange message
report value returned by printRange