Tutorial 10

advertisement
Name: _____________________________
Student ID:____________________
Tutorial 10
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!
{
}
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!
{
}
1
Name: _____________________________
Student ID:____________________
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
{
}
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.
2
Name: _____________________________
Student ID:____________________
public static boolean doubleEquals (double num1, double num2, double 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)
{
}
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) );
3
Name: _____________________________
Student ID:____________________
Output on the screen:
Java Resources in reverse is: secruoseR avaJ
f) 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)
{
}
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
4
Download