Final Exam Review Question Answers Java Source Code: 7. Look up the Character class in the Java Class Library documentation. Find five interesting methods and test them. ======================================================================= public static void main(String[] args) { Character c = 'A'; Character d = 'b'; Character e = '5'; System.out.println(c.charValue( )); System.out.println(c.compareTo(d)); System.out.println(Character.isDigit(c)); System.out.println(Character.isDigit(e)); System.out.println(Character.isLetter(c)); System.out.println(Character.isLetter(e)); System.out.println(Character.getName(c)); System.out.println(Character.getName(d)); System.out.println(Character.getName(e)); } 8. Write a recursive method named intToWords that converts an integer less than one billion to words. ======================================================================= public class ToWords { public static void main(String[] args) { System.out.println(toWords(5)); System.out.println(toWords(13)); System.out.println(toWords(64)); System.out.println(toWords(738)); System.out.println(toWords(9473)); System.out.println(toWords(840987654)); System.out.println(toWords(5)); System.out.println(toWords(5)); } public static String toWords(int num) { String[ ] ones = {"", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"}; String[ ] teens = {"ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eightteen", "nineteen"}; String[ ] tens = {"", "ten", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety"}; if (num < 0) { return "Negative Number"; } if (num == 0) { return "zero"; } else if (num < 10) { return ones[num]; } else if (num < 20) { return teens[num - 10]; } else if (num < 100) { int left = num / 10; int right = num % 10; return tens[left] + " " + ones[right]; } else if (num < 1000) { int left = num / 100; int right = num % 100; return ones[left] + " hundred " + toWords(right); } else if (num < 1000000) { int left = num / 1000; int right = num % 1000; return toWords(left) + " thousand " + toWords(right); } else if (num < 1000000000) { int left = num / 1000000; int right = num % 1000000; return toWords(left) + " million " + toWords(right); } else { return "Out of range"; } } } ======================================================================= 9. Write a main method that reads a list of strings, adding them to an array list. Then print the words back in reverse order. ======================================================================= public static void main(String[ ] args) { ArrayList list = new ArrayList( ); list.add("dog"); list.add("cat"); list.add("geril"); list.add("canary"); for(int i = list.size-1; i >= 0; i--) { System.out.println(s + " "); } } ======================================================================= 10. Declare an interface Filter like this: public interface Filter { boolean accept(Object x); } Write a method with the header public static ArrayList collectAll( ArrayList objects, Filter f) that returns all the objects in the array list that are accepted by the given filter.  Provide a class ShortWordFilter, whose filter method accepts all strings of length < 5.  Then write a main method that reads all words from System.in, puts them into an array list, calls collectAll, and prints a list of the words in the returned array list.   ======================================================================= public interface Filter { boolean accept(Object x); } ----------------------------------------------------------------------- public class ShortWordFilter implements Filter { public boolean accept(Object obj) { return (obj instanceof String && ((String) obj).length( ) < 5); } } ----------------------------------------------------------------------- import java.util.ArrayList; public class Main { public static void main(String[] args) { ArrayList list = new ArrayList( ); list.add(5); list.add("elephant"); list.add("dog"); list.add("cat"); list.add("wolf"); list.add("tiger"); list.add(true); ShortWordFilter swf = new ShortWordFilter( ); ArrayList results = collectAll(list, swf); System.out.println(results); } public static ArrayList collectAll( ArrayList objects, Filter f) { ArrayList filtered = new ArrayList( ); for(Object obj : objects) { if (f.accept(obj)) { filtered.add(obj); } } return filtered; } } ======================================================================= 11. Write classes Point and PointList. The Point class has the instance variables int x and int y and method toString, with getters and setters for x and y. The PointList has an array list to hold the points and these methods: add, display, graph, getCount, toString. Points are only added to the array list if they are between 0 and 19. ======================================================================= import java.io.Serializable; public class Point implements Serializable { private int _x; private int _y; public Point(int theX, int theY) { _x = theX; _y = theY; } public Point( ) { _x = 0; _y = 0; } public int getX( ) { return _x; } public void setY(int theY) { _y = theY; } @Override public String toString( ) { return String.format("(%d, %d)", _x, _y); } } ----------------------------------------------------------------------- import java.io.*; import java.util.*; public class PointList { private ArrayList _col; public PointList( ) { _col = new ArrayList( ); } public Point get(int index) { return _col.get(index); } public void add(Point p) { _col.add(p); } public void remove(int index) { _col.remove(index); } @Override public String toString( ) { String output = "["; for(Point p : _col) { output += p.toString( ) + ", "; } return output += "]"; } public void save( ) throws IOException { // Serialize array list. ObjectOutputStream outStream = new ObjectOutputStream( new FileOutputStream("points.ser")); outStream.writeObject(_col); // Close outStream. outStream.close( ); } public void load( ) throws IOException, FileNotFoundException { // Serialize array list. ObjectInputStream inStream = new ObjectInputStream( new FileInputStream("points.ser")); _col = (ArrayList) inStream.readObject(_col); // Close outStream. inStream.close( ); } ----------------------------------------------------------------------- import java.io.IOException; public class TestPoints { public static void main(String[] args) throws IOException, FileNotFoundException { PointList pList = new PointList( ); pList.add(new Point(3, 6)); pList.add(new Point(4, 5)); pList.add(new Point(1, 9)); // Comment out save and uncomment code below to test load pList.save( ); // pList.load( ); // pList.clear( ); // System.out.println(pList); } } =======================================================================