You are on page 1of 9

Problem Set 1

Variables and Operators

1. Write comments that explain what these lines of code doL

e.g.
float average; // Declare a variable called average, of type float

int x = 5;
float y=5f;
String address;
String name = “Iasone”;
x = x + 5;
a++;
a--;
y += 5;
z *= 3;
d=x % 3;

2. Write code for the following comments:

// Declare a variable called Foo, of type integer

// Declare a string variable called address and assign “1138 Kessel Drive” to it.

/* Declare a variable to store floating pointer numbers and give that variable a name of Buzz
and assign 1.138 to it.
*/

// declare an int called average

// declare an int variable named g1 and assign 78 to it

// declare an int variable named g2 and assign g1 plus 7 to it

// declare an int variable named g3 and assign g2 minus 2 to it

// add g1, g2, and g3 and assign that value to a variable called average

// assign average divided by 3 to average

// print out on one line “The average of “ followed by the values of g1, g2, and g3 separated by “,”

ICS111 – Problem Set 1 1


3. Trace the following program using the trace table below by writing out the values stored in
each of the variables after each line of code:

1 String s;
2 int n;
3 float d;
4 s = “hello”;
5 n = 5;
6 n = 6;
7 d = 2.1f;
8 s = s+“goodbye”;
9 n = 4;

s n d
1 “” Not defined Not defined
2 “” 0 Not defined
3
4
5
6
7
8
9

4. Trace the following code with a trace table:

1 int a; 

2 int b; 

3 int c = 5; 

4 float f = 2.5f; 

5 a = c - 2; 

6 b = a * c; 

7 f /= 2; 

8 a = b + c;
9 b += c; 

10 b--;

ICS111 – Problem Set 1 2


If Statements

5. What does each of these operators mean?

==

!=

>

>=

<

<=

6. When does a conditional with && evaluate true?

7. When does a conditional with || evaluates true?

8. In the lines of code marked a to e, indicate which lines are true or false or in
error:

int x = 10;
int k = 2;
float zzz = 6.1f;

a. x >= k 


b. k > zzz 


c. x = 10 


d. (k > zzz) || (x < zzz)


e. (x == 10) && (zzz != 2.1f)

ICS111 – Problem Set 1 3


9. What will the following code print out?

int n = 100;
int b = 7;

if (n >= 100) {

if (n / b > 10) {

if (b % 3 == 0) {

System.out.println("first");

} else if (b % 3 == 1) {

System.out.println("second");

} else {

System.out.println("third");

} else {

if (b % 2 == 0) {

System.out.println("fourth");

} else {

System.out.println("fifth");}

} else {

System.out.println("sixth");

ICS111 – Problem Set 1 4


10. Trace each line of code below using a trace table with the columns:
linenumber, go, half, and printout.

While Loops

11. Fill in the blanks in the following code to produce the desired outcome:
(10 points)

a. Print numbers from 0 to 1000:

int c = 0;

while (______) {
System.out.println(c);
c++;
}

ICS111 – Problem Set 1 5


b. Print even numbers from 100 to 512:

int c = ___

while (______) {
System.out.println(c);
c=c+2;
}

c. Print even numbers from 100 to 512:

int c = ___
while (______) {
c+=2;
System.out.println(c);
}

d. Print numbers from 512 to 100:

int _________

while (______) {
System.out.println(c);
c--;
}

e. Print numbers from 1 to 1000:

int d = _____
do {
System.out.println(d);
d++;
} while (d<1000);

f. Print numbers from 512 to 100:

int x = _____
do {
x--; System.out.println(x);
} while (______);

ICS111 – Problem Set 1 6


g. Print the following output:

11111
22222
33333
44444

int x=1;
int y=1;

while(y<=4) {
while(_____) {
System.out.print(y);
x++;
}
System.out.println(“”);
y++;
___________
}

h. Print the following output:

11 21 31 41
12 22 32 42
13 23 33 43

int x=1;
int y=1;

while(_____) {
while(_____) {
System.out.print(x+”“+y+” “);
x++;
}
System.out.println(“”);
y++;
____________
}

i. Write the code to print the following output:

ICS111 – Problem Set 1 7


11 12 13
21 22 23
31 32 33
41 42 43

j. Write the code to print the following output:


xoxoxo
oxoxox
xoxoxo
oxoxox

12. Trace the following code with a trace table.

int c = 0;
float b = 0.2f;
int e = 3;
float res = 1;
while (c < e) {
res *= b;
c++;
}

13. Look at the Bug Run project (provided in the lecture notes folder).

In this program the bug rotated from the left side of the
screen to the right side of the screen and then stopped.
Add a second while loop to move the bug back to the left
side of the screen.

ICS111 – Problem Set 1 8


14. Look at the Saberbouncer project (provided in the lecture notes folder).

In this program, a saber bounces


between the left side of the screen to the
right side of the screen (while rotating).
Modify this program to have the saber
also bounce between the top and bottom
of the screen as well as from left to right.

ICS111 – Problem Set 1 9

You might also like