Tutorial 10 - Answers
Question 1.
Write the following static methods as described below:
a)Write a method called isAlpha that accepts a character parameter and returns true if that character is either an uppercase or lowercase alphabetic letter (Roman alphabet).
public static boolean isAlpha (char ch) // for Roman alphabet only!
{
return ( (ch >= 'a' & ch <= 'z') || (ch >= 'A' & ch <= 'Z') );
}
Output (assume that this is done in the main method part of the program):
char a=’a’;
char b=’[‘;
char c=’Z’;
System.out.println(a + “ is a letter character: “ + isAlpha(a));
System.out.println(b + “ is a letter character: “ + isAlpah(b));
System.out.println(c + “ is a letter character: “ + isAlpha(c));
Output on the screen:
a is a letter character: true
] is a letter character: false
Z is a letter character: true
b)Write a method called typeOfChar that accepts a character parameter and returns a String “Upper” or “Lower” or “Neither” depending on if that character is an uppercase or lowercase alphabetic letter or neither.
public static String typeOfChar (char ch) // for Roman alphabet only!
{
String upper = “Upper”;
String lower = “Lower”;
String neither = “Neither”;
if ((ch >= 'a' & ch <= 'z')) //lower case letter
return lower;
else if ( (ch >= 'A' & ch <= 'Z') ) //upper case letter
return upper;
else
return neither; //not a letter
}
Output (assume that this is done in the main method part of the program):
char a='A';
char b='m';
char c='[';
System.out.println(a + " is " + typeOfChar(a));
System.out.println(b + " is " + typeOfChar(b));
System.out.println(c + " is " + typeOfChar(c));
Output on the screen:
A is Upper
m is Lower
[ is Neither
c)Write a method called validate that accepts three integer parameters. The first two parameters represent a range, and the purpose of the method is to verify that the value of the third parameter is in that range. You may assume that the first parameter is less than or equal to the second. If the third parameter is not in the specified range, the method should prompt the user and read a new value. This new value should be tested for validity as well. The method should only return to the calling method once a valid value has been obtained, and it should return the valid value.
public static int validate (int low, int high, int num) throws IOException
{
BufferedReader std=new BufferedReader (new InputStreamReader (System.in));
String input;
while (num < low || num > high)
{
System.out.print ("Invalid value. Please reenter: ");
input = std.readLine(); //read in user response
num = Integer.valueOf(input).intValue();
}
return num;
}
Output (assume that this is done in the main method part of the program):
int low = 20;
int high = 100;
int value1 = 76;
int value2 = 12;
System.out.println(“The number in range is " + validate(low, high, value1));
System.out.println(“The number in range is " + validate(low, high, value2));
Output on the screen:
The number in range is 76
Invalid value. Please reenter: 8
Invalid value. Please reenter: 18
Invalid value. Please reenter: 65
The number in range is 65
d)Write a method called doubleEquals that accepts three floating point values as parameters. The method should return true if the first two parameters are essentially equal, within the tolerance of the third parameter.
public static boolean doubleEquals (double num1, double num2, double tolerance)
{
return (Math.abs(num1-num2) < tolerance);
}
Output (assume that this is done in the main method part of the program):
double first=2.7;
double second=3.0;
double third = 4.0;
double fourth= -2.3;
double fifth= -2.5;
double tolerance=0.5;
System.out.println(first + " and " + second + “ are essentially equal: “ + doubleEquals (first, second, tolerance));
System.out.println(second + " and " + third + “ are essentially equal: “ + doubleEquals (second, third, tolerance));
System.out.println(first + " and " + fourth + “ are essentially equal: “ + doubleEquals (first, fourth, tolerance));
System.out.println(fifth + " and " + fourth + " are essentially equal: " + doubleEquals (fifth, fourth, tolerance));
Output on the screen:
2.7 and 3.0 are essentially equal: true //are within 0.5 of each other
3.0 and 4.0 are essentially equal: false
2.7 and -2.3 are essentially equal: false
-2.5 and -2.3 are essentially equal: true
e)Write a method called reverse that accepts a String as a parameter and returns a String that contains the characters of the parameter in reverse order. Note: there is actually a method in the String class that performs this operation, but for the sake of this exercise you will write your own.
public static String reverse (String str)
{
String result = "";
for (int index=str.length()-1; index >= 0; index--)
result += str.charAt(index);
return result;
} // Note this is very expensive. Better to store char in array and when
// full construct String from that as shown below
public static String reverse (String str)
{
int n = str.length()-1;
char ch[] = new char [ str.length() ];
for (int index=n; index >= 0; index--)
ch[ n - index] = str.charAt(index);
return new String( ch );
}
Output (assume that this is done in the main method part of the program):
String input = “Java Resources”;
System.out.println(input + “ in reverse is: “ + reverse(input));
Output on the screen:
Java Resources in reverse is: secruoseR avaJ
f)Write a method called randomInRange that accepts two integer parametersrepresenting a range. You may assume that the first parameter is lessthan or equal to the second, and that both are positive. The methodshould return a random integer in the specified range.
public static int randomInRange (int low, int high)
{
return ((int) (Math.random()*(high-low+1)) + low);
}
Output (assume that this is done in the main method part of the program):
int low = 10;
int high = 30;
System.out.println(“The random number is “ + randomInRange(low, high));
System.out.println(“The random number is “ + randomInRange(low, high));
Output on the screen:
The random number is 15
The random number is 27