Vous êtes sur la page 1sur 10

Computer Science E-10a: Introduction to Computer Science Using Java, I

Problem Set Two, part A


Simple Exercises (26 points total)
You need not submit java programs for the following problems. Please create your text (.txt)
files in your cloud9 editor. Answers submitted in another file format such as .doc, .pages, .rtf, or
.pdf will not be accepted. Please use the exact name for each file.

[1]

3 points (1 point for each)

Use file Plus.txt

Precisely what is printed by each of the following?

[2]

(a)

System.out.println ( "3 + 4" );

(b)

System.out.println ( 3 + 4 );

(c)

System.out.println ( 3 + "4" );

3 points

Use file Tomorrow.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!\"");
}
}

Dr. H. H. Leitner

Computer Science E-10a: Introduction to Computer Science Using Java, I

[3]

4 points

Use file BadExample.txt

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);
}
}

Here is what was output after we attempted to compile this program:


BadExample.java:9: error: cannot find symbol
int sum = x + y;
^
symbol:
variable x
location: class BadExample
BadExample.java:9: error: cannot find symbol
int sum = x + y;
^
symbol:
variable y
location: class BadExample
2 errors

Dr. H. H. Leitner

Computer Science E-10a: Introduction to Computer Science Using Java, I

[4]

4 points

Use file Kelvin.txt

To convert a temperature from Fahrenheit degrees to Kelvin (absolute), the


following relationship holds:
!
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. Whats wrong with the
code, and how would you fix it?
class Kelvin {
public static void main(String [] args) {
double fahrenheit = 212.0;
double k = (5/9) * (fahrenheit-32) + 273.16;
System.out.println("Kelvin equivalent = " + k);
}
}

[5]

6 points (2 points for each)

Use file ForLoops.txt

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)

for (int i = 0; i < 3000; i++) {

(b)

for (int i = -5; i <= 5; i++) {

(c)

for (int i = 50; i > -10; i = i -10) {

}
}
}

[6]

6 points total

Use file Strange.txt

Part (a): 3 points


Precisely what does the following program output when it is executed?
Dr. H. H. Leitner

Computer Science E-10a: Introduction to Computer Science Using Java, I

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!");
}

Dr. H. H. Leitner

Computer Science E-10a: Introduction to Computer Science Using Java, I

Main Programming Problems

(33-49 points total)

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

Use file DooBee.java

Write a complete Java program containing a for loop to print the message
DooBeeDooBeeDooBeeDooBeeDooBeeDooBee Do

[8]

6 points

Use file Prob8.java

Carefully type the following program into a file named Prob8.java


class Prob8 {
// compute the area of a circle
public static void main(String [] args)
{
radius = 17;
pi = 3.14;
System.out.println (pi * radius * radius);
}
}
If you attempt to compile this program using javac, you will discover 5
errors are produced by the Java compiler. All the errors are due to two simple
omissions in the above program. Fix this program so that it will compile and
work correctly.

Dr. H. H. Leitner

Computer Science E-10a: Introduction to Computer Science Using Java, I

[9]

9 points

Use file Diamond.java

Write a complete Java program to produce precisely the following


output. (Do not use any loops or conditional statements; mainly you need
to use just System.out.println statements!)

D
I I
A

M
O

O
N N
D

[10]

10 points

Use file Prob10.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
System.out.println ("Controlling complexity is the essence of
System.out.println ("Controlling complexity is the essence of
System.out.println ("Controlling complexity is the essence of
}

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

Computer Science E-10a: Introduction to Computer Science Using Java, I

[11]

6 points

Use file Prob11.java

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:
////////////////\\\\\\\\\\\\\\\\
////////////********\\\\\\\\\\\\
////////****************\\\\\\\\
////************************\\\\
********************************

static void drawFigure()


{
final int N = 5;
for (int line = 1; line <= N; line++)
{
for (int i = 1; missing Java code )
{
System.out.print ("/");
}
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 named Prob11.java that contains a main program

Dr. H. H. Leitner

Computer Science E-10a: Introduction to Computer Science Using Java, I

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]

5 points of extra credit

Use file Drawing.java

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

Computer Science E-10a: Introduction to Computer Science Using Java, I

-"-'-"-'-"\
/
\_______/

_______
/

/
\
-"-'-"-'-"\
/
\_______/

[13]

5 points of extra credit

Use file Pattern.java

Write a complete Java program to output the pattern


*****
****
***
**
*
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:
*******
******
*****
****
***
**
*

Dr. H. H. Leitner

Computer Science E-10a: Introduction to Computer Science Using Java, I

Dr. H. H. Leitner

Vous aimerez peut-être aussi