iântâ âaâ=10, âbâ;
aâ=âaâ+5; // a=15
bâ=âaâ+4; // b=19
b= (++a); // a=16, b=16
intâ âansâ=âaâ+âbâ; //16+19=35
//Write a function called isOdd that takes an int and returns a boolean;
//the function returns true if the number is odd and false otherwise
public static
boolean isOdd(int a) {
return (a%2==1);
}
//Write a function called isLarge that takes an int and returns a boolean;
// the function returns true if its argument is larger than 1000, false otherwise.
boolean isLarge(int n){
return n>1000;
}
//Write a function called fizz, that takes an integer and returns a String.
//The function returns âfizzâ if its argument is divisible by 3, and the
//number otherwise
public static
String fizz(int a) {
return a%3==0 ? "fizz" : ""+a;
/* if(a%3==0)
return "fizz";
else
return ""+a;
*/
}
publicâ âstaticâ
âintâ bar(âintâ âaâ) {
ifâ (âaâ<=0)
returnâ 2;
else
returnâ âaâ*bar(âa-âÂ1);
}
bar(-1) ? = 2
bar(3) = 3*bar(2) = 3* 2 *bar(1) = 3 * 2 * 2 = 18
publicâ âstaticâ
String baz(âcharâ âc1â, âcharâ âc2â) {
String âansâ=â""â;
forâ(âcharâ âcâ=âc1â; âcâ<=âc2â; ++âcâ)
ansâ+=âcâ;
returnâ âansâ;
}
baz('a','d') ?
//Write a function called printFromTo, which takes two integer parameters,
// and prints all numbers between its first and second parameters,
// including both parameters (print a newLine character after each number).
void printFromTo(int from, int to) {
for(int i=from; i<=to; ++i) {
System.out.println(i);
}
}
void printFromTo(int from, int to) {
int i=from;
while( i<=to) {
System.out.println(i);
++i;
}
}
void printFromTo(int from, int to) {
if(from<=to) {
System.out.println(from);
printFromTo(from+1,to);
}
}
void printFromTo2(int from, int to) {
for(int i=from+1; i