Tutorial 10 - Answers - Dalhousie University
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 = 'A' && ch = 'a' && ch = 'A' && ch 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
Write a method called randomInRange that accepts two integer parameters representing a range. You may assume that the first parameter is less than or equal to the second, and that both are positive. The method should 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
................
................
In order to avoid copyright disputes, this page is only a partial summary.
To fulfill the demand for quickly locating and searching documents.
It is intelligent file search solution for home and business.