akosiraff

Download Lab05b

Dec 5th, 2017
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.95 KB | None | 0 0
  1.  
  2. Download: https://solutionzip.com/downloads/lab05b/
  3. Lab Questions
  4. Big Java, Late Objects / Java for Everyone, 2e
  5. Chapter Number: 4 Loops
  6. 4) You can test to see if an integer, x, is even or odd using the Boolean expression (x / 2) * 2 == x. Integers that are even make this expression true, and odd integers make the expression false.
  7. Use a for loop to iterate five times. In each iteration, request an integer from the user. Print each integer the user types, and whether it is even or odd. Keep up with the number of even and odd integers the user types, and print “Done” when finished, so the user won’t try to type another integer. Finally, print out the number of even and odd integers that were entered.
  8. 6.1) Use nested for loops to produce the following output
  9. 1 1
  10. 1 2
  11. 1 3
  12. 1 4
  13. 2 1
  14. 2 2
  15. 2 3
  16. 2 4
  17. Let the outer loop print the numbers in the left column, and the inner loop print the numbers in the right column. In each iteration, print the loop control variables to produce the output.
  18. 6.3) Repeat Lab 6.1 using nested do loops.
  19. 7.1) Use nested for loops to produce the following output:
  20. X
  21. XX
  22. XXX
  23. XXXX
  24. XXXXX
  25. The outer loop can control the number of rows that will be printed. The inner loop can control the number of X’s that print on a single line. The trick is to notice that there is a relationship between the row number and the number of X’s in the row. This relationship allows you to use the outer loop control variable to control the inner loop.
  26. 7.2) NOT ANSWERED
  27. 10.1) A variable that counts the iterations of a loop is called a loop index or loop control variable. In the preceding examples nyear served as an index, counting the number of years to the next millennium. This type of loop is frequently written using a for loop.
  28. for (initialization; condition; update)
  29. {
  30. statement
  31. }
  32. Write a program controlled by two (non-nested) for loops that produces the following listing of inclusive dates, from the fifth century B.C. through the fifth century A.D.
  33. Century 5 BC 400-499
  34. Century 4 BC 300-399
  35. Century 3 BC 200-299
  36. Century 2 BC 100-199
  37. Century 1 BC 1-99
  38. Century 1 AD 1-99
  39. Century 2 AD 100-199
  40. Century 3 AD 200-299
  41. Century 4 AD 300-399
  42. Century 5 AD 400-499
  43. 10.2) Write the ListCenturies program using a single loop for (i = -5; i <= 5; i++) with an if statement in the body of the loop. 11.1) One loop type might be better suited than another to a particular purpose. The following usages are idiomatic: for Known number of iterations while Unknown number of iterations do At least one iteration Convert the following while loop to a do loop. public class PrintSum { public static void main(String[] args) { Scanner in = new Scanner(System.in); int sum = 0; int n = 1; while (n != 0) { System.out.print("Please enter a number, 0 to quit: "); n = in.nextInt(); if (n != 0) { sum = sum + n; System.out.println("Sum = " + sum); } } } } 12) Convert this while loop to a for loop. /** Program to compute the first integral power to which 2 can be raised that is greater than that multiple of a given integer. */ public class CountPowerOf2 { public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.print("Please enter a number, 0 to quit: "); int n = in.nextInt(); int i = 1; while (n * n > Math.pow(2, i))
  44. {
  45. i++;
  46. }
  47. System.out.println(“2 raised to ” + i
  48. + ” is the first power of two greater than ” + n + ” squared”);
  49. }
  50. }
  51. }
  52. 13) Convert this for loop to a while loop:
  53. public static void main(String[] args)
  54. {
  55. for (int i = 1; i <= 10; i++) { System.out.println(i + " squared equals " + i * i); } } 15) To generate random numbers, you construct an object of the Random class, and then apply one of the following methods: nextInt(n): A random integer between the integers 0 (inclusive) and n (exclusive) nextDouble(): A random floating-point number between 0 (inclusive) and 1 (exclusive) Write a program that simulates the drawing of one card (for example, an ace of spades).
  56.  
  57. Download: https://solutionzip.com/downloads/lab05b/
Add Comment
Please, Sign In to add comment