This document discusses various Java flow control concepts including:
1. Rewriting an if/else statement as a switch statement and rewriting a for loop as a while loop.
2. The output of a method call based on the method code provided.
3. Writing a do/while loop to verify odd user input, using a for loop to count characters in a String, and rewriting another if/else as a switch statement.
This document discusses various Java flow control concepts including:
1. Rewriting an if/else statement as a switch statement and rewriting a for loop as a while loop.
2. The output of a method call based on the method code provided.
3. Writing a do/while loop to verify odd user input, using a for loop to count characters in a String, and rewriting another if/else as a switch statement.
1. Rewrite the following code segment using a switch statement:
1. if ( x == 0) { 2. y = 3; 3. System . out . println (" Zero "); 4. } else if ( x == 1) { 5. System . out . println (" One "); 6. } else { 7. System . out . println (" Other "); 8. } 2. Rewrite the following for loop as a while loop. 1. for ( int i = 0; i < MAX ; i ++) { 2. // loop body 3. } 3. Given the following method declaration, what would be the output from the method call mystery(10, 5);? 1. public void mystery ( int num1 , int num2 ) { 2. int total = num2 + num1 / num2 % num1 * num2 ; 3. for ( int i = 0; i < total ; i +=5) { 4. for ( int j = 1; j <= i ; j ++){ 5. if ( total % j == 0) 6. System . out . print ( j + " "); 7. } 8. } 9. } 4. Write a do loop that verifies that the user enters an odd value. You may assume that a Scanner object called input has already been created. 5. Write a code fragment that determines how many times the character ‘A’ appears in a String object called name. 6. Rewrite the following code segment using a switch statement: 1. if ( c == ’a ’ || c == ’b ’) { 2. System . out . println (" a or b "); 3. } else if ( c >= ’p ’ && c <= ’s ’) { 4. System . out . println (" p .. s "); 5. } else { 6. System . out . println (" Other "); 7. } 7. What output is produced by the following code fragment? 1. for ( int val = 200; val >= 0; val -= 1) 2. if ( val % 4 != 0) 3. System . out . println ( val ); 8. Transform the following while loop into an equivalent do loop (make sure it produces the same output). 1. int num = 1; 2. while ( num < 20) { 3. num ++; 4. System . out . println ( num ); 5. } 9. Transform the while loop from the previous exercise into an equivalent for loop (make sure it produces the same output). 10. Write a for loop to print the odd numbers from 1 to 99 (inclusive). 11. Write a for loop to print the multiples of 3 from 300 down to 3. 12. Write a code fragment that reads 10 integer values from the user and prints the highest value entered. 13. Write a method called powersOfTwo that prints the first 10 powers of 2 (starting with 2).The method takes no parameters and doesn’t return anything.
(Ebook) The Technology Procurement Handbook: A Practical Guide to Digital Buying by Sergii Dovgalenko ISBN 9781789662122, 1789662125 - Download the full ebook now for a seamless reading experience