The Java Learning Project

I started learning Java programming and found a perfect website for practicing: Codingbat.com. CodingBat is a free site of live coding problems to build coding skill in Java and Python. CodingBat is a project by Nick Parlante, a computer science lecturer at Stanford.

I will upload my solutions here.

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

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
public int blueTicket(int a, int b, int c) {
  int ab=a+b;
  int bc=b+c;
  int ac=a+c;
  int result;
  if (ab==10 || bc==10 || ac==10){
    result=10;
  } else if (ab==bc+10 || ab==ac+10){
    result=5;
  } else {
    result=0;
  } return result;
}

Given two int values, return whichever value is larger. However if the two values have the same remainder when divided by 5, then the return the smaller value. However, in all cases, if the two values are the same, return 0. Note: the % "mod" operator computes the remainder, e.g. 7 % 5 is 2.

maxMod5(2, 3) → 3
maxMod5(6, 2) → 6
maxMod5(3, 2) → 3

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
public int maxMod5(int a, int b) {
  int smaller=a>=b?b:a;
  int larger=a>=b?a:b;
  if (a==b) {
    return 0;
  } else if (a%5==b%5) {
    return smaller;
  } else {
    return larger;
  }
}

Return the sum of two 6-sided dice rolls, each in the range 1..6. However, if noDoubles is true, if the two dice show the same value, increment one die to the next value, wrapping around to 1 if its value was 6.

withoutDoubles(2, 3, true) → 5
withoutDoubles(3, 3, true) → 7
withoutDoubles(3, 3, false) → 6

1
2
3
4
5
6
7
8
9
public int withoutDoubles(int die1, int die2, boolean noDoubles) {
  int result;
  if ((noDoubles) && die1==die2) {
    die1++;
    die1=die1>6 ? 1 : die1;
  }
  result=die1+die2;
  return result;
}

Keresés

Ajánló