Problem-1
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.
Example
cigarParty(30, false) → false
cigarParty(50, false) → true
cigarParty(70, true) → true
Solution
public boolean cigarParty(int cigars, boolean isWeekend) {
if (cigars>=40 && cigars<=60 && !isWeekend) return true;
else if (cigars>=40 && isWeekend) return true;
return false;
}
Problem-2
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).
Example
dateFashion(5, 10) → 2
dateFashion(5, 2) → 0
dateFashion(5, 5) → 1
Solution
public int dateFashion(int you, int date) {
int result=1;
if(you>=8 || date>=8) result=2;
if (you<=2 || date<=2) result=0;
return result;
}
Problem-3
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.
Example
squirrelPlay(70, false) → true
squirrelPlay(95, false) → false
squirrelPlay(95, true) → true
Solution
public boolean squirrelPlay(int temp, boolean isSummer) {
if ((!isSummer) && (temp>=60 && temp<=90)) return true;
else if ((isSummer) && (temp>=60 && temp<=100)) return true;
return false;
}
Problem-4
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.
Example:
caughtSpeeding(60, false) → 0
caughtSpeeding(65, false) → 1
caughtSpeeding(65, true) → 0
Solution
public int caughtSpeeding(int speed, boolean isBirthday) {
int result=0;
if(isBirthday){
if(speed<=65) result=0;
if(speed>65 && speed<=85) result=1;
if (speed>85) result=2;
}
if(!isBirthday){
if(speed<=60) result=0;
if(speed>60 && speed<=80) result=1;
if (speed>80) result=2;
}
return result;
}
Problem-5
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.
Example
sortaSum(3, 4) → 7
sortaSum(9, 4) → 20
sortaSum(10, 11) → 21
Solution
public int sortaSum(int a, int b) {
int sum=a+b;
if (sum>=10 && sum <=19) sum=20;
return sum;
}
Problem-6
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”.
Example
alarmClock(1, false) → “7:00”
alarmClock(5, false) → “7:00”
alarmClock(0, false) → “10:00”
Solution
public String alarmClock(int day, boolean vacation) {
String result=””;
if( !vacation) {
if(day>=1 && day<=5 ) result=”7:00″;
else result=”10:00″;
}
if(vacation) {
if(day==0 || day==6) result=”off”;
else result=”10:00″;
}
return result;
}
Problem-7
The number 6 is a truly great number. Given two int values, a and b, return true if either one is 6. Or if their sum or difference is 6. Note: the function Math.abs(num) computes the absolute value of a number.
Example
love6(6, 4) → true
love6(4, 5) → false
love6(1, 5) → true
Solution
public boolean love6(int a, int b) {
int sum=(a+b);
int diff=Math.abs(a-b);
return (a==6 || b==6 || sum==6 ||diff==6) ;
}
Problem-8
Given a number n, return true if n is in the range 1..10, inclusive. Unless “outsideMode” is true, in which case return true if the number is less or equal to 1, or greater or equal to 10.
Example
in1To10(5, false) → true
in1To10(11, false) → false
in1To10(11, true) → true
Solution
public boolean in1To10(int n, boolean outsideMode) {
if(!outsideMode && (n>=1 && n<=10)) return true;
if (outsideMode && (n<=1 || n>=10)) return true;
return false;
}
Problem-9
We’ll say a number is special if it is a multiple of 11 or if it is one more than a multiple of 11. Return true if the given non-negative number is special. Use the % “mod” operator .
Example
specialEleven(22) → true
specialEleven(23) → true
specialEleven(24) → false
Solution
public boolean specialEleven(int n) {
return (n % 11==0 || n%11==1);
}
Problem-10
Return true if the given non-negative number is 1 or 2 more than a multiple of 20.
Example
more20(20) → false
more20(21) → true
more20(22) → true
Solution
public boolean more20(int n) {
return (n%20 == 1 || n%20==2);
}
Problem-11
Return true if the given non-negative number is a multiple of 3 or 5, but not both.
Solution
public boolean old35(int n) {
if (n% 3==0 && n%5==0) return false;
if (n%3==0 || n%5==0) return true;
return false;
}