ANSWERS TO REVIEW QUESTIONS 1. See textbook and notes. 2. The ones we covered in csc211 are int, char, double, and boolean. Other Java primitive types are byte, short, long, and float. 3. % 4. x != y if x and y are primitive data types or !x.equals(y) for strings. 5. length, equals, charAt, toUpperCase, substring, valueOf, equalsIgnoreCase. 6. getText, setText, setEditable, addActionListener. A JTextBox object generates an action event when the object has the focus and the return key is pressed. 7. f = x > y; 8. System.out.println(args[args.length - 1]); 9. public static void main(String[] args) { int i; String fileName, line; FileReader fr; BufferedReader br; for(i = 1; i <= 100; i++) { fileName = "d:\\file" + i + ".txt"; fr = new FileReader(fileName); br = new BufferedReader(fr); line = br.readLine(); if (line != null) System.out.println(line); } } 10. public static void main(String[] args) { int code; System.out.println("Code Character"); for(code = 32; code <= 127; code++) System.out.println(code + " " + (char) code); } 11. Make the instance variable private and supply a public accessor method. 12. Supply instance variables and a mutator method like this: // Instance variable x. private int x; // Boolean flag that tells if x has been written yet. private boolean hasBeenWritten = false; // Mutator method setX: public void setX(int newValue) { if (!hasBeenWritten) { x = newValue; hasBeenWritten = true; } } 13. int[] a = new int[50]; for(int i = 0; i <= 49; i++) a[i] = 2 * i + 1; 14. A class defined in another class (called the outer class). The inner class has access to all the private instance variables of the outer class. Inner classes are used to implement classes that implement ActionListener. 15. (a) Declare the reference variables for the controls as instance variables. In the constructor: (b) Create the control objects. (c) Add the controls to the panel. (d) Call setContentPane for the panel. (e) Define an inner class ClickDetector that implements ActionListener. (f) In this inner class, define an actionPerformed method which is the event handler for the click event. (g) Create a ClickDetector object. (h) Add the ClickDetector object to the button object. 16. public void actionPerformed(ActionEvent e) { String stars = "*"; for(int i = 0; i <= 19; i++) { t[i].setText(stars); stars += "*"; } } 17. public void actionPerformed(ActionEvent e) { int i, sum, count; for(i = 0; i <= t.length - 1; i++) { sum += t[i].getText().length(); count++; } ave.setText(String.valueOf( (double) sum / count)); } 18. public void actionPerformed(ActionEvent e) { for(int i = 0; i <= 19; i++) if(e.getSource() == b[i]) { t.setText(String.valueOf(i)); break; } } 19. public static void main(String[] args) { String[] a = new String[200]; FileReader fr = new FileReader("c:\\Words.txt"); BufferedReader br = new BufferedReader(fr); int nWords = 0; String line; while((line = br.readLine()) != null) { a[nWords] = line; nWords++; } } 20. public static void main(String[] args) { String[] a = {"racoon", "opossum", "skunk", "rabbit"}; FileWriter fw = new FileWriter("c:\\Words.txt"); BufferedWriter bw = new BufferedWriter(fw); PrintWriter pw = new PrintWriter(bw); String line; for(int i = 0; i <= a.length - 1; i++) { pw.println(a[i]); } } 21. The readLine method of the BufferedReader object returns a null value. 22. public static void main(String[] args) { FileReader fr = new FileReader("c:\\Words.txt"); BufferedReader br = new BufferedReader(fr); String line; int i, total = 0; while((line = br.readLine()) != null) { for(i = 0; i <= line.length() - 1; i++) if (line.charAt(i).equals(" ")) total++; total--; } } 23. x[0] x[1] x[2] x[3] +------+------+------+------+ Output: | o | o | o | o | 61 +---|--+---|--+---|--+---|--+ 94 | \ / | 61 | \ / | 94 | \ / | | \/ | | /\ | | / \ | | / \ | | | | | V V V V +---------+ +---------+ | s = 61 | | s = 63 | | s = 122 | | s = 189 | +---------+ +---------+