Module 5
Given a Scanner reference variable named input that has been associated with an input source consisting of a sequence of strings and two int variables, count and longest, write the code necessary to examine all the strings in the input source and determine how long the longest string (or strings are).That value should be assigned to longest.The number of strings that are of that length should be assigned to count.For a hint on this quiz, please check https://liveexample.pearsoncmg.com/supplement/REVELProjectHint.pdf.
count = 0; longest = 0; while (input.hasNext()) { String s = input.next(); if (s.length() > longest) { longest = s.length(); count = 1; } else if (s.length() == longest) count++; }
Given an int variable n that has already been declared and initialized to a positive value,use a do...while loop to print a single line consisting of n asterisks. Use no variables other than n.
do { System.out.print('*'); } while (--n > 0); System.out.println();
What is count after the loop is finished? int count = 1; do { count += 3; } while (count < 5);
7
for loop
A loop construct that begins with the keyword for.
while loop
A loop construct that begins with the keyword while
infinite loop
A loop that runs indefinitely due to a bug in the loop.
sentinel value
A special input value that signifies the end of the input.
loop
A structure that controls repeated executions of a block of statements.
The loop continuation is checked after the loop body is executed.
posttest loop
The loop continuation is checked before the loop body is executed.
pretest loop
How many times is the following loop executed? for (int count = 1; count < 5; count += 3) { // Execute the body }
2
int count = 1; while (count < 5) { count += 3; }
2
The following loop displays _______________.for (int i = 1; i <= 10; i++) {System.out.print(i + " ");i++;}
1 3 5 7 9 Explanation: In this for loop, i is initialized to 1. i++ adds 1 to i at the end of the iteration. In the action-after-each-iteration, i is again incremented by 1. So, i is in effect incremented twice. So, i is 1, then 3, 5, 7, and 9.
How many times will the following code print "Welcome to Java"? int count = 0;do {System.out.println("Welcome to Java");count++;} while (count < 10);
10
How many times will the following code print "Welcome to Java"? int count = 0;do {System.out.println("Welcome to Java");} while (++count < 10);
10
What is the value in count after the following loop is executed? int count = 0;do {System.out.println("Welcome to Java");} while (count++ < 9);System.out.println(count);
10
What is y after the following for loop statement is executed? int y = 0;for (int i = 0; i < 10; ++i) { y += 1;}
10
How many times will the following code print "Welcome to Java"? int count = 0;do {System.out.println("Welcome to Java");} while (count++ < 10);
11
What is the output of the following code? for (int i = 1; i < 10; i += 2) { System.out.printf("%1d", i); }
13579
How many times is the following loop executed? int count = 1; do { count += 3; } while (count < 5);
2
How many times is the println statement executed? for (int i = 0; i < 10; i++) for (int j = 0; j < i; j++)System.out.println(i * j)
45
Analyze the following code. int count = 0;while (count < 100) {// Point ASystem.out.println("Welcome to Java!");count++;// Point B}// Point C
A. count < 100 is always true at Point A E. count < 100 is always false at Point C
continue statement
Break out of the current iteration.
break statement
Break out of the current loop.
How many times will the following code print "Welcome to Java"? int count = 0;while (count < 10) {System.out.println("Welcome to Java");count++;}
C. 10
How many times will the following code print "Welcome to Java"? int count = 0;while (count++ < 10) {System.out.println("Welcome to Java");}
C. 10
Which of the following loops correctly computes 1/2 + 2/3 + 3/4 + ... + 99/100? A:double sum = 0;for (int i = 1; i <= 99; i++) {sum = i / (i + 1);}System.out.println("Sum is " + sum);B:double sum = 0;for (int i = 1; i < 99; i++) {sum += i / (i + 1);}System.out.println("Sum is " + sum);C:double sum = 0;for (int i = 1; i <= 99; i++) {sum += 1.0 * i / (i + 1);}System.out.println("Sum is " + sum);D:double sum = 0;for (int i = 1; i <= 99; i++) {sum += i / (i + 1.0);}System.out.println("Sum is " + sum);E:double sum = 0;for (int i = 1; i < 99; i++) {sum += i / (i + 1.0);}System.out.println("Sum is " + sum);
CD
nested loop
Consists of an outer loop and one or more inner loops. Each time the outer loop is repeated, the inner loops are reentered, and all required iterations are performed.
Analyze the following code. double sum = 0;for (double d = 0; d < 10; sum += sum + d) { d += 0.1;}
D. The program compiles and runs fine.
Analyze the following statement: double sum = 0;for (double d = 0; d < 10;) {d += 0.1;sum += sum + d;}
D. The program compiles and runs fine.
Which of the following loops prints "Welcome to Java" 10 times? A:for (int count = 1; count <= 10; count++) {System.out.println("Welcome to Java");}B:for (int count = 0; count < 10; count++) {System.out.println("Welcome to Java");}C:for (int count = 1; count < 10; count++) {System.out.println("Welcome to Java");}D:for (int count = 0; count <= 10; count++) {System.out.println("Welcome to Java");}
E. AB
is to redirect the input from a data file rather from the keyboard. The file is specified at the command line after the symbol <.
Input Redirection
input redirection
Obtain the input from a file instead of from the console.
iteration
One time execution of the loop body.
is to redirect the output to a data file rather to the console. The file is specified at the command line after the symbol >.
Output Redirection
is a special value that signifies the end of the input.
Sentinel Value
A loop that uses a sentinel value to control its execution is called a sentinel-controlled loop.
Sentinel-Controlled Loop
posttest loop
The loop continuation condition is checked after the loop body is executed.
pretest loop
The loop continuation condition is checked before the loop body is executed.
loop body
The part of the loop that contains the statements to be repeated.
Analyze the following code: public class Test { public static void main (String args[]) {int i = 0;for (i = 0; i < 10; i++);System.out.println(i + 4);}}
The program compiles despite the semicolon (;) on the for loop line, and displays 14. The program compiles despite the semicolon (;) on the for loop line, and displays 14.
Analyze the following fragment: double sum = 0;double d = 0;while (d != 10.0) {d += 0.1;sum += sum + d;}
The program may not stop because of the phenomenon referred to as numerical inaccuracy for operating with floating-point numbers.
output redirection
Write the output to a file instead of to the console.
Do the following two statements in (I) and (II) result in the same value in sum? (I):for (int i = 0; i < 10; ++i) {sum += i;}(II):for (int i = 0; i < 10; i++) {sum += i;}
Yes
To add 0.01 + 0.02 + ... + 1.00, what order should you use to add the numbers to get better accuracy?
add 0.01, 0.02, ..., 1.00 in this order to a sum variable whose initial value is 0. Explanation: Adding the numbers in the order specified in A will result in a more accurate result than in B, because two numbers to be added will be about the same size. Get Statistics
off
by-one error - A common error in the loop because the loop is executed one more or one less time than it should have been.
Write a loop that displays all possible combinations of two letters where the letters are 'a', or 'b', or 'c', or 'd', or 'e'.The combinations should be displayed in ascending alphabetical order:aaabacadaebabb...ee
for (char i = 'a'; i <= 'e'; i++) for (char j = 'a'; j <= 'e'; j++) { System.out.print(i); System.out.println(j); }
Assume that the int variables i and j have been declared, and that n has been declared and initialized.Using for loops (you may need more than one), write code that will cause a triangle of asterisks of size n to be output to the screen.For example, if n = 3 , the triangle is as follows:******
for (i = 1; i <= n; i++) { for(j = 1; j <= i; j++) System.out.print('*'); System.out.println(); }
Write a for loop that prints in ascending order all the positive integers less than 200 that are divisible by both 2 and 3, separated by exactly one space.
for (int i = 1; i < 200; i++) { if (i % 2 == 0 && i % 3 == 0) { System.out.print(i + " "); } }
Write a for loop that prints in ascending order all the positive multiples of 5 that are less than 175, separated by exactly one space.
for (int n = 5; n < 175; n = n + 5) System.out.print(n + " ");
Write a for loop that prints all the even integers from 80 through 20 inclusive, separated by exactly one space.
for (int n = 80; n >= 20; n = n - 2) System.out.print(n + " ");
is a loop that runs forever due to an error in the code.
infinite loop
Consider this data sequence: "3 11 5 5 5 2 4 6 6 7 3 -8".Any value that is the same as the immediately preceding value is considered a consecutive duplicate.In this example, there are three such consecutive duplicates: the 2nd and 3rd 5s and the second 6.Note that the last 3 is not a consecutive duplicate because it was preceded by a 7.Write some code that uses a loop to read such a sequence of non-negative integers, terminated by a negative number.When the code finishes executing, the number of consecutive duplicates encountered is printed. In this case, 3 would be printed.Assume the availability of a variable, stdin, that references a Scanner object associated with standard input. That is, stdin = new Scanner(System.in); is given.
int n, prev, consecdups = 0; prev = stdin.nextInt(); n = stdin.nextInt(); while (n >= 0) { if (n == prev) consecdups++; prev = n; n = stdin.nextInt(); } System.out.println(consecdups);
Write a loop that reads positive integers from standard input and that terminates when it reads an integer that is not positive.After the loop terminates, it prints out, on a line by itself and separated by spaces,the sum of all the even integers read,the sum of all the odd integers read,a count of the number of even integers read, anda count of the number of odd integers read,all separated by at least one space.Declare any variables that are needed.Assume the availability of a variable, stdin, that references a Scanner object associated with standard input. That is, stdin = new Scanner(System.in); is given.
int x; int esum = 0, ecount = 0; int osum = 0, ocount = 0; x = stdin.nextInt(); while (x > 0) { if (x % 2 == 0) { esum += x; ecount++; } else { osum += x; ocount++; } x = stdin.nextInt(); } System.out.println(esum + " " + osum + " " + ecount + " " + ocount);
Write a loop that reads positive integers from standard input, printing out those values that are greater than 100, each followed by a space, and that terminates when it reads an integer that is not positive.Declare any variables that are needed.Assume the availability of a variable, stdin, that references a Scanner object associated with standard input. That is, stdin = new Scanner(System.in); is given.
int x; x = stdin.nextInt(); while (x > 0) { if (x > 100) System.out.print(x + " "); x = stdin.nextInt(); }
is one time execution of the loop body.
iteration
Given an int variable k that has already been declared, use a do...while loop to print a single line consisting of 53 asterisks. Use no variables other than k.
k = 0; do { System.out.print('*'); k++; } while(k < 53); System.out.println();
Given an int variable k that has already been declared, use a while loop to print a single line consisting of 88 asterisks. Use no variables other than k.
k = 1; while (k <= 88) { System.out.print('*'); k++; } System.out.println();
is the part of the body that contains the statements to be repeated.
loop body
is a Boolean expression that controls the execution of the loop.
loop continuation condition
Given an int variable n that has already been declared, write some code that repeatedly reads a value into n until at last a number between 1 and 10 (inclusive) has been entered.Assume the availability of a variable, stdin, that references a Scanner object associated with standard input. That is, stdin = new Scanner(System.in); is given.
n = -1; while (n < 1 || n > 10) n = stdin.nextInt();
is an error in the program that causes the loop body to be executed one more or less time.
off-by-one
Given int variables k and total that have already been declared, use a for loop to compute the sum of the squares of the first 50 counting numbers, and store this value in total.Thus your code should put 1*1 + 2*2 + 3*3 +... + 49*49 + 50*50 into total.Use no variables other than k and total.
total = 0; for (k = 1; k <= 50; k++) total += k * k;
Given an int variable n that has been initialized to a positive value and, in addition, int variables k and total that have already been declared, use a for loop to compute the sum of the cubes of the first n whole numbers, and store this value in total.Use no variables other than n, k, and total.
total = 0; for (k = 1; k <= n; k++) total += k * k * k;
Given int variables k and total that have already been declared, use a do...while loop to compute the sum of the squares of the first 50 counting numbers, and store this value in total.Thus your code should put 1*1 + 2*2 + 3*3 +... + 49*49 + 50*50 into total.Use no variables other than k and total.
total = 0; k = 50; do { total += k * k; k--; } while (k > 0);
Given an int variable n that has been initialized to a positive value and, in addition, int variables k and total that have already been declared, use a do...while loop to compute the sum of the cubes of the first n whole numbers, and store this value in total.Use no variables other than n, k, and total.
total = 0; k = n; do { total += k * k * k; k--; } while (k > 0);
Given int variables k and total that have already been declared, use a while loop to compute the sum of the squares of the first 50 counting numbers, and store this value in total.Thus your code should put 1*1 + 2*2 + 3*3 +... + 49*49 + 50*50 into total. Use no variables other than k and total.
total=0; k=1; while(k<=50) { total = total + (k * k); k=k + 1; }
Given a Scanner reference variable named input that has been associated with an input source consisting of a sequence of lines, write the code necessary to read in every line and print them all out on a single line, separated by a space.Hint: Use input.nextLine() to read a line and use input.hasNextLine() to test if the input has a next line available to read.
while (input.hasNextLine()) { System.out.print(input.nextLine() + " "); }
Given an int variable n that has already been declared and initialized to a positive value, use a while loop to print a single line consisting of n asterisks. Use no variables other than n.
while (n > 0) { System.out.print('*'); n--; } System.out.println();
do
while loop - A loop construct that begins with the keyword do.