Professional Documents
Culture Documents
0. Predict the output of the program, then run the program and answer the
questions:
x += 7;
System.out.println( x );
x *= 4;
System.out.println( x );
x -= 6;
System.out.println( x );
x /= 2;
System.out.println( x );
x %= 5;
System.out.println( x );
}
Questions:
0. What do the <op>= operations do?
1. Write a simple assignment operation (ie x = ___) for x += 8;
2. Write x = x / 10; using an <op>= statement.
3. Do you think there are arithmetic modifiers for boolean variables as
well?
What do you think they look like? What do they do? Experiment to
verify your predicitions.
4. Question 3 for Strings.
1. Predict the output of the program, then run the program and answer the
questions:
x++;
System.out.println( x );
++x;
System.out.println( x );
x--;
System.out.println( x );
--x;
System.out.println( x );
int y = x++;
System.out.println( "x: " + x + " y: " + y );
y = ++x;
System.out.println( "x: " + x + " y: " + y );
}
Questions:
0. What do ++ and -- do?
1. How could you write x++; as:
0. A simple assignment (x = ___)?
1. An arithmetic modifier statement?
2. How could you rewrite x = x - 1; using the least possible number of
keystrokes?
3. What is the difference between x++ and ++x ?
4. What is the value of y after:
int x = 10;
int y = x--;
y = x++ + ++x + x++ + ++x + ++y;
2. Predict the output of the program, then run the program and answer the
questions:
Questions:
0. What does for do?
1. In the for statement, what does int i = 0; do? When does this
statement execute?
How many times does this statement execute?
2. What does i < 10 do? When is it evaluated? How many times does the
program evaluate this expression.
3. What does i++ do? When is it evaluated? How many times does the
program execute this statement?
4. How many times does System.out.println(i) execute? For what values of
i?
5. What values does i take on? Note that this question is subtly
different than the second part of question 4.
6. Is this program equivalent to the above program?
4. Complete the program so that it prints out the String name in reverse on
single line.
For the value of name shown, the output of the program should be:
tnagitS divaD
Hints: If name is "David Stigant", what expression (using the variable name
and a String function) will give you a 't'?
an 'n'?
an 'a'?
a 'g'?
an 'i'?
Do you notice a pattern? What is the same in each of the expressions? What
is different?
You need a loop (why?)
Everytime a loop executes, the same thing happens ==> this should be the
thing that is the same in each expression.
The thing that changes everytime through a loop is the loop variable ==> use
that to take care of the differences in each expression.
Finally, what is the first value of the loop variable? How does it change
each time through the loop? What condition will be true
for as long as the loop needs to execute? (Or, when does the loop need
to stop?)
5. Complete the program so that it prints out the characters of String name with
spaces between them on the same line.
If name is "David Stigant" the output should be:
D a v i d S t i g a n t
public static void main( String [] args ) {
String name = "David Stigant";
Hints: You need to print out "D ", "a ", "v " etc.
There are several things to print, so you need to use what?
Let name be "David Stigant". For each thing you need to print out, write an
expression involving name and String function(s) that
results in that thing.
Looking at your expressions, what is the same (==> loop body template) and
what is different (==> variable)
What does the loop variable need to start out as? How does it change each
time through the loop? When do you end?
6. Complete the program so that it prints the String name two characters at a
time on different lines.
If name is "David Stigant" the output should be:
Da
av
vi
id
d
S
St
ti
ig
ga
an
nt
7. Complete the program so that it prints the String name two characters at a
time on different lines without repeating characters.
If the name is "David Stigant" the ouput should be:
Da
vi
d
St
ig
an
t
8. Complete the program so that it prints the String name several times, but
"rotated" one character at a time.
If the name is "David Stigant" the output should be:
David Stigant
avid StigantD
vid StigantDa
id StigantDav
d StigantDavi
StigantDavid
StigantDavid
tigantDavid S
igantDavid St
gantDavid Sti
antDavid Stig
ntDavid Stiga
tDavid Stigan
Hints: There are two things to print on each line. For example, on the
fourth line (line 3) you need to print "id Stigant" and "Dav".
How can you write expressions involving name to get each of those Strings?
What about "d Stigant" and "Davi" for line 4?
D
Da
Dav
Davi
David
David
David S
David St
David Sti
David Stig
David Stiga
David Stigan
David Stigant
10. Complete the program so that it prints (assuming name is "David Stigant"):
D
av
vid
id S
d Sti
Stiga
Stigant