Java Logic Part Two- 9 Problems with Solutions

Problem-1
We want to make a row of bricks that is goal inches long. We have a number of small bricks (1 inch each) and big bricks (5 inches each). Return true if it is possible to make the goal by choosing from the given bricks.
Example
makeBricks(3, 1, 8) → true
makeBricks(3, 1, 9) → false
makeBricks(3, 2, 10) → true
Solution
public boolean makeBricks(int small, int big, int goal) {
for (int i=0; i<=big; i++){
for (int j=0;j<=small; j++){
if ( (i*5 + j)==goal) return true;
}
}
return false;
}
Problem-2
Given 3 int values, a b c, return their sum. However, if one of the values is the same as another of the values, it does not count towards the sum.
Exmaple
loneSum(1, 2, 3) → 6
loneSum(3, 2, 3) → 2
loneSum(3, 3, 3) → 0
Solution
public int loneSum(int a, int b, int c) {
int result = a+b+c;
if (a==b && b==c) result=0;
else if (a==b) result=c;
else if (a==c) result=b;
else if (b==c) result=a;
return result;
}
Problem-3
Given 3 int values, a b c, return their sum. However, if one of the values is 13 then it does not count towards the sum and values to its right do not count. So for example, if b is 13, then both b and c do not count.
Example
luckySum(1, 2, 3) → 6
luckySum(1, 2, 13) → 3
luckySum(1, 13, 3) → 1
Solution
public int luckySum(int a, int b, int c) {
int sum=a+b+c;
if (a==13) sum=0;
else if (b==13) sum=a;
else if (c==13) sum=a+b;
return sum;
}
Problem-4
For this problem, we’ll round an int value up to the next multiple of 10 if its rightmost digit is 5 or more, so 15 rounds up to 20. Alternately, round down to the previous multiple of 10 if its rightmost digit is less than 5, so 12 rounds down to 10. Given 3 ints, a b c, return the sum of their rounded values. To avoid code repetition, write a separate helper “public int round10(int num) {” and call it 3 times. Write the helper entirely below and at the same indent level as roundSum().
Example
roundSum(16, 17, 18) → 60
roundSum(12, 13, 14) → 30
roundSum(6, 4, 4) → 10
Solution
public int roundSum(int a, int b, int c) {
int result= round10(a) + round10(b) + round10(c);
return result;
}
public int round10(int num){
if (num%10>=5)  num=num+(10-num%10);
else num=num-num%10;
return num;
}
Problem-5
Given three ints, a b c, return true if one of b or c is “close” (differing from a by at most 1), while the other is “far”, differing from both other values by 2 or more. Note: Math.abs(num) computes the absolute value of a number.
Example
closeFar(1, 2, 10) → true
closeFar(1, 2, 3) → false
closeFar(4, 1, 3) → true
Solution
public boolean closeFar(int a, int b, int c) {
int ab=Math.abs(a-b);
int ca=Math.abs(c-a);
int cb= Math.abs(c-b);
if ( (ab<=1 && ca>=2 && cb>=2) || (ca<=1 && ab>=2 && cb>=2)) return true;
return false;
}
Problem-6
Given 2 int values greater than 0, return whichever value is nearest to 21 without going over. Return 0 if they both go over.
Example
blackjack(19, 21) → 21
blackjack(21, 19) → 21
blackjack(19, 22) → 19
Solution
public int blackjack(int a, int b) {
int a1=21-a;
int b1=21-b;
int result=0;
if( a1>=0 && b1>=0){
if (a1<b1 ) result=a;
else result=b;
}
if (a1<0 && b1>=0) result=b;
if( b1<0 && a1>=0) result=a;
return result;
}
Problem-7
Given three ints, a b c, one of them is small, one is medium and one is large. Return true if the three values are evenly spaced, so the difference between small and medium is the same as the difference between medium and large.
Example
evenlySpaced(2, 4, 6) → true
evenlySpaced(4, 6, 2) → true
evenlySpaced(4, 6, 3) → false
Solution
public boolean evenlySpaced(int a, int b, int c) {
int ab= Math.abs(a-b);
int bc=Math.abs(b-c);
int ac=Math.abs(a-c);
if(ab==bc && bc==ac) return true;
if ( ab==0 || bc==0 || ac==0) return false;
if (ab==bc || bc==ac || ab==ac) return true;
return false;
}
Problem-8
We want make a package of goal kilos of chocolate. We have small bars (1 kilo each) and big bars (5 kilos each). Return the number of small bars to use, assuming we always use big bars before small bars. Return -1 if it can’t be done.
Example
makeChocolate(4, 1, 9) → 4
makeChocolate(4, 1, 10) → -1
makeChocolate(4, 1, 7) → 2
Solution
public int makeChocolate(int small, int big, int goal) {
for (int i=0;i<=small;i++){
for(int j=0;j<=big;j++){
if (((j*5) + (i))==goal) return i;
}
}
return -1;
}
Problem-9
Given 3 int values, a b c, return their sum. However, if any of the values is a teen — in the range 13..19 inclusive — then that value counts as 0, except 15 and 16 do not count as a teens. Write a separate helper “public int fixTeen(int n) {“that takes in an int value and returns that value fixed for the teen rule. In this way, you avoid repeating the teen code 3 times (i.e. “decomposition”). Define the helper below and at the same indent level as the main noTeenSum().
Example
noTeenSum(1, 2, 3) → 6
noTeenSum(2, 13, 1) → 3
noTeenSum(2, 1, 14) → 3
Solution
public int noTeenSum(int a, int b, int c) {
int result=fixTeen(a)+fixTeen(b)+fixTeen(c);
return result;
}
public int fixTeen(int n){
if((n>=13 && n<=19) && (n!=15 && n!=16)) n=0;
else n=n;
return n;
}
**All Problems were taken from the website CodingBat