Java Warmup-1 of codingbat.com solutions to problems 1 to 15. If you are new here please visit the previous post learn-coding-computer-programming-for-beginners.
1. Problem Name :sleepIn
Solution:
Hint: The problem statement says we can sleep when it is not a weekday or
if we are on vacation.
public boolean sleepIn(boolean weekday, boolean vacation) {
if (!weekday || vacation) {
return true;
}
return false;
//
Solution notes: better to write "vacation" than "vacation ==
true"
//
though they mean exactly the same thing.
//
Likewise "!weekday" is better than "weekday == false".
//
This all can be shortened to: return (!weekday || vacation);
//
Here we just put the return-false last, or could use an if/else.
}
|
2. Problem Name : monkeyTrouble
Solution:
Hint:
We are in trouble if they are both smiling or if neither of them is smiling. We
have to return true if we are in trouble.
public boolean monkeyTrouble(boolean aSmile, boolean bSmile) {
if (aSmile && bSmile) {
return true;
}
if (!aSmile && !bSmile) {
return true;
}
return false;
//
The above can be shortened to:
// return ((aSmile && bSmile) ||
(!aSmile && !bSmile));
//
Or this very short version (think about how this is the same as the above)
// return (aSmile == bSmile);
}
|
3. Problem Name : diff21
Solution:
Hint:
We need to sum for all, but if a and b are equal then we have to double the
sum.
public int sumDouble(int a, int b) {
//
Store the sum in a local variable
int sum = a + b;
//
Double it if a and b are the same
if (a == b) {
sum = sum * 2;
}
return sum;
}
|
4. Problem Name : sumDouble
Solution:
Hint: We need to return (21-n) if
n is less than or equal to 21 else we have to return ((n-21)*2).
public int diff21(int n) {
//if n less than 21 return (21-n)
if (n <= 21) {
return 21 - n;
}
else // else n is greater than 21 so return ((n-21)*2)
{
return (n - 21) * 2;
}
}
|
5. Problem Name : parrotTrouble
Solution:
Hint:
We are in trouble if the parrot is talking and the hour is before 7 or
after 20.
public boolean parrotTrouble(boolean talking, int hour) {
return
(talking && (hour < 7 || hour > 20));
//
Need extra parenthesis around the || clause
//
since && binds more tightly than ||
//
&& is like arithmetic *, || is like arithmetic +
}
|
6. Problem Name : makes10
Solution:
Hint:
We need to return true if a is 10 or b is 10 or (a+b) is 10
public boolean makes10(int a, int b) {
return (a == 10 || b == 10 || (a+b) == 10);
}
|
7. Problem Name : nearHundred
Solution:
Hint: we need to
return true if it is within 10 of 100 or 200.
public boolean nearHundred(int n) {
return ((Math.abs(100 - n) <= 10) ||
(Math.abs(200 - n) <= 10));
}
|
8. Problem Name : posNeg
Solution:
Hint: if negative is true return true if a AND b
are both negative. Else return true if a is positive and b negative OR a is
negative and b is positive.
public boolean posNeg(int a, int b, boolean negative) {
if (negative) {
return (a < 0 && b < 0);
}
else {
return ((a < 0 && b > 0) ||
(a > 0 && b < 0));
}
}
|
9. Problem Name : notString
Solution:
Hint: return the string as such if it starts with
not else return not concatenated to the start of the string.
public String
notString(String str) {
if (str.length() >= 3 && str.substring(0, 3).equals("not")) {
return str;
}
return "not " + str;
}
|
10. Problem Name : missingChar
Solution:
Hint: We need to remove character at n. Do a substring
from 0 to n and substring from n+1 to
string length to omit the character.
public String
missingChar(String str, int n) {
String front = str.substring(0, n);
//
Start this substring at n+1 to omit the char.
//
Can also be shortened to just str.substring(n+1)
//
which goes through the end of the string.
String back = str.substring(n+1, str.length());
return front + back;
}
|
11. Problem Name : frontBack
Solution:
Hint: if length is less than or equal to one return
the string as such. Else get the mid string and return last char + mid String +
first char
public String
frontBack(String str) {
if (str.length() <= 1) return str;
String mid = str.substring(1, str.length()-1);
//
last + mid + first
return str.charAt(str.length()-1) + mid + str.charAt(0);
}
|
12. Problem Name : front3
Solution:
Hint:
If len is greater than 3 we need to return first 3 character string appended
thrice else return the string as such thrice
public String
front3(String str) {
String front;
if (str.length() >= 3) {
front = str.substring(0, 3);
}
else {
front = str;
}
return front + front + front;
}
|
13. Problem Name : backAround
Solution:
Hint:
Get the last character as string, return last+string+last
public String
backAround(String str) {
//
Get the last char
String last = str.substring(str.length() - 1);
return last + str + last;
}
|
14. Problem Name : or35
Solution:
Hint:
If divisible by three number %3 will be zero, if divisible by 5 number %5 will
be zero
public boolean or35(int n) {
return (n % 3 == 0) || (n % 5 == 0);
}
|
15. Problem Name : front22
Solution:
Hint:
We need to take only first two characters if the string has less than 2 characters
we take the string as such and return front+str+front.
public String front22(String str) {
//
First figure the number of chars to take
int take = 2;
if (take > str.length()) {
take = str.length();
}
String front = str.substring(0, take);
return front + str + front;
}
|
Please visit my blogs for solutions for the other problems in Java Warmup 1.
No comments:
Post a Comment