Advertisement
willieshi232

Untitled

Oct 19th, 2015
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.96 KB | None | 0 0
  1. 2a. true
  2. 2b. false
  3. 2c. true
  4. 2d. false
  5. 2e. true
  6. 2f. false
  7. 2g. false
  8. 2h. true
  9. 2i. true
  10. 4.
  11. line 5: if statement should use () parentheses, not {} brackets
  12. line 5: = should be ==
  13. line 5: smaller is out of scope here
  14. line 10: void should be int
  15. line 13: => should be >= (or better yet, no if test is needed)
  16. line 16: should not write variable's type of int when returning it
  17. line 16: int smaller is out of scope here (declare outside if or return directly)
  18. 6a. 13 21
  19. 6b. 5 6
  20. 6c. 6 5
  21. 6d. 7 11
  22. 8. The program's brace placement makes it so if number % 3 == 0 the number isn't odd, if I had number as 9 then it wouldn't read 9 as odd. The else statement is nested with the closest if statement so to fix it you have to do the following
  23. if (number % 2 == 0) {
  24. if (number % 3 == 0) {
  25. System.out.println("Divisible by 6.");
  26. }
  27. } else {
  28. System.out.println("Odd.");
  29. }
  30. 10.
  31. a = 2;
  32. if (x < 30) {
  33. x++;
  34. }
  35. System.out.println("Java is awesome! " + x);
  36. 12.import java.util.*;
  37. public class Bills {
  38. public static void main(String[] args) {
  39. Scanner pikachu = new Scanner(System.in);
  40. int Bills1 = Bills(pikachu, "John");
  41. int Bills2 = Bills(pikachu, "Jane");
  42. System.out.println("John needs " + Bills1 + " bills");
  43. System.out.println("Jane needs " + Bills2 + " bills");
  44. }
  45.  
  46. public static int Bills(Scanner console, String name) {
  47. System.out.print("How much will " + name + " be spending? ");
  48. double amount = pikachu.nextDouble();
  49. System.out.println();
  50. int numBills = (int) (amount / 20.0);
  51. if (numBills * 20.0 < amount) {
  52. numBills++;
  53. }
  54. return numBills;
  55. }
  56. }
  57. 14.
  58. Scanner pikachu = new Scanner(System.in);
  59. System.out.print("Enter a card: ");
  60. String rank = pikachu.next();
  61. String suit = pikachu.next();
  62. if (rank.equals("2")) {
  63. rank = "Two";
  64. } else if (rank.equals("3")) {
  65. rank = "Three";
  66. } else if (rank.equals("4")) {
  67. rank = "Four";
  68. } else if (rank.equals("5")) {
  69. rank = "Five";
  70. } else if (rank.equals("6")) {
  71. rank = "Six";
  72. } else if (rank.equals("7")) {
  73. rank = "Seven";
  74. } else if (rank.equals("8")) {
  75. rank = "Eight";
  76. } else if (rank.equals("9")) {
  77. rank = "Nine";
  78. } else if (rank.equals("10")) {
  79. rank = "Ten";
  80. } else if (rank.equals("J")) {
  81. rank = "Jack";
  82. } else if (rank.equals("Q")) {
  83. rank = "Queen";
  84. } else if (rank.equals("K")) {
  85. rank = "King";
  86. } else { // rank.equals("A")
  87. rank = "Ace";
  88. }
  89.  
  90. if (suit.equals("C")) {
  91. suit = "Clubs";
  92. } else if (suit.equals("D")) {
  93. suit = "Diamonds";
  94. } else if (suit.equals("H")) {
  95. suit = "Hearts";
  96. } else { // suit.equals("S")
  97. suit = "Spades";
  98. }
  99.  
  100. System.out.println(rank + " of " + suit);
  101. 16.
  102. public static int countFactors(int n) {
  103. int count = 0;
  104. for (int i = 1; i <= n; i++) {
  105. if (n % i == 0) {
  106. count++;
  107. }
  108. }
  109. return count;
  110. }
  111. 18.6.800000000000001 roundoff error
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement