1) 1 pt each (a) C (b) E (c) D (d) (e) A 2 a) 4pts Output 1: x = 2 y = 3 Output 2: x = 10 y = 5 b) 3pts) Output1 : 3 Output2 : 3 Output3 : 8 c) 3 pts. d) 6 pts) (i) 10 (ii) NumberFormatException 3 (iii) ArrayIndexOutOfBoundsException 12 Q3) 3 pts for each. 1pt for error, 1pt for correction 1 pt for explanation. (a) try catch in.read (b) declare int a outside of try block (c) type cast v.firstElement() (d) implements mathod(String x) Q4) a) boolean isPicture(File file) { // Your codes String s = file.toString(); return s.endsWith(".gif") || s.endsWith(".jpg"); } b) public class GraphicsTest extends javax.swing.JFrame { public void paint(java.awt.Graphics g) { java.awt.Color color = java.awt.Color.black; g.setColor(color); } } 5 a) import java.util.*; public class Person { public String lastName; public String firstName; public char gender; public Person(String lastName, String firstName, char gender) { this.lastName = lastName; this.firstName = firstName; this.gender = gender; } // total 5 pts public boolean equals(Object p) { //Your codes if(p instanceof Person) { // 1pt Person temp = (Person)p ; // 1pt return lastName.equals(temp.lastName) && //2 pts firstName.equals(temp.firstName); } else { return false; // 1pt } } // total 6 pts public static Vector male(Vector v) { Vector result = new Vector(); // 1 pt for creating the vector for(int i = 0; i < v.size(); i++) { // 1 pt for the lop Person p = (Person)result.get(i); // 1pt for getting the i-th if(p.gender == 'm' || p.gender == 'M') { // 1 pt for testing result.add(p); // 1 pt for adding } } return result; // 1 pt for returning } // total 4 pts public static int common(Vector v1, Vector v2) { int num = 0; for(int i = 0; i < v1.size(); i++) { // 1pt if(v2.contains(v1.get(i))) { // 3 pts num++; } } return num; } } import java.io.*; // 2 pts for general impression, like compiler error, does the // student's program make any sense at all? public class Search { public static boolean search(File file, String keyword) { try { BufferedReader in = new BufferedReader(new FileReader(file)); // 1pt String line = null; while((line = in.readLine()) != null) { // 1 pts for the loop if(line.indexOf(keyword) >= 0) { // 2 pts return true; } } return false; // 1 pts } catch (Exception ex) { // 2pts for catch exceptions and return return false; } } public static void main(String[] args) throws Exception { String folder = args[0]; String keyword = args[1]; // your codes File file = new File(args[0]); // 1 pt PrintStream out = new PrintStream(new FileOutputStream("out.txt")); // 1pt if(!file.exists()) { // 1 pt out.println("The folder doesn't exists."); return; } if(!file.isDirectory()) { // 1pt out.println("The folder is not a directory."); return; } // 4 pts File[] list = file.listFiles(); for(int i = 0; i < list.length; i++) { if(search(list[i], keyword)) { out.println(list[i]); } } } } import java.awt.*; import javax.swing.*; import java.awt.event.*; public class GetMoney extends JFrame implements ActionListener { // set up 3 pts JButton[] buttons = new JButton[25]; JLabel moneyLabel = new JLabel("Total Amount: $0"); JButton bottomButton = new JButton("Leave and take all the money"); JPanel centerPanel = new JPanel(new GridLayout(5,5)); Container contentPane = getContentPane(); int total = 0; public GetMoney() { super("Get Money"); contentPane.add(moneyLabel, BorderLayout.NORTH); for(int i = 0; i < 25; i++) { buttons[i] = new JButton(); buttons[i].setActionCommand("" + i); buttons[i].addActionListener(this); } random(buttons); for(int i = 0; i < 25; i++) { centerPanel.add(buttons[i]); } contentPane.add(centerPanel); contentPane.add(bottomButton, BorderLayout.SOUTH); // 4 pts bottomButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { disableAll(); moneyLabel.setText("You got $" + total + " !"); } }); } // 8 pts public void actionPerformed(ActionEvent e) { int x = Integer.parseInt(e.getActionCommand()); JButton button = (JButton)e.getSource(); if(x == 0) { button.setIcon(new ImageIcon("smiley.gif")); moneyLabel.setText("You lost all your money !"); disableAll(); } else { total+=x; button.setText("$"+x); button.setEnabled(false); moneyLabel.setText("Total Amount: $" + total); } } public void disableAll() { for(int i = 0; i < 25; i++) { buttons[i].setEnabled(false); } bottomButton.setEnabled(false); } public static void random(Object[] array) { for(int i = array.length-1; i >=0; i--) { int j = (int)(i*Math.random()); Object temp = array[j]; // swap; array[j] = array[i]; array[i] = temp; } } public static void main(String[] args) { GetMoney f = new GetMoney(); f.setSize(350,350); f.setVisible(true); } }