Handout #7 Part A — Pencil-and-Paper Exercises (30

Handout #7
Computer Science 1: Great Ideas in Computer Science
This assignment is due prior to 5 PM on Friday, February 26. You might find it
useful to consult the first two chapters in the Reges and Stepp textbook.
You must electronically submit your solutions. The mechanics for doing so will be
discussed during section, and a summary of the steps involved will appear on our course
website.
The main learning objectives are that you gain familiarity with the for loop, variable
assignment, and the flow of control between methods in Java.
Part A — Pencil-and-Paper Exercises (30 points total)
You need not run Java programs on a computer in solving the following
problems. Place your answers into separate “text” files using the names
indicated on each problem. Please create your text files using the same text
editor that you use for your java files. Answers submitted in another file
format such as .doc, .pages, .rtf, or .pdf will lose one point per problem.
[1]
3 points
Use file Prob01.txt
What is the precise output from the following code?
int foo;
int bar = 10;
foo = 17 - 4 / 10;
foo += 6;
bar = foo % bar;
System.out.println
System.out.println
System.out.println
System.out.println
Spring, 2016
(foo * 2);
(foo + bar);
(foo);
(bar);
Dr. H. H. Leitner
Computer Science 1: Great Ideas in Computer Science
[2]
Page 2
2 points
Use file Prob02.txt
Precisely what does this program print when executed?
class Prob2
{
public static void main (String [] args)
{
System.out.print ("She said, \"");
System.out.print ("Never put off till tomorrow... ");
System.out.println ("\nWhat you can do");
System.out.print ("The day");
System.out.println ();
System.out.println ("After tomorrow!\"");
}
}
[3]
3 points (1 point for each)
Use file Prob03.txt
Precisely what is printed by each of the following?
[4]
(a)
System.out.println ( "3 + 4 ");
(b)
System.out.println (
3 + 4
(c)
System.out.println (
3 + "4"
2 points
);
);
Use file Prob04.txt
Assume that you have a variable named count that will take on the values
1, 2, 3, 4, and so on. You are going to formulate a Java expression in terms of
count that will yield a particular sequence. For example, to get the sequence 2,
4, 6, 8, 10, 12, ..., you would use the expression (2 * count).
What Java expression would yield the sequence 4, 19, 34, 49, 64, 79, … ?
Spring, 2016
Problem Set 3
Dr. H. H. Leitner
Computer Science 1: Great Ideas in Computer Science
[5]
Page 3
7 points total
Use file Prob05.txt
Part (a): 4 points
Precisely what does the following program output when it is executed?
class Strange
{
static void first()
{
System.out.println("Inside first method!");
}
static void second()
{
System.out.println("Inside second method!");
first();
}
static void third()
{
System.out.println("Inside third method!");
first();
second();
}
public static void main (String [] args)
{
first();
third();
second();
third();
}
}
Part (b): 3 points
What would have been the output of the preceding program if the
method third had instead been written like this?
static void third()
{
first();
second();
System.out.println("Inside third method!");
}
Spring, 2016
Problem Set 3
Dr. H. H. Leitner
Computer Science 1: Great Ideas in Computer Science
[6]
Page 4
3 points
Use file Prob06.txt
Complete the following Java code
for (int j = 0; j < 5; j++)
{
// your code goes right here!
}
so that this loop will print the following numbers, one per line:
-2
1
4
7
10
[7]
6 points (2 points for each)
Use file Prob07.txt
Precisely how often do the following loops execute? Assume that variable i is
an integer variable that does not get changed by the “body” of the loop.
[8]
(a)
for (i = 0; i < 10; i++) { …
(b)
for (i = -10; i <= 10; i++) { …
(c)
for (i = 10; i > -10; i = i -2) { …
4 points
}
}
}
Use file Prob08.txt
To convert a temperature from Fahrenheit degrees to Kelvin (absolute), the
following relationship holds:
!
Spring, 2016
Problem Set 3
Dr. H. H. Leitner
Computer Science 1: Great Ideas in Computer Science
Page 5
Someone wrote the following program that is supposed to print the Kelvin
equivalent of any Fahrenheit temperature, based upon the current value of a
numeric variable named fahrenheit. Unfortunately, even though the
program compiles correctly, it produces an incorrect answer. What’s wrong
with the code, and how would you fix it?
class Prob8
{
public static void main (String [] args)
{
double farenheit = 212.0;
double k = (5/9) * (farenheit-32) + 273.16;
System.out.println ("Kelvin equivalent = " + k);
}
}
Part B — Programming Problems
(70 points)
Solve the following problems using your account on nice.harvard.edu
You must submit your work electronically via your account on
nice.harvard.edu. We will demonstrate how to do this and provide you with
documentation on our website. Be sure you use the precise file names
shown when you write your solutions!
[9]
7 points
Use file Diamond.java
Write a simple Java program to produce the following output:
D
I I
A
A
M
M
O
O
N N
D
Spring, 2016
Problem Set 3
Dr. H. H. Leitner
Computer Science 1: Great Ideas in Computer Science
[10]
Page 6
6 points
Use file Prob10.java
Carefully type the following program into a file named Prob10.java
class Prob10
{
public static void main (String [] args)
{
int trump = -1;
sanders = 17;
System.out.println (sanders * Trump);
}
}
Now attempt to compile this program using javac, and you will discover 3
errors are produced by the Java compiler. All the errors are due to simple
mistakes in the above program. Fix this program so that the program will
compile and work correctly.
[11]
14 points
Use file Prob11.java
The following static method named drawFigure is supposed to produce the
following output:
////////////////\\\\\\\\\\\\\\\\
////////////********\\\\\\\\\\\\
////////****************\\\\\\\\
////************************\\\\
********************************
static void drawFigure()
{
for (int line = 1; line <= 5; line++)
{
for (int i = 1; missing Java code )
{
System.out.print("/");
Spring, 2016
Problem Set 3
Dr. H. H. Leitner
Computer Science 1: Great Ideas in Computer Science
Page 7
}
for (int i = 1; missing Java code) {
System.out.print("*");
}
for (int i = 1; missing Java code) {
System.out.print("\\");
}
System.out.println();
}
}
Unfortunately, there are 3 for loops that contain missing Java code. You
need to fill in those missing expressions, and then include the completed
drawFigure method in a file that contains a main program that calls on the
method so that the pattern is output.
[12]
8 points
Use file DooBee.java
Write a Java program containing a for loop to print the message
DooBeeDooBeeDooBeeDooBeeDooBeeDooBee Do
[13]
10 points
Use file Pattern.java
Write a complete Java program to output the pattern
*****
****
***
**
*
Spring, 2016
Problem Set 3
Dr. H. H. Leitner
Computer Science 1: Great Ideas in Computer Science
Page 8
Your program should define the following “constant” to control how many lines
get output: final int HEIGHT = 5; Thus if HEIGHT were set equal to 7
instead of 5, the output would look like this:
*******
******
*****
****
***
**
*
[14]
10 points
Use file Prob14.java
Consider the following Java method that outputs four times a quote from
a famous computer scientist named Brian Kernighan:
static void print4x()
{
System.out.println ("Controlling complexity is the essence of programming!");
System.out.println ("Controlling complexity is the essence of programming!");
System.out.println ("Controlling complexity is the essence of programming!");
System.out.println ("Controlling complexity is the essence of programming!");
}
Use this print4x method in a complete Java program that outputs the
Kernighan quote 64 times. Your solution must contain a main method, the
print4x method, and at least one additional method. You should not use
any for loops or while loops or anything like that — just method calls!
[15]
15 points
Use file Drawing.java
Write a method named top() that draws this figure (there are precisely 7
underscore characters in the top line):
________
/
\
/
\
Spring, 2016
Problem Set 3
Dr. H. H. Leitner
Computer Science 1: Great Ideas in Computer Science
Page 9
Now write a method named bottom() that draws this figure:
\
/
\________/
Finally, define a main method that uses the top() and bottom() methods
(along with any additional methods you might like) to output the following
sequence:
_______
/
\
/
\
\
/
\_______/
-"-'-"-'-"_______
/
\
/
\
\
/
\_______/
-"-'-"-'-"\
/
\_______/
_______
/
\
/
\
-"-'-"-'-"\
/
\_______/
Spring, 2016
Problem Set 3
Dr. H. H. Leitner
Computer Science 1: Great Ideas in Computer Science
Page 10
Part C — Extra Credit Problems
[16]
(7 to 21 points)
7 points of “extra credit”
Use file Triangle.java
Write a Java program that produces the output
100
200
300
400
…
…
…
900
202
304
406
308
412
418
916
932
…
1028
Your program must not, of course, consist entirely of
System.out.println statements! Use for loops.
[17]
14 points
Use file Song.java
Write a Java program that produces as output the lyrics of the song, “There
Was an Old Lady.” Use methods for reach verse and the refrain. Here are the
song’s the complete lyrics:
There was an old lady who swallowed a fly.
I don’t know why she swallowed that fly,
Perhaps she'll die.
There was an old lady who swallowed a spider,
That wriggled and jiggled and wiggled inside her.
She swallowed the spider to catch the fly.
I don’t know why she swallowed that fly,
Perhaps she'll die.
There was an old lady who swallowed a bird;
How absurd, to swallow a bird!
She swallowed the bird to catch the spider
Spring, 2016
Problem Set 3
Dr. H. H. Leitner
Computer Science 1: Great Ideas in Computer Science
Page 11
That wriggled and jiggled and wiggled inside her.
She swallowed the spider to catch the fly.
I don’t know why she swallowed that fly,
Perhaps she'll die.
There was an old lady who swallowed a cat.
Imagine that, she swallowed a cat.
She swallowed the cat to catch the bird,
She swallowed the bird to catch the spider
That wriggled and jiggled and wiggled inside her.
She swallowed the spider to catch the fly.
I don’t know why she swallowed that fly,
Perhaps she'll die.
There was an old lady who swallowed a dog.
What a hog! To swallow a dog!
She swallowed the dog to catch the cat,
She swallowed the cat to catch the bird,
She swallowed the bird to catch the spider,
That wriggled and jiggled and wiggled inside her.
She swallowed the spider to catch the fly.
I don’t know why she swallowed that fly ,
Perhaps she'll die.
There was an old lady who swallowed a goat.
Just opened her throat and swallowed a goat!
She swallowed the goat to catch the dog,
She swallowed the dog to catch the cat,
She swallowed the cat to catch the bird,
She swallowed the bird to catch the spider,
That wriggled and jiggled and wiggled inside her.
She swallowed the spider to catch the fly.
I don’t know why she swallowed that fly,
Perhaps she'll die.
There was an old lady who swallowed a cow.
I don't know how she swallowed a cow!
She swallowed the cow to catch the goat,
She swallowed the goat to catch the dog,
She swallowed the dog to catch the cat,
She swallowed the cat to catch the bird,
She swallowed the bird to catch the spider,
That wriggled and jiggled and wiggled inside her.
She swallowed the spider to catch the fly.
I don’t know why she swallowed that fly,
Perhaps she'll die.
Spring, 2016
Problem Set 3
Dr. H. H. Leitner
Computer Science 1: Great Ideas in Computer Science
Page 12
There was an old lady who swallowed a horse,
She died, of course.
!
Spring, 2016
Problem Set 3
Dr. H. H. Leitner