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 two ints, each in the range 10..99, return true if there is a digit that appears in both numbers, such as the 2 in 12 and 23. (Note: division, e.g. n/10, gives the left digit while the % "mod" n%10 gives the right digit.)

shareDigit(12, 23) → true
shareDigit(12, 43) → false
shareDigit(12, 44) → false

1
2
3
4
5
6
7
public boolean shareDigit(int a, int b) {
  int a1=a/10;
  int a2=a%10;
  int b1=b/10;
  int b2=b%10;
  return (a1==b1 || a1==b2 || b1==a2 || a2==b2);
}

Given a string, return a string where for every char in the original, there are two chars.

doubleChar("The") → "TThhee"
doubleChar("AAbb") → "AAAAbbbb"
doubleChar("Hi-There") → "HHii--TThheerree"

1
2
3
4
5
6
7
8
9
public String doubleChar(String str) {
  String character="";
  String result="";
  for (int i=0; i<str.length(); i++){
    character=str.substring(i,i+1);
    result=result+character+character;
  }
  return result;
}

Given 2 non-negative ints, a and b, return their sum, so long as the sum has the same number of digits as a. If the sum has more digits than a, just return a without b. (Note: one way to compute the number of digits of a non-negative int n is to convert it to a string with String.valueOf(n) and then check the length of the string.)

sumLimit(2, 3) → 5
sumLimit(8, 3) → 8
sumLimit(8, 1) → 9

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
public int sumLimit(int a, int b) {
  int a_length=(String.valueOf(a)).length();
  int b_length=(String.valueOf(b)).length();
  int sum=a+b;
  if ((String.valueOf(sum)).length()==(String.valueOf(a)).length()) {
    return sum;
  } else {
    return a;
  }
}

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;
}

Given three ints, a b c, return true if two or more of them have the same rightmost digit. The ints are non-negative. Note: the % "mod" operator computes the remainder, e.g. 17 % 10 is 7.

lastDigit(23, 19, 13) → true
lastDigit(23, 19, 12) → false
lastDigit(23, 19, 3) → true

1
2
3
public boolean lastDigit(int a, int b, int c) {
   return ((a % 10)==(b % 10) || (b % 10)==(c % 10) || (a % 10)==(c % 10));
}

Given three ints, a b c, return true if b is greater than a, and c is greater than b. However, with the exception that if "bOk" is true, b does not need to be greater than a.

inOrder(1, 2, 4, false) → true
inOrder(1, 2, 1, false) → false
inOrder(1, 1, 2, true) → true

1
2
3
4
5
6
7
public boolean inOrder(int a, int b, int c, boolean bOk) {
  if ((bOk && c>b) || (b>a && c>b)) {
    return true;
  } else {
    return false;
  }
}

We are having a party with amounts of tea and candy. Return the int outcome of the party encoded as 0=bad, 1=good, or 2=great. A party is good (1) if both tea and candy are at least 5. However, if either tea or candy is at least double the amount of the other one, the party is great (2). However, in all cases, if either tea or candy is less than 5, the party is always bad (0).

teaParty(6, 8) → 1
teaParty(3, 8) → 0
teaParty(20, 6) → 2

1
2
3
4
5
6
7
8
9
public int teaParty(int tea, int candy) {
  if ((tea>=5 && candy>=5) && (tea>=2*candy || candy>=2*tea)){
    return 2;
  } else if (tea>=5 && candy>=5){
    return 1;
  } else {
    return 0;
  }
}

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;
  }
}

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;
}

When squirrels get together for a party, they like to have cigars. A squirrel party is successful when the number of cigars is between 40 and 60, inclusive. Unless it is the weekend, in which case there is no upper bound on the number of cigars. Return true if the party with the given values is successful, or false otherwise.

cigarParty(30, false) → false
cigarParty(50, false) → true
cigarParty(70, true) → true

1
2
3
public boolean cigarParty(int cigars, boolean isWeekend) {
  return (((cigars>=40 && cigars<=60) && !isWeekend) || (cigars>=40 && isWeekend));
}

Given an array of ints of odd length, look at the first, last, and middle values in the array and return the largest. The array length will be a least 1.

maxTriple([1, 2, 3]) → 3
maxTriple([1, 5, 3]) → 5
maxTriple([5, 2, 3]) → 5

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
public int maxTriple(int[] nums) {
  int max=0;
  int first=nums[0];
  int last=nums[nums.length-1];
  int mid=nums[nums.length/2];
  if ((first>last) && (first>mid)){
    max=first;
  }else if ((last>first) && (last>mid)) {
    max=last;
  }else{
    max=mid;
  }
  return max;
}

Given an array of ints of odd length, return a new array length 3 containing the elements from the middle of the array. The array length will be at least 3.

midThree([1, 2, 3, 4, 5]) → [2, 3, 4]
midThree([8, 6, 7, 5, 3, 0, 9]) → [7, 5, 3]
midThree([1, 2, 3]) → [1, 2, 3]

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
public int[] midThree(int[] nums) {
  int[] result = new int[3];
  if (nums.length>3){
     result[0]=nums[(nums.length/2)-1];
     result[1]=nums[(nums.length/2)];
     result[2]=nums[(nums.length/2)+1];
  }else{
    result=nums;
  }return result;
}

Start with 2 int arrays, a and b, each length 2. Consider the sum of the values in each array. Return the array which has the largest sum. In event of a tie, return a.

biggerTwo([1, 2], [3, 4]) → [3, 4]
biggerTwo([3, 4], [1, 2]) → [3, 4]
biggerTwo([1, 1], [1, 2]) → [1, 2]

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
public int[] biggerTwo(int[] a, int[] b) {
  int sum_a;
  int sum_b;
  sum_a=a[0]+a[1];
  sum_b=b[0]+b[1];
  if (sum_a>=sum_b){
    return a;
  }else{
    return b;
  }
}

Given an int array length 2, return true if it contains a 2 or a 3.

has23([2, 5]) → true
has23([4, 3]) → true
has23([4, 5]) → false

1
2
3
public boolean has23(int[] nums) {
  return ((nums[0]==2) || (nums[1]==2) || (nums[0]==3) || (nums[1]==3));
}

Keresés

Ajánló