Exercises 4
Exercises 4
EXERCISE A.
Use the Java API docs to find the showConfirmDialog(Component parent component, Object message, String title, int option type) method of the javax.swing.JOptionPane class.
EXERCISE B.
In Java, a class method is one which is static, i.e. can be called on the class without requiring an instance of the class. To create a static method, you need to declare the method as static:
public class MyClass {
private static final byte MY_CONST = 10;
//empty constructor
public MyClass(){
}
public static byte myStaticMethod(double aNumber){
if(aNumber > MY_CONST){
return 0;
} else {
return 1;
}
}
}
Note that any object variable (i.e. not one declared and used inside the static method) that will be used by the static method must also be declared to be static - see MY_CONST above. To use this static method simply call it by using the class name and dot notation:
byte result = MyClass.myStaticMethod(myNum);
No class instantiation MyClass anObj = new MyClass(); is required.
Build a Java class Comparison that contains a single static method isZero(double aNumber), which tests to see if its argument is close to zero; the method must have return type boolean. Use the constant EPSILON = 1E-14 as the test value. Build a class TestComparison that tests the Comparison class. Have TestComparison solicit the input number by means of a dialog box. Compile and run your classes; document Comparison.java.
EXERCISE C.
Implement a Java class StringToWords that has a single static method
public static String[] getWords(String str).
This class must break any string up into 'words' using the space char as the word separator, i.e. the returned words must not contain any leading, trailing or included spaces; the method must return the words in an array. Implement a class TestWords that tests the StringToWords class by requesting an input string from the user with an input dialog. Have TestWords write each extracted word to the console on a new line - see below:
EXERCISE D.
In the previous exercise the class StringToWords is a somewhat rudimentary version of an object known as a string tokeniser. A string tokeniser is an object that breaks a string up into a set of values (also known as tokens) by using a preset character as the delimiter for each datum in the string. In the case of class StringToWords the delimiting character is the space character. Java has a string tokeniser ('tokenizer' - American spelling) java.util.StringTokenizer of its own, which is a very useful class because it allows us to set the delimiter to be any character we desire. Have a look at this class in the Java documentation. We are really only interested in the StringTokenizer(String str) constructor and the following two methods:
- hasMoreTokens()
Tests to see if there are more tokens to be extracted from the tokeniser's string; returns true or false. - nextToken()
Returns the next token from the tokeniser's string.
By default a StringTokenizer uses any white-space char like a space as datum delimiter (just like a StringToWords object), so using the constructor StringTokenizer(String str) will produce a string tokeniser that looks for space chars in String str to use as the char that separates the tokens. For example:
StringTokenizer tokeniser = new StringTokenizer("23 54 13 8 11");
will produce an object tokeniser that has five String integer values as its tokens.
Download DataStore.class - here is the documentation for the class.
Using classes DataStore and java.util.StringTokenizer, implement a class InputTokeniser that finds the average, maximum and minimum values of a set of double precision numbers separated by spaces, entered as a string by means of a
javax.swing.JOptionPane.showInputDialog(String message)
input dialog. Your class should use the constructors DataStore() and StringTokenizer(String str), as well as any methods from these two classes required to produce the requested output.
Note: java.util.StringTokenizer is a deprecated class, i.e. it should not be used in new applications. We have used this class here for illustration and learning purposes, and we will explore a good alternative to StringTokenizer a little further on in this tutorial.
© G. Hearn, & University of the Western Cape, 2006






