CSC 211 -- ANSWERS FOR PRACTICE FINAL A Warning, the code is not tested to look out for minor typos. 1. s.a s.b t.a t.b u.a u.b Output: --------+-------+-------+---------+------+--------- 17 xyz 11 "xyz" 100 "apple3" 11 "7peach5" 100 apple38 17 "xyz" 100 "apple38" 11 "7peach5" 11 7peach5 2. a. See notes. b. Long version. public static void main(String[] args) { int x, y, result; FileReader fr = new FileReader("c:\\numbers.txt"); BufferedReader br = new BufferedReader(fr); x = Integer.parseInt(br.readLine()); y = Integer.parseInt(br.readLine()); result = x + y; System.out.println(result); } b. Short (macho) version. public static void main(String[] args) { FileReader fr = new FileReader("c:\\numbers.txt"); BufferedReader br = new BufferedReader(fr); System.out.println(Integer.parseInt(br.readLine()) + Integer.parseInt(br.readLine())); } c. Long version public static void main(String[] args) { int x, y, result; x = Integer.parseInt(args[0]); y = Integer.parseInt(args[1]); result = x + y; System.out.println(result); } c. Short (macho) version public static void main(String[] args) { System.out.println(Integer.parseInt(args[0]) + Integer.parseInt(args[1])); } 3. a. Example Instance variables: departureDateAndTime, arrivalDataAndTime, maxPassengers, numbPassengers, orig, dest. Example Methods: Constructor, toString, getOrig, getDest, addPassenger, removePassenger. b. public class AirlineFlight { private int maxPassengers, numbPassengers; private String orig, dest; public AirlineFlight(String anOrig, String aDest) { maxPassengers = 0; numbPassengers = 0; orig = anOrig; dest = aDest; } public String toString() { return "Max=" + maxPassengers + " N=" + numbPassengers + " Orig=" + orig + " Dest=" + dest; } public void getOrig() { return orig; } public void getDest() { return dest; } public void addPassenger() { numbPassengers++; } public void removePassenger() { numbPassengers--; } } c. Test the AirlineFlight class. public static void main(String[] args) { AirlineFlight x = new AirlineFlight("ORD", "SAN"); System.out.println(x.getOrig()); System.out.println(x.getDest()); System.out.println(x); } d. Assume an array of AirlineFlight objects a that is already read in from a file. Assume that there are no unused slots in the array. This part is considered to hard to be on the final exam. ORD is the 3 letter abbreviation for Chicago O'Hare and SAN is the abbreviation for San Diego. public static void main(String[] args) { ... int i, j; for(i = 0; i <= a.length - 1; i++) for(j = 0; j <= a.length - 1; j++) if(a[i].getOrig().equals("ORD") && a[j].getDest().equals("SAN") && a[i].getDest().equals(a[j].getOrig())) System.out.println( "Orig=ORD Middle= " + a[i].getDest() + " Dest=SAN"); } 3. public class Die { private int face; public Die() { roll(); } public void roll() { return (int)(Math.random() * 6) + 1; } public int getFace() { return face; } } public class Main { public static void main(String[] args) { Die x = new Die(); int[] total = new int[7]; int i; for(i = 1; i <= 100; i++) { x.roll(); total[x.getFace()]++; } for(i = 1; i <= 6; i++) System.out.println("Face " + i + ": " + total[i]); } } 5. The import statements are omitted. The main statement is the same for both Problems 5 and 6: public class Main { public static void main(String[] args) { UI x = new UI(); } } public class UI extends JFrame { JTextField t; JButton b; JPanel p; ClickDetector cd; public UI() { super("To Upper"); t = new JTextField(5); b = new JButton("To Upper"); p = new JPanel(); cd = new ClickDetector(); p.add(t); p.add(b); setContentPane(p); b.addActionListener(cd); setSize(150, 100); show(); } private class ClickDetector implements ActionListener { public void actionPerformed(ActionEvent e) { t.setText(t.getText().toUpperCase()); } } } 6. public class UI extends JFrame { JTextField[] t; JButton b; JPanel p; ClickDetector cd; public UI() { super("To Upper"); int i; t = new JTextField[6]; for(i = 0; i <= 5; i++) t[i] = new JTextField(10); b = new JButton("Average"); p = new JPanel(); cd = new ClickDetector(); p.add(t); p.add(b); setContentPane(p); b.addActionListener(cd); setSize(150, 300); show(); } private class ClickDetector implements ActionListener { public void actionPerformed(ActionEvent e) { int i, sum = 0; double ave; for(i = 0; i <= 4; i++) sum += t[i].getText().length(); ave = sum / 5.0; t[5].setText(String.valueOf(ave)); } } }