Page 180 - Introduction to Programming with Java: A Problem Solving Approach
P. 180

                146
Chapter 4 Control Statements
4.
[after §4.10] Given this main method:
1
public static void main(String[] args)
2{
3
int i;
String debug;
for (int i=0; i<3; i++)
4
5
6{
7
switch (i
* i)
= "first";
case 2:
= "second";
= "third";
8{
9
10
case 0:
debug
break;
case 1:
debug
case 3:
debug
default:
11
12
13
14
15
16
17
System.out.println("In default");
// end switch
18
}
19
} // end for
System.out.println("i = " + i);
} // end main
20
21
Trace the code using either the short form or the long form. To help you get started, here’s the trace setup. For the short form, you won’t need the line# column.
 line#
 i
 debug
 output
 5.
          Apago PDF Enhancer
[after§4.10]Giventhebelowprogramskeleton.Insertcodeinthe<insert-code-here>sectionsuchthatthe program prints the product of even integers from 2 to num. You are not required to perform input validation.
public class ProductEvenInts
{
}
// end class ProductEvenInts
public static void main(String[] args)
{
}
Scanner stdIn = new Scanner(System.in);
int i, num, product;
System.out.print("Enter a positive even number: ");
num = stdIn.nextInt();
<insert-code-here>
System.out.println("Product = " + product);
// end main
Sample session:
 Enter a positive even number: 8
Product = 384

























   178   179   180   181   182