Advertisement
Crenox

CodingBat Problems & Solutions

Sep 16th, 2015
222
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.92 KB | None | 0 0
  1. The parameter weekday is true if it is a weekday, and the parameter vacation is true if we are on vacation. We sleep in if it is not a weekday or we're on vacation. Return true if we sleep in.
  2. -------------------------------------------------------------------------------------------------------------------------------
  3. public boolean sleepIn(boolean weekday, boolean vacation)
  4. {
  5. boolean sleep = false;
  6.  
  7. if(!weekday == true || vacation == true)
  8. {
  9. sleep = true;
  10. }
  11.  
  12. return sleep;
  13. }
  14. -------------------------------------------------------------------------------------------------------------------------------
  15. We have two monkeys, a and b, and the parameters aSmile and bSmile indicate if each is smiling. We are in trouble if they are both smiling or if neither of them is smiling. Return true if we are in trouble.
  16. -------------------------------------------------------------------------------------------------------------------------------
  17. public boolean monkeyTrouble(boolean aSmile, boolean bSmile)
  18. {
  19. boolean inTrouble = false;
  20.  
  21. if ((aSmile == true && bSmile == true) || (aSmile == false && bSmile == false))
  22. {
  23. inTrouble = true;
  24. }
  25.  
  26. return inTrouble;
  27. }
  28. -------------------------------------------------------------------------------------------------------------------------------
  29. Given two int values, return their sum. Unless the two values are the same, then return double their sum.
  30. -------------------------------------------------------------------------------------------------------------------------------
  31. public int sumDouble(int a, int b) {
  32. int sum = a + b;
  33.  
  34. if (a == b)
  35. {
  36. sum *= 2;
  37. }
  38.  
  39. return sum;
  40. }
  41. -------------------------------------------------------------------------------------------------------------------------------
  42. Given an int n, return the absolute difference between n and 21, except return double the absolute difference if n is over 21.
  43. -------------------------------------------------------------------------------------------------------------------------------
  44. public int diff21(int n)
  45. {
  46. int difference = 21 - n;
  47.  
  48. if (n > 21)
  49. {
  50. difference = (Math.abs(21 - n)) * 2;
  51. }
  52.  
  53. return difference;
  54. }
  55. ---------------------------------------------------------------------------------------------------------------------------------
  56. We have a loud talking parrot. The "hour" parameter is the current hour time in the range 0..23. We are in trouble if the parrot is talking and the hour is before 7 or after 20. Return true if we are in trouble.
  57. -----------------------------------------------------------------------------------------------------------------------------
  58. public boolean parrotTrouble(boolean talking, int hour)
  59. {
  60. boolean inTrouble = false;
  61.  
  62. if (talking)
  63. {
  64. inTrouble = false;
  65.  
  66. if (hour < 7 || hour > 20)
  67. {
  68. inTrouble = true;
  69. }
  70. }
  71.  
  72. return inTrouble;
  73. }
  74. -----------------------------------------------------------------------------------------------------------------------------
  75. Given 2 ints, a and b, return true if one if them is 10 or if their sum is 10.
  76. -----------------------------------------------------------------------------------------------------------------------------
  77. public boolean makes10(int a, int b)
  78. {
  79. boolean isTen = false;
  80.  
  81. int sum = a + b;
  82.  
  83. if (a == 10 || b == 10 || sum == 10)
  84. {
  85. isTen = true;
  86. }
  87.  
  88. return isTen;
  89. }
  90.  
  91. -----------------------------------------------------------------------------------------------------------------------------
  92. Given an int n, return true if it is within 10 of 100 or 200. Note: Math.abs(num) computes the absolute value of a number.
  93. -----------------------------------------------------------------------------------------------------------------------------
  94. public boolean nearHundred(int n)
  95. {
  96. boolean tenOf = false;
  97.  
  98.  
  99.  
  100. if((Math.abs(100 - n) <= 10) || (Math.abs(200 - n) <= 10))
  101. {
  102. tenOf = true;
  103. }
  104.  
  105. return tenOf;
  106. }
  107. -----------------------------------------------------------------------------------------------------------------------------
  108. Given 2 int values, return true if one is negative and one is positive. Except if the parameter "negative" is true, then return true only if both are negative.
  109. -----------------------------------------------------------------------------------------------------------------------------
  110. public boolean posNeg(int a, int b, boolean negative)
  111. {
  112. boolean isBoth = false;
  113.  
  114. if (negative)
  115. {
  116. if (a < 0 && b < 0)
  117. {
  118. isBoth = true;
  119. }
  120. }
  121. else
  122. {
  123. if((a < 0 && b > 0) || (a > 0 && b < 0))
  124. {
  125. isBoth =true;
  126. }
  127. }
  128.  
  129. return isBoth;
  130. }
  131. -----------------------------------------------------------------------------------------------------------------------------
  132. Given a string, return a new string where "not " has been added to the front. However, if the string already begins with "not", return the string unchanged. Note: use .equals() to compare 2 strings.
  133. -----------------------------------------------------------------------------------------------------------------------------
  134. public String notString(String str)
  135. {
  136.  
  137. if (!str.contains("not"))
  138. {
  139. str = "not" + " " + str;
  140. }
  141.  
  142. if (str.contains("is"))
  143. {
  144. str = "not" + " " + str;
  145. }
  146.  
  147. return str;
  148. }
  149.  
  150. -----------------------------------------------------------------------------------------------------------------------------
  151. Given a non-empty string and an int n, return a new string where the char at index n has been removed. The value of n will be a valid index of a char in the original string (i.e. n will be in the range 0..str.length()-1 inclusive).
  152. -----------------------------------------------------------------------------------------------------------------------------
  153. public String missingChar(String str, int n)
  154. {
  155. String result = str.substring(0, n) + str.substring(n + 1);
  156.  
  157. return result;
  158. }
  159. -----------------------------------------------------------------------------------------------------------------------------
  160. Given a string, return a new string where the first and last chars have been exchanged.
  161. -----------------------------------------------------------------------------------------------------------------------------
  162. public String frontBack(String str)
  163. {
  164. if (str.length() > 1)
  165. {
  166. char[] chars = str.toCharArray();
  167. // replace with swap()
  168. char first = chars[0];
  169. chars[0] = chars[chars.length - 1];
  170. chars[chars.length - 1] = first;
  171. str = new String(chars);
  172. }
  173.  
  174. return str;
  175. }
  176. -----------------------------------------------------------------------------------------------------------------------------
  177.  
  178. -----------------------------------------------------------------------------------------------------------------------------
  179.  
  180. -----------------------------------------------------------------------------------------------------------------------------
  181.  
  182. -----------------------------------------------------------------------------------------------------------------------------
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement