Chapter 5

अब Quizwiz के साथ अपने होमवर्क और परीक्षाओं को एस करें!

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();

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: aa ab ac ad ae ba bb ... 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 * ** ***

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 multiples of 5 that are less than 175, separated by exactly one space.

for (int k = 5; k < 175; k += 5) System.out.print(k + " ");

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 k = 6; k <= 200; k += 6) System.out.print(k + " ");

Write a for loop that prints all the even integers from 80 through 20 inclusive, separated by exactly one space.

for (int k = 80; k >= 20; k -= 2) System.out.print(k + " ");

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, 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(); }

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();

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();

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; Correct

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=50; while (k>0) { total+=k*k; k--; }

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()); if (input.hasNextLine()) System.out.print(" "); } System.out.println(); Correct

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();


संबंधित स्टडी सेट्स

Exam 2 Pharmacology Study Guide Part two 48,49,54,55

View Set

Organizational Behavior - Chapter 9

View Set

Pre-Lab #5: Introduction to Friction

View Set

Embryonic to Adult Brain Derivatives

View Set

Maternal-Newborn Ch. 21 Complications occurring before labor and delivery

View Set

Midterm Part 1 & 2: Unit 1 - Unit 15

View Set