MyprogrammingLab 5.2

Ace your homework & exams now with 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.

count = 0; longest =0; String myString = new String(); while (input.hasNext()){ myString = input.next(); if (myString.length() == longest) count++; else if (myString.length() > longest){ longest = myString.length(); count = 1; } }

Given a Scanner reference variable named input that has been associated with an input source consisting of a sequence of lines of text and an int variable named count, write the code necessary to count the lines of text in the input source and place that value into count.

count = 0; while(input.hasNext()){ input.nextLine(); count++; }

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.

do { n = stdin.nextInt(); } while (n > 10 || n < 1);

Given a String variable response that has already been declared, write some code that repeatedly reads a value from standard input into response until at last a Y or y or N or n has been entered. ASSUME the availability of a variable, stdin, that references a Scanner object associated with standard input.

do { response = stdin.next(); } while (!response.equals("Y") && !response.equals("y") && !response.equals("N") && !response.equals("n"));

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. There should NOT be a trailing space at the end of the line.

if (input.hasNext()) System.out.print(input.nextLine()); while (input.hasNext()){ System.out.print(" " + input.nextLine()); }

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.

int firstNumber, secondNumber = -1, cDuplicate = 0; firstNumber = stdin.nextInt(); while (stdin.hasNextInt() && firstNumber >= 0) { if (firstNumber == secondNumber) { cDuplicate++; } else secondNumber = firstNumber; firstNumber = stdin.nextInt(); } System.out.println(cDuplicate);

Write a loop that reads strings from standard input where the string is either "land", "air", or "water". The loop terminates when "xxxxx" (five x characters) is read in. Other strings are ignored. After the loop, your code should print out 3 lines: the first consisting of the string "land:" followed by the number of "land" strings read in, the second consisting of the string "air:" followed by the number of "air" strings read in, and the third consisting of the string "water:" followed by the number of "water" strings read in. Each of these should be printed on a separate line. ASSUME the availability of a variable, stdin, that references a Scanner object associated with standard input.

int land = 0; int air = 0; int water = 0; String str = new String(); while(!(str.equals("xxxxx"))) { str = stdin.next(); if(str.equals("land")) land++; else if(str.equals("air")) air++; else if(str.equals("water")) water++; } System.out.println("land:" + land); System.out.println("air:" + air); System.out.println("water:" + water);

Write a loop that reads positive integers from standard input, printing out those values that are even, 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.

int num=1; while (num>0){ num = stdin.nextInt(); if ((num % 2) == 0 && num > 0 ){ System.out.print(num + " "); } }

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.

int number = 0; while (stdin.hasNextInt() && number >= 0){ number =stdin.nextInt(); if (number > 100) System.out.print(number + " "); }

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, and a 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.

int odd=0; int oddCount=0; int evenCount=0; int even=0; int i=1; while (i>0){ i = stdin.nextInt(); if ((i % 2)==0 && (i>0)){ even+=i; evenCount++; } if ((i % 2)!=0 && (i>0)){ odd+=i; oddCount++; } } System.out.print(even + " " + odd + " " + evenCount + " " + oddCount);

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 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 and do not modify the value of n.

k = 0 ; total = 0 ; while ( k <= n ) { total = total + ( k * k * k) ; k++ ; }

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

Consider this data sequence: "fish bird reptile reptile bird bird bird mammal fish". Let's define a SINGLETON to be a data element that is not repeated immediately before or after itself in the sequence. So, here there are four SINGLETONs (the first appearance of "fish", the first appearance of "bird", "mammal", and the second appearance of "fish"). Write some code that uses a loop to read a sequence of words, terminated by the "xxxxx". The code assigns to the variable n the number of SINGLETONs that were read. (For example in the above data sequence it would assign 4 to n. Assume that n has already been declared. but not initialized. Assume that there will be at least one word before the terminating "xxxxx". ASSUME the availability of a variable, stdin, that references a Scanner object associated with standard input.

n = 0; String word1, word2, word3; word3 = stdin.next(); word2 = stdin.next(); word1 = stdin.next(); if (!(word3.equals(word2))) n++; if (!(word2.equals(word3)) && (!(word2.equals(word1)))) n++; while (!(word1.equals("xxxxx")) && (stdin.hasNext())) { word3 = word2; word2 = word1; word1 = stdin.next(); if (!(word2.equals(word3)) && (!(word2.equals(word1)))) n++; }

Write a statement that increases the value of the int variable total by the value of the int variable amount. That is, add the value of amount to total and assign the result to total.

total += amount;

Given a Scanner reference variable named input that has been associated with an input source consisting of a sequence of integers and an int variable named total, write the code necessary to add all the integers in the input source and place their sum into total.

total = 0; while(input.hasNextInt()){ total += input.nextInt( ); }

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; for (k=1;k<=50; k++) total+=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 it out on its own line.

while (input.hasNext()){ System.out.println(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--; }

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, the sum of all the even integers read. Declare any variables that are needed. ASSUME the availability of a variable, stdin, that references a Scanner object associated with standard input.

int odd=0; int even=0; int i=1; while (i>0){ i = stdin.nextInt(); if ((i % 2)==0 && (i>0)){ even+=i; } if ((i % 2)!=0 && (i>0)){ odd+=i; } } System.out.print(even);

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, separated by a space and on a single line, the sum of all the even integers read and the sum of all the odd integers read. Declare any variables that are needed. ASSUME the availability of a variable, stdin, that references a Scanner object associated with standard input.

int odd=0; int even=0; int i=1; while (i>0){ i = stdin.nextInt(); if ((i % 2)==0 && (i>0)){ even+=i; } if ((i % 2)!=0 && (i>0)){ odd+=i; } } System.out.print(even + " " + odd);


Related study sets

chapter 17 anatomy and physiology

View Set

T/F and Multiple Choice (Final MGMT 466)

View Set

Solving Equations (ALL ONE SIDE)

View Set

Int'l Political Economy (unit 1)

View Set

exam #2 chap 15, 35, 36, 37, 38 medsurgII

View Set