Posts Tagged functions
max1020
Given 2 positive int values, return the larger value that is in the range 10..20 inclusive, or return 0 if neither is in that range.
max1020(11, 19) ? 19
max1020(19, 11) ? 19
max1020(11, 9) ? 11
public int max1020(int a, int b) {
int temp;
if(a>b) temp=a;
else temp=b;
if (temp >= 10 && temp <= 20) return temp;
if (b >= 10 && b <= 20) return b;
if (a >= 10 && a <= 20) return a;
return 0;
}
in3050
Given 2 int values, return true if they are both in the range 30..40 inclusive, or they are both in the range 40..50 inclusive.
in3050(30, 31) ? true
in3050(30, 41) ? false
in3050(40, 50) ? true
public boolean in3050(int a, int b) {
if((a>=30 && a<=40)&&(b>=30 && b<=40)) return true;
else if((a>=40 && a<=50)&&(b>=40 && b<=50)) return true;
else return false;
}
hasTeen
We’ll say that a number is “teen” if it is in the range 13..19 inclusive. Given 3 int values, return true if 1 or more of them are teen.
hasTeen(13, 20, 10) ? true
hasTeen(20, 19, 10) ? true
hasTeen(20, 10, 13) ? true
public boolean hasTeen(int a, int b, int c) {
if(a>=13 && a<=19) return true;
else if(b>=13 && b<=19) return true;
else if(c>=13 && c<=19) return true;
else return false;
}
loneTeen
We’ll say that a number is “teen” if it is in the range 13..19 inclusive. Given 2 int values, return true if one or the other is teen, but not both.
loneTeen(13, 99) ? true
loneTeen(21, 19) ? true
loneTeen(13, 13) ? false
public boolean loneTeen(int a, int b) {
if((a==b)) return false;
else if((a >=13 && a<=19) && (b <13 || b>19)) return true;
else if((b >=13 && b<=19) && (a <13 || a>19)) return true;
else return false;
}
in1020
Given 2 int values, return true if either of them is in the range 10..20 inclusive.
in1020(12, 99) ? true
in1020(21, 12) ? true
in1020(8, 99) ? false
public boolean in1020(int a, int b) {
if((a==b) && ((a>=10 && a<=20)&&(b>=10 && b<=20) )) return true;
else if( (a>=10 && a<=20)&&(b<10 || b>20) ) return true;
else if( (b>=10 && b<=20)&&(a<10 || a>20) ) return true;
else return false;
}
missingChar
Given a non-empty string and an int n, return a new string where the char at index n has been removed. The value of n will be a valid index of a char in the original string (i.e. n will be in the range 0..str.length()-1 inclusive).
missingChar(”kitten”, 1)-> “ktten”
missingChar(”kitten”, 0) -> “itten”
missingChar(”kitten”, 4) ->”kittn”
public String missingChar(String str, int n) {
if(n>=0 && n<=str.length()-1)
{if(str.length()==1) return(" ");
else return( str.substring(0,n)+str.substring(n+1));
}
else return str;
}
notString
Given a string, return a new string where “not ” has been added to the front. However, if the string already begins with “not”, return the string unchanged. Note: use .equals() to compare 2 strings.
notString(”candy”) -> “not candy”
notString(”x”) ->”not x”
notString(”not bad”)-> “not bad”
public String notString(String str) {
if(str.length()>=3){
if(str.substring(0,3).equals("not"))
return str;
else return "not "+str;
}
else return "not "+str;
}
posNeg
Given 2 int values, return true if one is negative and one is positive. Unless negative is true, then they both must be negative.
posNeg(1, -1, false) ->true
posNeg(-1, 1, false)-> true
posNeg(1, 1, false) ->false
public boolean posNeg(int a, int b, boolean negative) {
if(((a< 0 && b>0 ) || (a>0 && b<0)) && (negative!=true)) return true;
else if((a<0 && b<0) && (negative ==true)) return true;
else return false;
}
nearHundred
Given an int n, return true if it is within 10 of 100 or 200. Note Math.abs(n) returns the absolute value of a number.
nearHundred(93) -> true
nearHundred(90) -> true
nearHundred(89) ->false
public boolean nearHundred(int n) {
if((Math.abs(100 - n) <= 10) || (Math.abs(200 - n) <= 10)) return true;
else return false;
}
makes10
Given 2 ints, a and b, return true if one if them is 10 or if their sum is 10.
makes10(9, 10) -> true
makes10(9, 9) ->false
makes10(1, 9) -> true
public boolean makes10(int a, int b) {
if( a ==10 || b==10) return true;
else if((a+b)==10) return true;
else return false;
}


Recent Comments