# Southern Illinois University Carbondale CS202

Document Sample

					           Southern Illinois University Carbondale
CS202: Introduction to Computer Science
Fall 2005
Section 6

MIDTERM EXAMINATION 2

Instructor: Kenny Fong

November 1, 2005

Name: __________________________________________
ID: _____________________________________________
Signature: _______________________________________

DIRECTIONS:
1. Write your name and student ID above.
2. The maximum score of this exam is 100.
3. Attempt all questions.
4. Complete all answers in the spaces provided.
5. No aids are permitted. All notes and books have to be placed on the floor.
6. Blank sheets are available for your scratch work.
7. You are not required to document any code you write.
8. You are not allowed to leave the exam room during the first hour of the exam.
9. The duration of the exam is 120 minutes.
10. Cheating is an academic offense. Your signature above indicates that you
understand and agree to the University’s policies regarding cheating on exams.

Module                 Marks              Maximum
1                                          33
2                                          26
3                                          17
4                                          24
Total                                      100
MODULE 1: ITERATION STATEMENTS (33 points)

1.   (16) What output is produced by each of the following code fragments? If there is any infinite loop, just
write down “infinite loop”.

a)   int num = 3;
while (num <= 7) {
num++;
System.out.print(num + ″ ″);
}

b) int num = 7;
do {
num--;
System.out.print(num + ″ ″);
} while (num <= 3);

c)   int product = 1;
for (int i = -5; i <= 3; i += 2) {
product *= i;
System.out.print(product + ″ ″);
}

d) int product = 1;
for (int i = -5; i <= 3; i -= 2) {
product *= i;
System.out.print(product + ″ ″);
}

2
e)   int product = 1;
for (int i = -5; i <= 3; i -= 2) {
product *= i;
System.out.print(product + ″ ″);
if (product > 0) break;
}

f)   int product = 1;
for (int i = -5; i <= 3; i -= 2) {
product *= i;
System.out.print(product + ″ ″);
if (product > 0) continue;
}

g) int num = 0;
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 3; j++) {
++num;
}
System.out.print(num + ″ ″);
}

h) int sum = 0;
int j = 0;
do {
j++;
for (int i = 5; i > j; i--)
sum = sum + (i+j);
} while (j < 5);
System.out.println(sum);

3
2.   (17) Write a complete Java program that opens and reads an input file with pathname C:\in.txt, and prints
the number of English letters on each line of the input file in an output file with pathname C:\out.txt. The
nth line of the output file should contain only the number of English letters on the nth line of the input file.
You may assume that the input file contains no blank lines. A sample input file and the corresponding
output file are shown below:

Input file C:\in.txt                                  Output file C:\out.txt
Kenny is GREAT!!!                                     12
SIUC 150 SIUC                                         8
z                                                     1
PRESIDENT George W. Bush                              20
11/1/2005                                             0

4
5
MODULE 2: CLASSES AND METHODS (26 points)

1.   (2) Give one difference between a local variable and an instance variable.

2.   (2) When an object is instantiated, which method of the object is the first one invoked? What is the return
type of this method?

3.   (2) If a method is overloaded, how must two definitions of this method (in the same class) differ from each
other?

6
4.   (2) In order to enforce encapsulation, should we make an instance variable public or private? Why?

5.   (2) What is a mutator method? What is its return type?

6.   (2) Write a single statement that declares and creates an integer wrapper object storing the integer value 34.

7
7.   (10) Consider the following class definition:

public class Duple
{
private static int t = 0;
private int x;
private int y;

public Duple()
{
x = 0;
y = 0;
}

public Duple(int initX, int initY)
{
x = initX;
y = initY;
}

public void negate()
{
x = -x;
y = -y;
}

{
x += duple2.x;
y += duple2.y;
}

public boolean equals(Duple duple2)
{
return (x == duple2.x && y == duple2.y);
}

public static int getT()
{
return t;
}

public String toString()
{
t += 1;
return ″(″ + x + ″,″ + y + ″)″;
}
}

Consider the following declarations:

Duple     a   =   new   Duple();
Duple     b   =   new   Duple(2, -3);
Duple     c   =   new   Duple(5, 7);
Duple     d   =   new   Duple(-2, 3);

8
a)   What output is produced by the following statements?

System.out.println(a);
System.out.println(b);
System.out.println(c);
System.out.println(Duple.getT());

b) What output is produced by the following statements?

System.out.println(c);
System.out.println(d);

c)   Write a single statement that would cause b.equals(d) to return true.

d) If we make the getT() method non-static, can the Duple class still compile? Why?

9
8.   (4) Consider the following class definition that uses the Duple class defined in the previous problem:

public class DupleWrapper
{
private Duple duple;

public DupleWrapper(int x, int y)
{
init(x, y);
}

{
}

private void init(int x, int y)
{
duple = new Duple(x, y);
}
}

Consider the following declarations:

DupleWrapper dw = new DupleWrapper(3, 7);
Duple d2 = new Duple(5, 4);

a)   What output is produced by the following statements?

System.out.println(d2);

b) Can the following statement compile? Why?

dw.init(5, 0);

10
MODULE 3: APPLETS (17 points)

Write a complete Java applet that draws exactly the following. The size of the applet is 300 × 300 pixels.

height / 4

height / 4

width / 4                                width / 4

11
12
MODULE 4: PROGRAMMING (24 points)

Design and implement a class named Die that is used to represent a die used in games. The class must have
five instance variables:
• an int variable for the number of faces,
• an int variable for the current face value,
• an int variable for the number of times the die has been rolled,
• an int variable for the number of rolls in which the face value is an odd number,
• a Random variable for a random number generator object that simulates the rolling.

The class should have two constructors:
• The default constructor constructs a die with 6 faces.
• The second constructor takes one parameter, which specifies the number of faces of the die to be
constructed. However, if the parameter value is less than 4, the number of faces will be set to 4.
Besides the number of faces, both constructors should also initialize the other instance variables appropriately.
The face value of a die should be initialized to 1 (but this is not counted as the first roll).

The class should have an accessor method for the current face value.

The class should provide a method roll() that rolls the die using the random number generator and returns
the new face value.

The class should provide a toString() method that returns a string representation of the current status of the
die in the format shown in the sample dialog below:

Number of faces: 8
Current face value: 5
Number of rolls: 34
Number of odd-face rolls: 21

Your code should be as clean and simple as possible, and encapsulation should be strictly enforced.

13
14
15


DOCUMENT INFO
Shared By:
Categories:
Stats:
 views: 18 posted: 5/1/2010 language: English pages: 15
How are you planning on using Docstoc?