Computer Science E-10a Problem Set 2 Part A
Computer Science E-10a Problem Set 2 Part A
[1]
[2]
(a)
(b)
System.out.println ( 3 + 4 );
(c)
System.out.println ( 3 + "4" );
3 points
Dr. H. H. Leitner
[3]
4 points
Consider the following program, and the resulting error messages that occur
when the program gets compiled using the javac command. In 1 or 2 simple
English sentences, explain why the error messages occurred:
// This program does not compile. How come?
class BadExample
{
public static void main (String [] args)
{
int x = 3;
int y = 7;
computeSum();
}
static void computeSum()
{
int sum = x + y;
System.out.println("sum = " + sum);
}
}
Dr. H. H. Leitner
[4]
4 points
[5]
Precisely how often does each of the following loops execute? Assume that
variable i does not get changed by the body of the loop inside the braces.
(a)
(b)
(c)
}
}
}
[6]
6 points total
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 ();
}
}
Dr. H. H. Leitner
Solve the following problems in Cloud9 using the javac compiler as you go. Make sure you
name your file with the correct file name.
[7]
8 points
Write a complete Java program containing a for loop to print the message
DooBeeDooBeeDooBeeDooBeeDooBeeDooBee Do
[8]
6 points
Dr. H. H. Leitner
[9]
9 points
D
I I
A
M
O
O
N N
D
[10]
10 points
Consider the following Java method that outputs four times a quote from
a famous computer scientist named Brian Kernighan:
programming!");
programming!");
programming!");
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! No
individual method should have more than 4 individual statements.
Dr. H. H. Leitner
[11]
6 points
This problem is required for all graduate-credit students. Undergraduatecredit students may submit a solution for extra credit.
The following static method named drawFigure is supposed to produce the
following output:
////////////////\\\\\\\\\\\\\\\\
////////////********\\\\\\\\\\\\
////////****************\\\\\\\\
////************************\\\\
********************************
Dr. H. H. Leitner
that calls on the method so that the pattern shown above gets output. The
missing Java code should be written as a function of the available variables, and
not utilize hard-coded numbers. You may only add code where it says
"missing Java code", in the test and increment sections of the for loops.
[12]
Write a method named top() that draws this figure (there are precisely 7
underscore characters in the top line):
________
/
\
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:
_______
/
/
\
\
/
\_______/
-"-'-"-'-"_______
/
/
\
\
/
\_______/
Dr. H. H. Leitner
-"-'-"-'-"\
/
\_______/
_______
/
/
\
-"-'-"-'-"\
/
\_______/
[13]
Dr. H. H. Leitner
Dr. H. H. Leitner