Upload
joella-marshall
View
214
Download
0
Tags:
Embed Size (px)
Citation preview
Output ProgramsThese slides will present a variety of small programs. Each program has a control structure that was introduced in this chapter.
Our concern will be with the output of each program, and more importantly, to develop a way to determine program output correctly for programs that involve control structures.
You can expect that on quizzes and/or tests only a program segment or a method is shown.
Teacher/Student Versions,Tablet PCs, and Inking
The “For Teachers” version of this presentation has 2 slides for each program.
The first slide only shows the program.The second shows the program, worked out solution, and output.
The “For Students” versiononly has 1 slide for eachprogram with no providedsolution or output. Students are expected to work out the solutions either on paper, or ideally they can “ink” directly on their laptops.
public class Output0501{
public static void main (String args[]){
for (int x = 1; x < 8; x++)System.out.println("x = " + x);
}}
public class Output0501{
public static void main (String args[]){
for (int x = 1; x < 8; x++)System.out.println("x = " + x);
}}
x Output
1 x = 1
2 x = 2
3 x = 3
4 x = 4
5 x = 5
6 x = 6
7 x = 7
public class Output0502{
public static void main (String args[]){
for (int x = 0; x <= 8; x+=2)System.out.println("x = " + x);
}}
public class Output0502{
public static void main (String args[]){
for (int x = 0; x <= 8; x+=2)System.out.println("x = " + x);
}}
x Output
0 x = 0
2 x = 2
4 x = 4
6 x = 6
8 x = 8
public class Output0503{
public static void main (String args[]){
for (int x = 100; x > 0; x-=20)System.out.println("x = " + x);
}}
public class Output0503{
public static void main (String args[]){
for (int x = 100; x > 0; x-=20)System.out.println("x = " + x);
}}
x Output
100 x = 100
80 x = 80
60 x = 60
40 x = 40
20 x = 20
public class Output0504{
public static void main (String args[]){
for (int x = 1; x < 100; x*=2)System.out.println("x = " + x);
}}
public class Output0504{
public static void main (String args[]){
for (int x = 1; x < 100; x*=2)System.out.println("x = " + x);
}}
x Output
1 x = 1
2 x = 2
4 x = 4
8 x = 8
16 x = 16
32 x = 32
64 x = 64
public class Output0505{
public static void main (String args[]){
for (int x = 729; x >= 1; x/=3)System.out.println("x = " + x);
}}
public class Output0505{
public static void main (String args[]){
for (int x = 729; x >= 1; x/=3)System.out.println("x = " + x);
}}
x Output
729 x = 729
243 x = 243
81 x = 81
27 x = 27
9 x = 9
3 x = 3
1 x = 1
public class Output0506{ public static void main (String args[]) {
int x = 100;if (x == 100)
System.out.println("Hello"); }}
public class Output0506{ public static void main (String args[]) {
int x = 100;if (x == 100)
System.out.println("Hello"); }} x x == 100? Output
100 true Hello
public class Output0507{ public static void main (String args[]) {
int x = 100;if (x > 100)
System.out.println("Hello");else
System.out.println("Goodbye"); }
}
public class Output0507{ public static void main (String args[]) {
int x = 100;if (x > 100)
System.out.println("Hello");else
System.out.println("Goodbye"); }
}x x > 100? Output
100 false Goodbye
public class Output0508{ public static void main (String args[]) {
int x = 100;if (x >= 100)
System.out.println("Hello");else
System.out.println("Goodbye"); }}
public class Output0508{ public static void main (String args[]) {
int x = 100;if (x >= 100)
System.out.println("Hello");else
System.out.println("Goodbye"); }}
x x >= 100? Output
100 true Hello
public class Output0509{
public static void main (String args[]){
int x = 0;int y = 0;for (y = 1; y <= 25; y++){
y+=5;System.out.println(y);
}}
}
public class Output0509{
public static void main (String args[]){
int x = 0;int y = 0;for (y = 1; y <= 25; y++){
y+=5;System.out.println(y);
}}
}
x y Output0 0
0 1
0 6 60 7
0 12 120 13
0 18 180 19
0 24 240 25
0 30 30
public class Output0510{ public static void main (String args[]) { int x = 0; int y = 0; for (x = 1; x > 1; x--) y++; System.out.println("y = " + y); }}
public class Output0510{ public static void main (String args[]) { int x = 0; int y = 0; for (x = 1; x > 1; x--) y++; System.out.println("y = " + y); }}
x y Output0 0 y = 0
public class Output0511{ public static void main (String args[]) { int x = 0; int y = 0; while (x < 10) { y = x + 2; x = y + 3; } System.out.println("y = " + y); }}
public class Output0511{ public static void main (String args[]) { int x = 0; int y = 0; while (x < 10) { y = x + 2; x = y + 3; } System.out.println("y = " + y); }}
y x Output0 0
2 5
7 10 y = 7
public class Output0512{ public static void main (String args[]) { int x = 2; do { if (x % 2 == 0)
x+=2; else
x++; } while (x < 10); System.out.println("x = " + x); }}
public class Output0512{ public static void main (String args[]) { int x = 2; do { if (x % 2 == 0)
x+=2; else
x++; } while (x < 10); System.out.println("x = " + x); }}
x x % 2 == 0 Output
2 true
4 true
6 true
8 true
10 true x = 10
public class Output0513{ public static void main (String args[]) { int x = 10; int y = 1; do { if (x % 2 == 0)
x += 5; else
y += 2; } while (y < x); System.out.println("x = " + x); }}
public class Output0513{ public static void main (String args[]) { int x = 10; int y = 1; do { if (x % 2 == 0)
x += 5; else
y += 2; } while (y < x); System.out.println("x = " + x); }}
x % 2 == 0 x y Output10 1
true 15
false 3
false 5
false 7
false 9
false 11
false 13
false 15 x = 15
public class Output0514{ public static void main (String args[]) { int x = 1; int y = 3; int z = 5; while (z > x + y) { x = y + z; y = x + z; z = x - y; } System.out.println("x = " + x); System.out.println("y = " + y); System.out.println("z = " + z); }}
public class Output0514{ public static void main (String args[]) { int x = 1; int y = 3; int z = 5; while (z > x + y) { x = y + z; y = x + z; z = x - y; } System.out.println("x = " + x); System.out.println("y = " + y); System.out.println("z = " + z); }}
x y z Output1 3 5
8 13 -5 x = 8y = 13z = -5
public class Output0515{ public static void main (String args[]) { int x = 168; int y = 90; int z = 0; do { z = x % y; if (z == 0)
System.out.println("y = " + y); else {
x = y; y = z;
} } while (z != 0); }}
public class Output0515{ public static void main (String args[]) { int x = 168; int y = 90; int z = 0; do { z = x % y; if (z == 0)
System.out.println("y = " + y); else {
x = y; y = z;
} } while (z != 0); }}
x y z Output
168 90 0
78
90 78 12
78 12 6
12 6 0 y = 6
public class Output0516{
public static void main(String args[]){
for (int x = 1; x < 3; x++){
for (int y = 1; y < 4; y++)System.out.print(x*y + " ");
System.out.println();}
}}
public class Output0516{
public static void main(String args[]){
for (int x = 1; x < 3; x++){
for (int y = 1; y < 4; y++)System.out.print(x*y + " ");
System.out.println();}
}}
x y Output
1 1 11 2 21 3 3
CRLF2 1 22 2 42 3 6
public class Output0517{
public static void main(String args[]){
int x = 5;int k,y;while (x <= 8){
y = 1;while (y <= 7){
y++;k = x + y;
}x += y;System.out.println("x = " + x);
} }
}
public class Output0517{
public static void main(String args[]){
int x = 5;int k,y;while (x <= 8){
y = 1;while (y <= 7){
y++;k = x + y;
}x += y;System.out.println("x = " + x);
} }
}
y k x Output5
12 73 84 95 106 117 128 13 13
x = 13
An OBOB is an Off By One Bug error.
This problem occurs at the boundaries of control structures.
Carefully check your program code at the boundaries to avoid OBOBs.
OBOB Warning!
public class Output0518{
public static void main(String args[]){
int a = 1, b = 2, c = 3, d = 4;while (a < b && c < d){
a = c + d;c = a + b;
}System.out.println(a + b + c + d);
}}
public class Output0518{
public static void main(String args[]){
int a = 1, b = 2, c = 3, d = 4;while (a < b && c < d){
a = c + d;c = a + b;
}System.out.println(a + b + c + d);
}}
a b c d Output
1 2 3 47 9
22
public class Output0519{
public static void main(String args[]){
int a;int b = 2;for (int k = 0; k < 4; k++){
a = k;b += k;while (a < b){
System.out.println(a + " " + b);a += b;b += k;
}}
}}
public class Output0519{
public static void main(String args[]){
int a;int b = 2;for (int k = 0; k < 4; k++){
a = k;b += k;while (a < b){
System.out.println(a + " " + b);a += b;b += k;
}}
}}
k a b Output
20 0 2 0 2
2 21 1 3 1 3 4 42 2 6 2 6
8 83 3 11 3 11
14 14
public class Output0520{
public static void main(String args[]){
int a = 0;int b = 10;for (int k = a; k < b; k++){
a++;b--;System.out.println(a + " " + b + " " + k);
}}
}
public class Output0520{
public static void main(String args[]){
int a = 0;int b = 10;for (int k = a; k < b; k++){
a++;b--;System.out.println(a + " " + b + " " + k);
}}
}
k a b Output
0 100 1 9 1 9 01 2 8 2 8 12 3 7 3 7 23 4 6 4 7 34 5 5 5 5 4 5
public class Output0521{
public static void main(String args[]){
int t = 0;for (int p = 1; p <= 4; p++)
for (int q = 1; q <= 4; q++)t++;
System.out.println("t = " + t);}
}
public class Output0521{
public static void main(String args[]){
int t = 0;for (int p = 1; p <= 4; p++)
for (int q = 1; q <= 4; q++)t++;
System.out.println("t = " + t);}
}
p q t Output0
1 1 12 23 34 4
2 1 52 63 74 8
3 1 92 103 114 12
4 1 132 143 154 16
t = 16
public class Output0521{
public static void main(String args[]){
int t = 0;for (int p = 1; p <= 4; p++)
for (int q = 1; q <= 4; q++)t++;
System.out.println("t = " + t);}
}
Shortcut
This question essentially boils down to determining how many times the t++;
statement is executed.
The outer loop repeats 4 times.The inner loop repeats 4 times.
4 * 4 = 16
public class Output0522{
public static void main(String args[]){
int n1, n2, n3;n1 = n2 = n3 = 1;for (int k = 3; k <= 10; k++){
n3 = n1 + n2;n1 = n2;n2 = n3;
}System.out.println("n3 = " + n3);
}}
public class Output0522{
public static void main(String args[]){
int n1, n2, n3;n1 = n2 = n3 = 1;for (int k = 3; k <= 10; k++){
n3 = n1 + n2;n1 = n2;n2 = n3;
}System.out.println("n3 = " + n3);
}}
k n3 n1 n2 Output1 1 1
3 2 1 24 3 2 35 5 3 56 8 5 87 13 8 138 21 13 219 34 21 3410 55 34 55
n3 = 55
public class Output0523{
public static void main(String args[]){
int a = 5;int b = 10;int c = 13;while (a < b && b != c){
a += 2;b++;if ((a + b) % 2 == 0)
c++;}System.out.println(a + " " + b + " " + c);
}}
public class Output0523{
public static void main(String args[]){
int a = 5;int b = 10;int c = 13;while (a < b && b != c){
a += 2;b++;if ((a + b) % 2 == 0)
c++;}System.out.println(a + " " + b + " " + c);
}}
a b a+b a+b even c a < b b != c Output5 10 15 false 13 true true7 11 18 true 14 true true9 12 21 false true true11 13 24 true 15 true true13 14 27 false true true15 15 30 true 16 false true
15 15 16
// THE MYSTERY PROGRAMimport java.util.Scanner;public class Output0524{
public static void main (String args[]) {
Scanner input = new Scanner(System.in);int a, b , c;System.out.print("Enter integer 1 ===>> ");a = input.nextInt();System.out.print("Enter integer 2 ===>> ");b = input.nextInt();if (a < b)
if (b < a)c = 1000;
elsec = 2500;
elseif (b > a)
c = 2000;else
c = 2500;System.out.println("c = " + c);
}}
// THE MYSTERY PROGRAMimport java.util.Scanner;public class Output0524{
public static void main (String args[]) {
Scanner input = new Scanner(System.in);int a, b , c;System.out.print("Enter integer 1 ===>> ");a = input.nextInt();System.out.print("Enter integer 2 ===>> ");b = input.nextInt();if (a < b)
if (b < a)c = 1000;
elsec = 2500;
elseif (b > a)
c = 2000;else
c = 2500;System.out.println("c = " + c);
}}
a b c Output? ? 2500 c = 2500
public class Output0525{
public static void main(String args[]){
for (int x = 1; x <= 4; x++){
for (int y = 1; y <= 4; y++)System.out.print((x + y) + " ");
System.out.println();}
}}
public class Output0525{
public static void main(String args[]){
for (int x = 1; x <= 4; x++){
for (int y = 1; y <= 4; y++)System.out.print((x + y) + " ");
System.out.println();}
}}
x y x + y1 1 21 2 31 3 41 4 52 1 32 2 42 3 52 4 63 1 43 2 53 3 63 4 74 1 54 2 64 3 74 4 8