Advertisement
Guest User

Untitled

a guest
Apr 25th, 2017
638
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.84 KB | None | 0 0
  1. Algorithm Workbench
  2.  
  3.  
  4. 6. Write an if statement that prints the message “The number is valid” if the variable
  5. grade is within the range 0 through 100.
  6. Answer:
  7. Scanner scan= new Scanner (System.in);
  8. System.out.println("Enter a number: ");
  9. int count = scan.nextInt();
  10. if (count >= 0 && count <= 100) {
  11. System.out.println("The number is valid");
  12. }else {
  13. System.out.println("The number is not valid. Try again");
  14. }
  15. 7. Write an if statement that prints the message “The number is valid” if the variable
  16. temperature is within the range −50 through 150.
  17. Answer:
  18. Scanner scan= new Scanner (System.in);
  19. System.out.println("Enter a number: ");
  20.  
  21. int count = scan.nextInt();
  22. if (count >= -51 && count <= 150) {
  23.  
  24. System.out.println("The number is valid");
  25. }else {
  26. System.out.println("The number is not valid. Try again");
  27. }
  28.  
  29. 8. Write an if statement that prints the message “The number is not valid” if the variable
  30. hours is outside the range 0 through 80.
  31. int hours = scan.nextInt();
  32. if (hours <= 0 || hours >= 80) {
  33. System.out.println("The number is not valid. Try again");
  34. }else {
  35. System.out.println("The number is valid");
  36. }
  37.  
  38.  
  39. 11.
  40.  
  41. a. q = x < y ? a + b : x * 2;
  42. b. q = x < y ? x * 2 : a + b;
  43. c. q = x < y ? 0 : 1;
  44.  
  45. __C__ if (x < y)
  46. q = 0;
  47. else
  48. q = 1;
  49.  
  50. __B__ if (x < y)
  51. q = a + b;
  52. else
  53. q = x * 2;
  54. __A__ if (x < y)
  55. q = x * 2;
  56. else
  57. q = a + b;
  58.  
  59.  
  60.  
  61.  
  62. Short Answers
  63.  
  64. 1. Explain what is meant by the phrase “conditionally executed.”
  65. Answer: It means that a statement will only be executed only if a particular condition is met.
  66.  
  67.  
  68. 2. Explain why a misplaced semicolon can cause an if statement to operate incorrectly.
  69. Answer: A misplaced semicolon can cause an if statement to operate incorrectly because the if statement is not complete without its conditionally executed statement
  70.  
  71.  
  72. 3. Why is it good advice to indent all the statements inside a set of braces?
  73. Answer:
  74.  
  75. 4. What happens when you compare two String objects with the == operator?
  76. Answer: The “==” operator compares the references of two objects in memory. It returns true if both objects point to exact same location in memory.
  77.  
  78. 5. Explain the purpose of a flag variable. Of what data type should a flag variable be?
  79. Answer: When the flag variable is set to false, it indicates the condition does not exist. When the flag is set to true, it means the condition does exist. The flag variable should be true.
  80.  
  81. 6. What risk does a programmer take when not placing a trailing else at the end of an
  82. if-else-if statement?
  83. Answer: The other statements will run separately
  84.  
  85.  
  86. 7. Briefly describe how the && operator works.
  87. Answer: The && operator takes two Boolean expressions as operands and creates a Boolean expression that is true only when both subexpressions are true. If the expression of the left of the && operator is false, the expression on the right side will not be checked.
  88.  
  89. 8. Briefly describe how the || operator works.
  90. Answer: The || operator takes two boolean expressions as operands and creates a boolean expression that is true when either of the subexpressions is true
  91.  
  92. 9. Why are the relational operators called “relational”?
  93. Answer: The relational operators describes the relationship where one side is greater
  94.  
  95. 10. When does a constructor execute? What is its purpose?
  96.  
  97. Answer: Constructors are used to initialize the instances of a class. You use a constructor to create new objects often with parameters specifying the initial state or other important information about the object.
  98.  
  99.  
  100.  
  101. Other problems
  102.  
  103. 1. public static void pc6(){
  104. 2. System.out.println("Please enter time in number of seconds");
  105. 3. int timeInSeconds = keyboard.nextInt();
  106. 4. //check for years first
  107. 5. int years = timeInSeconds/31536000;
  108. 6. int secondsRemainingAfterAYear = timeInSeconds%31536000;
  109. 7.
  110. 8. int months = secondsRemainingAfterAYear/2628000;
  111. 9. int secondsRemainingAfterAMonth = timeInSeconds%2628000;
  112. 10.
  113. 11. int weeks = secondsRemainingAfterAMonth/604800;
  114. 12. int secondsRemainingAfterAWeek = timeInSeconds%604800;
  115. 13.
  116. 14. int day = secondsRemainingAfterAWeek/86400;
  117. 15. int secondsRemainingAfterADay = timeInSeconds%86400;
  118. 16.
  119. 17. int hours = secondsRemainingAfterADay / 3600;
  120. 18. int secondsRemainingAfterAnHour = secondsRemainingAfterADay%3600;
  121. 19.
  122. 20. int mins = secondsRemainingAfterAnHour/60;
  123. 21. int seconds = secondsRemainingAfterAnHour%60;
  124. 22.
  125. 23. System.out.printf("%d years %d months %d weeks %d days %d hours %d mins %d secs \n",years,months,weeks,day,hours,mins,seconds);
  126. 24.
  127. 25. //expand this to weeks, months and year
  128. 26.
  129. 27. }
  130. 28. public static void pc8(){
  131. 29. System.out.println("Please enter the number of packages purchased");
  132. 30. int packages = keyboard.nextInt();
  133. 31. int amount = 99;
  134. 32. if (packages >= 100){
  135. 33. System.out.println("Your discount is %50 and the total amount do is $" + (packages*amount -(packages*amount)*.50));
  136. 34. }else if(packages > 50){
  137. 35. System.out.println("Your discount is %40 and the total amount do is $" + (packages*amount -(packages*amount)*.40));
  138. 36. }else if(packages > 20){
  139. 37. System.out.println("Your discount is %30 and the total amount do is $" + (packages*amount -(packages*amount)*.30));
  140. 38. }else if(packages > 10){
  141. 39. System.out.println("Your discount is %20 the total amount do is $" + (packages*amount -(packages*amount)*.20));
  142. 40. }else if(packages < 10){
  143. 41. System.out.println("Your order doesn't qualify for a discount. The total amount due is $" +(packages*amount));
  144. 42. }
  145. 43. }
  146. 44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement