CSC 211 -- ANSWERS FOR PRACTICE FINAL C WARNING: The code in Problems 4, 5, and 6 has not been tested. Watch out for typos. 1. a. See notes and textbook. b. a == b compares addresses of String objects, a.equals(b) checks to see if the strings have exactly the same characters. c. public static void main(String[] args) { int i, sum = 0; for(i = 0; i < args.length; i++) sum += Integer.parseInt(args[i]); System.out.println("Sum is " + sum + "."); } 2. After swapping a[2] and a[4], the a array looks like this: a[0] a[1] a[2] a[3] a[4] --------+--------+------------+------+---------- "grapefruit" "orange" "watermelon" "date" "lemon" Then this is how the variables i, j, k, and m change i j k m k.charAt(m) ----+---+------------+---+------------- 0 2 "watermelon" 0 'w' 1 1 "orange" 0 'o' 2 2 "watermelon" 4 'r' Output: 3 3 "date" 0 'd' wordgame 4 1 "orange" 4 'g' 5 3 "date" 1 'a' 6 4 "lemon" 2 'm' 7 4 "lemon" 1 'e' 3. x[0] x[1] x[2] ====== ======== ======== a b a b refers to same ---+------- ---+----- object as x[0] 15 "xxxdog" 27 "yyycat" 20 "xxxdogzzz" 29 "yyycat2" 25 "xxxdogzzzzzz" Output: 25 xxxdogzzzzzz 29 yyycat2 25 xxxdogzzzzzz 4b. public class Student { private String name; private int id; private int totalHours; private int totalGradePoints; public Student(String n, int i, int h, int g) { name = n; id = i; totalHours = h; totalGradePoints = g; } public String toString() { return name + " " + id + " " + totalHours + " " + totalGradePoints; } public double getGpa() { return (double) totalGradePoints / totalHours; } } public class Main { public static void main(String[] args) { String n; int i, h, g; BufferedReader br = new BufferedReader( new FileReader("c:\\StudentInfo.txt"); n = br.readLine(); i = Integer.parseInt(br.readLine()); h = Integer.parseInt(br.readLine()); g = Integer.parseInt(br.readLine()); Student student = new Student(n, i, h, g); } } 5. public static void main(String[] args) { double minVal = 2.0; // 2.0 is a value larger than any // value in the array. double[] a = new double[100]; int i; for(i = 0; i < 100; i++) a[i] = Math.random(); for(i = 0; i < 100; i++) minVal = Math.min(a[i], minVal); System.out.println("Minimum value is " + minVal + "."); } 6. public class Main { public static void main() { // The initials ui mean user interface. UI ui = new UI(); } } import javax.swing.*; import java.awt.*; import java.awt.event.*; public class UI extends JFrame { private JPanel panel; private JTextField field; private JButton button; public UI() { panel = new JPanel(); field = new JTextField(8); button = new JButton("# Vowels"); panel.add(field); panel.add(button); setContentPane(panel); button.addActionListener(new CD()); setSize(100, 100); show(); } private class CD implements ActionListener { public void actionPerformed(ActionEvent e) { String word; char x; int count = 0, i; word = field.getText().toUpperCase(); for(i = 0; i < word.length(); i++) { x = word.charAt(i); if (x == 'A' || x == 'E' || x == 'I' || x == 'O' || x == 'U') count++; } field.setText(String.valueOf(count)); } } }