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 are driving a little too fast, and a police officer stops you. Write code to compute the result, encoded as an int value: 0=no ticket, 1=small ticket, 2=big ticket. If speed is 60 or less, the result is 0. If speed is between 61 and 80 inclusive, the result is 1. If speed is 81 or more, the result is 2. Unless it is your birthday -- on that day, your speed can be 5 higher in all cases.

caughtSpeeding(60, false) → 0
caughtSpeeding(65, false) → 1
caughtSpeeding(65, true) → 0

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
public int caughtSpeeding(int speed, boolean isBirthday) {
  int result=0;
  int bd=0;
  if (isBirthday){
    bd=5;
  }
  if (speed<=60+bd){
    result=0;
  } else if (speed>61+bd && speed<=80+bd){
    result=1;
  } else if (speed>=81+bd){
    result=2;
  }
  return result;
}

The squirrels in Palo Alto spend most of the day playing. In particular, they play if the temperature is between 60 and 90 (inclusive). Unless it is summer, then the upper limit is 100 instead of 90. Given an int temperature and a boolean isSummer, return true if the squirrels play and false otherwise.

squirrelPlay(70, false) → true
squirrelPlay(95, false) → false
squirrelPlay(95, true) → true

1
2
3
public boolean squirrelPlay(int temp, boolean isSummer) {
  return ((temp>=60 && temp<=90 && !isSummer) || (temp>=60 && temp <=100 && isSummer));
}

You and your date are trying to get a table at a restaurant. The parameter "you" is the stylishness of your clothes, in the range 0..10, and "date" is the stylishness of your date's clothes. The result getting the table is encoded as an int value with 0=no, 1=maybe, 2=yes. If either of you is very stylish, 8 or more, then the result is 2 (yes). With the exception that if either of you has style of 2 or less, then the result is 0 (no). Otherwise the result is 1 (maybe).

dateFashion(5, 10) → 2
dateFashion(5, 2) → 0
dateFashion(5, 5) → 1

1
2
3
4
5
6
7
8
9
public int dateFashion(int you, int date) {
  int result=0;
  if ((you>=8 || date>=8) && (you>2 && date>2)){
    result=2;
  }else if ((you<8 || date<8) && (you>2 && date>2)){
    result=1;
  }
  return result;
}

Keresés

Ajánló