ANSWERS FOR PRACTICE FINAL -- SPRING 2003 1. x[0] x[1] x[2] x[3] x[4] x[5] x[6] x[7] x[8] a b c y +----+----+----+----+----+----+----+----+----+----+----+----+----+ | 12 | 19 | 34 | 41 | 57 | 65 | 79 | 91 | 99 | 0 | 8 | 4 | 79 | | | | | | | | | | | 4 | 8 | 6 | | Output: Index is 6 2. (a) +-------------------------------------------------+ | Patient | +-------------------------------------------------+ | - name | | - age | | - diagnosis | | - doctor | +-------------------------------------------------+ | + Patient(n:String, a:int, d:String, doc:String)| | + getName() : String | | + getAge() : int | | + getDiagnosis() : String | | + getDoctor() : String | | + toString() : String | +-------------------------------------------------+ // Part (b) public class Patient { private String name; private int age; private String diagnosis; private String doctor; public Patient(String n, int a, String d, String doc) { name = n; age = a; diagnosis = d; doctor = doc; } public String getName() { return name; } public int getAge() { return age; } public String getDiagnosis() { return diagnosis; } public String getDoctor() { return doctor; } public String toString() { return name + " " + age + " " + diagnosis + " " + doctor; } } // Part (c) public static void main(String[] args) { Patient p; p = new Patient("Bates,Jim", 35, "Fractured tibia", "Morgan,Dawn"); System.out.println(p); System.out.println(p.getName()); System.out.println(p.getAge()); System.out.println(p.getDiagnosis()); System.out.println(p.getDoctor()); } // Part (d). The output is: Bates, Jim 35 Fractured tibia Morgan, Dawn Bates, Jim 35 Fractured tibia Morgan, Dawn 3. int count = 0; for(int i = 0; i < k.length; i++) if(k[i].getName().equals("Emily")) count++; System.out.println("Count = " + count); 4. public static boolean isVowel(char x) { return x=='A' || x=='E' || x=='I' || x=='O' || x=='U' || x=='a' || x=='e' || x=='i' || x=='o' || x=='u'; } public static String removeVowels(String s) { String t = ""; for(int i; i < s.length(); i++) if(!isVowel(s.charAt(i))) t += s.charAt(i); return t; } 5. public class RemoveVowels extends Applet implements ActionListener { private TextField input, output; private Button remove; public void init() { input = new TextField(12); output = new TextField(12); remove = new Button("Remove Vowels"); add(input); add(output); add(remove); button.addActionListener(this); } public void actionPerformed(ActionEvent e) { String s, t; s = input.getText(); t = removeVowels(s); output.setText(t); } }