Showing posts with label logic. Show all posts
Showing posts with label logic. Show all posts

Java > Logic-1 > answerCell (CodingBat Solution)

Problem:

Your cell phone rings. Return true if you should answer it. Normally you answer, except in the morning you only answer if it is your mom calling. In all cases, if you are asleep, you do not answer.

answerCell(false, false, false) → true
answerCell(false, false, true) → false
answerCell(true, false, false) → false


Solution:

public boolean answerCell(boolean isMorning, boolean isMom, boolean isAsleep) {
  if (isAsleep)
    return false;
  if ((isMorning && isMom) || !isMorning)
    return true;
  else return false;
}
Read More

Java > Logic-1 > teenSum (CodingBat Solution)

Problem:

Given 2 ints, a and b, return their sum. However, "teen" values in the range 13..19 inclusive, are extra lucky. So if either value is a teen, just return 19.

teenSum(3, 4) → 7
teenSum(10, 13) → 19
teenSum(13, 2) → 19


Solution:

public int teenSum(int a, int b) {
  int sum = a+b;
  if ((a >= 13 && a <= 19) || (b >= 13 && b <= 19))
    return 19;
  else
    return sum;
}
Read More

Java > Logic-1 > nearTen (CodingBat Solution)

Problem:

Given a non-negative number "num", return true if num is within 2 of a multiple of 10. Note: (a % b) is the remainder of dividing a by b, so (7 % 5) is 2. See also: Introduction to Mod

nearTen(12) → true
nearTen(17) → false
nearTen(19) → true


Solution:

public boolean nearTen(int num) {
  if (num % 10 < 3 || num % 10 >=8)
    return true;
  else return false;
}
Read More

Java > Logic-1 > less20 (CodingBat Solution)

Problem:

Return true if the given non-negative number is 1 or 2 less than a multiple of 20. So for example 38 and 39 return true, but 40 returns false. See also: Introduction to Mod

less20(18) → true
less20(19) → true
less20(20) → false


Solution:

public boolean less20(int n) {
    return (n + 1) % 20 == 0 || (n + 2) % 20 == 0;
}
Read More

Java > Logic-1>blueTicket (CodingBat Solution)

Problem:

You have a blue lottery ticket, with ints a, b, and c on it. This makes three pairs, which we'll call ab, bc, and ac. Consider the sum of the numbers in each pair. If any pair sums to exactly 10, the result is 10. Otherwise if the ab sum is exactly 10 more than either bc or ac sums, the result is 5. Otherwise the result is 0.

blueTicket(9, 1, 0) → 10
blueTicket(9, 2, 0) → 0
blueTicket(6, 1, 4) → 10


Solution:

public int blueTicket(int a, int b, int c) {
  if(a+b == 10 || b+c == 10 || a+c == 10)
  return 10;
  else if (a+b == (10 + b+c) ||  a+b == (10 + a+c) )
  return 5;
  else
  return 0;
}
Read More

Follow Me

If you like our content, feel free to follow me to stay updated.

Subscribe

Enter your email address:

We hate spam as much as you do.

Upload Material

Got an exam, project, tutorial video, exercise, solutions, unsolved problem, question, solution manual? We are open to any coding material. Why not upload?

Upload

Copyright © 2012 - 2014 Java Problems  --  About  --  Attribution  --  Privacy Policy  --  Terms of Use  --  Contact