1 Write a method called randomInRange that takes in two numb
1) Write a method called randomInRange that takes in two numbers representing a
range. Print an error message and return zero if the second parameter is less than
the first. Otherwise, the method should return a randomly generated integer in that
range (inclusive). You may assume that the class has a static Random object
called generator already declared and instantiated.
2) Write a method called isPalindrome that accepts a String as a parameter and
returns true if the String is a palindrome, and false otherwise. You may assume
that the entered String consists entirely of lowercase letters (meaning it contains no
numbers, spaces, punctuation, etc). Hint: write code that creates a new string that
is the original string reversed, and then check to see if the two strings are equal.
3) Write a method called countSpaces that takes in a String as a parameter and
returns an integer that represents the number of space characters (\' \') contained in
the string, false otherwise..
4) Write a method called containsPair that takes in three integer parameters and
returns true if any two of the input parameters are the same.
5) Write a method called threeOfAKind that takes in three integer parameters and
returns true if all three of them are the same, false otherwise.
Solution
1 program)public class cheggex {
public static void main(String[] args) {
int out= RandomlnRange(7,9);
System.out.println(out);
}
public static int RandomlnRange(int n1,int n2){
int v1=n1;
int v2=n2;
int range;
if(v1>v2)
range=v1-v2;
else
range=v2-v1;
Random r=new Random();
int getr=r.nextInt(range)+1;
if(v2>v1){
System.out.println(\"error!\");
return 0;
}else
return getr;
}
}
2 program)
public class chekpallen{
public boolean ispallendrome(){
System.out.println(\"enter lowercase String:\");
Scanner sc=new Scanner(System.in);
String res=sc.next();
String output=\"\";
for(int i=res.length()-1;i>=0;i--)
output=output+res.charAt(i);
if(res.equals(output))
return true;
else
return false;
}
}
program 3)class cont{
public int countSpaces(){
System.out.println(\"ent4er String:\");
Scanner sc=new Scanner(System.in);
String in=sc.next();
int count=0;
for(int i=0;i<in.length();i++){
if(in.charAt(i)==\' \')
count++;
return count;
}
}
program 4)
public class example {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
boolean result=containsPair(4,3,5);
System.out.println(result);
}
private static boolean containsPair(int i, int j, int k) {
int val1=i;
int val2=j;
int val3=k;
if(val1==val2 ||val1==val3|| val2==val3)
return true;
else
return false;
}
}
program 5)
public class chekpallendrome {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
boolean result=containsPair(4,4,4);
System.out.println(result);
}
private static boolean containsPair(int i, int j, int k) {
int val1=i;
int val2=j;
int val3=k;
if(val1==val2 &&val1==val3)
return true;
else
return false;
}
}


