Advertisement
Ccuevas3410

Untitled

Mar 25th, 2017
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.26 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. 1Explain what is meant by the phrase “conditionally executed.”
  38. Answer: It means that a statement will only be executed only if  a particular condition is met.
  39.  
  40.  
  41. 2Explain why a misplaced semicolon can cause an if statement to operate incorrectly.
  42. Answer: A misplaced semicolon can cause an if statement to operate incorrectly because the if statement is not complete without its conditionally executed statement
  43.  
  44.  
  45. 3Why is it good advice to indent all the statements inside a set of braces?
  46.  
  47. 4What happens when you compare two String objects with the == operator?
  48. Answer: The “==” operator compares the references of two objects in memory. It returns true if both objects point to exact same location in memory.
  49.  
  50. 5Explain the purpose of a flag variable. Of what data type should a flag variable be?
  51.             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.
  52.  
  53. 6. What risk does a programmer take when not placing a trailing else at the end of an
  54. if-else-if statement?
  55. Answer:
  56.  
  57. 7Briefly describe how the && operator works.
  58. 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.
  59.  
  60. 8Briefly describe how the || operator works.
  61. Answer: The || operator takes two boolean expressions as operands and creates a boolean expression that is true when either of the subexpressions is true
  62.  
  63. 9Why are the relational operators called “relational”?
  64. Answer: The relational operators describes the relationship where one side is greater
  65.  
  66. 10. When does a constructor execute? What is its purpose?
  67.  
  68. 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
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement