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.

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

1
2
3
4
5
6
7
8
public int teenSum(int a, int b) {
  if ((a>=13 && a<=19) || (b>=13 && b<=19)){
    return 19;
  }
  else {
    return a+b;
  }
}

Given a day of the week encoded as 0=Sun, 1=Mon, 2=Tue, ...6=Sat, and a boolean indicating if we are on vacation, return a string of the form "7:00" indicating when the alarm clock should ring. Weekdays, the alarm should be "7:00" and on the weekend it should be "10:00". Unless we are on vacation -- then on weekdays it should be "10:00" and weekends it should be "off".

alarmClock(1, false) → "7:00"
alarmClock(5, false) → "7:00"
alarmClock(0, false) → "10:00"

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
public String alarmClock(int day, boolean vacation) {
  String result="";
  if (vacation){
    if (day==0 || day==6){
      result="off";
    }else{
      result="10:00";
    }
  }else{
    if (day==0 || day==6){
      result="10:00";
    }else{
      result="7:00";
    }
  }
  return result;
}

Given 2 ints, a and b, return their sum. However, sums in the range 10..19 inclusive, are forbidden, so in that case just return 20.

sortaSum(3, 4) → 7
sortaSum(9, 4) → 20
sortaSum(10, 11) → 21

1
2
3
4
5
6
7
public int sortaSum(int a, int b) {
  if (a+b>=10 && a+b<=19){
    return 20;
  } else {
    return a+b;
  }
}

Keresés

Ajánló