Q1(32 pts)
a) 3pts.
Ouput 0
Do What?
Output 1
One Two
Output 2
Two
Output 3
Three Do What?
Output 4
Do What?
b) 2 pts
2,4

c) The question is cancelled
B.
The conversion in line 6 is fine(class to interface), but the conversion in line 7 (interface to class) is not allowed. A cast in line 7 will make the code compile, but then at runtime a ClassCastException will be thrown, because Washer and Swampthing are incompatible.
d) 3 pts
10020
e) 2 pts
1,4
f) 2 pts
e
g) 10 pts
output 1
For loop
Error B 0
-1

output 2
For loop
End For Loop
2

output 3
For loop
End For Loop
Error D 0
-1

output 4
For loop
Error A 3
-1

output 5
For loop
Error C 2
-1
h) 5 pts

i) 5 pts

Q2 (20 pts)
a) 3 pts.
1pt for finding the error, 2 pts for correction.
Declare the class abstract or implement the missing method.
b) 3 pts.
public class ClassB {
    int div(int a, int b) {
        int result; // must declare it outside of other blocks.
        try {
           result = a/b;
        } catch (ArithmeticException ex) {
            System.out.println("something is wrong");
            return 0;
        }
        
        return result;
    }
}
c) 3 pts
import java.awt.*;

public class ClassC {
    public static void main(String[] args) {
        Color c = new Color(0.5f,0.5f,0.5f);
        Color d = new Color(1,2,3);
        Color e = d.red.blue.green; // this line is OK
    }
}
d) 3 pts
import javax.swing.*;

public class ClassD {
    public static void main(String[] args) {
        JLabel[] label = new JLabel[5];
        JFrame f = new JFrame();
        for(int i = 0; i < 5; i++) {
            label[i] = new JLabel();
            label[i].setText("" + i); // or label[i].setText(Integer.toString(i))
            f.getContentPane().add(label[i]);
        }
    }
}
e) 8 pts
1 pt for each method(total 5 pts). 1pt for each correction (total 3pts).
public class Test {
    public void test1() {
            Ex.methodA();
    }
    
    public void test2() {
        try {
            Ex.methodB();
        }  catch (ExceptionB ex) {
        }
    }
    
    public void test3() {
         try {
             Ex.methodAB();
         } catch (Exception B ex) {
         }
    }
    
    public void test4() {
         try {
            Ex.methodAB();
         } catch (ExceptionA ex) {
         } catch(ExceptioinB  ex) {
         }
    }
    public void test5() {
         try {
             Ex.methodAB();
         } catch (ExceptionB ex) {
         }
    }
}
Q3 (20 pts)
a) 10 pts, 2 pts for each change( 4 of them) 2 pts for no compiler error
public class Rewrite {
    public static void main(String[] args) {
        javax.swing.JFrame f = new javax.swing.JFrame("Hi");
        f.getContentPane().setLayout(new java.awt.GridLayout(1,1));
        f.getContentPane().add(new javax.swing.JLabel("abc"));
        f.setSize(100,100);
        f.setVisible(true);
    }
}
b) 10 pts.
import java.io.*;

public class  Count {
    public static int Count(File file) {
        File[] filelist = file.listFiles();
        if(filelist == null ) {
            return 0;
        } else {
           return file.listFiles().length;
        }
    }
}
    
or
import java.io.*;

public class  Count {
    public static int Count(File file) {
        if(file.isDirectory()) {
            File[] filelist = file.listFiles();
           return file.listFiles().length;
        } else {
        return 0;
       }
    }
}
Q4 (28 pts)
a) 5 pts

b) 6 pts . 2 pts each
A : increase the number on display by 3.
B : increase the number on display by 2.
C : reset the number on display to 3.
c) 2 pts
8
d) 15 pts
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

public class ActionTest2 extends JFrame {
    JButton buttonA = new JButton("A");
    JButton buttonB = new JButton("B");
    JButton buttonC = new JButton("C");
    JLabel display = new JLabel("0", JLabel.CENTER);
    Container contentPane = getContentPane();
    
    public ActionTest2() {
        contentPane.add(buttonA, BorderLayout.CENTER);
        contentPane.add(buttonB, BorderLayout.EAST);
        contentPane.add(buttonC, BorderLayout.WEST);
        contentPane.add(display, BorderLayout.SOUTH);
        
        buttonA.addActionListener(new ActionListener() {
              public void actionPerformed(ActionEvent e) {
                      int num = Integer.parseInt(display.getText()) + 3;
                      display.setText("" + num);
              }
       });
        
     
      buttonB.addActionListener(new ActionListener() {
              public void actionPerformed(ActionEvent e) {
                      int num = Integer.parseInt(display.getText()) + 2;
                      display.setText("" + num);
              }
       });
        
       buttonC.addActionListener( new ActionListener() {
           public void actionPerformed(ActionEvent e) {
               display.setText("" + 3);
           }
       });        
    }
    
    public static void main(String[] args) {
        ActionTest2 f = new ActionTest2();
        f.setSize(200,100);
        f.setVisible(true);
    }
}
Q5 (25 pts)
import java.io.*;

public class TimeCal {
    public static void main(String[] args) {
        BufferedReader in = null; 
        PrintStream out = out = null;  
        try {
             in = new BufferedReader(new FileReader(args[0]));
             out = new PrintStream(new FileOutputStream(args[1]));
        } catch (IOException ex) {
            System.out.println("IOException");
            System.exit(0);
        }
        
        String line = null;
        String[] lineBreak;
        int i = 0;
        try {
             while((line=in.readLine())!=null) {           
                 lineBreak = line.split("-");
                out.println(line + " " + ( second(lineBreak[1])- second(lineBreak[0])) + " seconds.");
            }
        } catch (IOException ex) {
            System.exit(0);
        }
        
        try {
            in.close();
            out.close();
        } catch (IOException ex) {
        }
    }
  
    /** time is in hh:mm:ss format */
    public static int second(String time) {
        try {
            String[] part = time.split(":");
            int hh = Integer.parseInt(part[0]);
            int mm = Integer.parseInt(part[1]);
            int ss = Integer.parseInt(part[2]);
            return 60*60*hh + 60*mm + ss;
        } catch (Exception ex) {
            return -1;
        }
    }
}
Q6 (25 pts)
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

public class ColorGame extends JFrame implements ActionListener {
    private Color[] colors = {Color.red, Color.blue, Color.yellow, Color.green,  
                            Color.pink, Color.black, Color.white,  Color.orange};
    private String[] name = {"red", "blue", "yellow", "green", 
                        "pink", "black", "white", "orange"};
    private JButton[] colorButton = new JButton[8];    
    
    private JLabel topLabel = new JLabel();
    private JLabel correctLabel = new JLabel();
    private JButton nextButton = new JButton("Next Question");
    
    private JPanel colorPanel = new JPanel(new GridLayout(2,4));
    private JPanel bottomPanel = new JPanel(new GridLayout(1,2));
    
    private Container contentPane = getContentPane();
    
    String currentColor;
    
    
    public ColorGame() {
        for(int i = 0; i < colorButton.length; i++) {
            colorButton[i] = new JButton();
        }
        
        for(int i = 0; i < colorButton.length; i++) {
            colorButton[i].setBackground(colors[i]);
            colorButton[i].setActionCommand(name[i]);
            colorButton[i].addActionListener(this);
            colorPanel.add(colorButton[i]);
        }
        
        bottomPanel.add(correctLabel);
        bottomPanel.add(nextButton);
        
        currentColor = randomColor();
        topLabel.setText(currentColor);
        nextButton.addActionListener(this);
        
        contentPane.add(topLabel, BorderLayout.NORTH);
        contentPane.add(colorPanel);
        contentPane.add(bottomPanel, BorderLayout.SOUTH);
        
    }
    
    public void actionPerformed(ActionEvent e) {
        if(e.getSource() == nextButton) {
            currentColor = randomColor();
            topLabel.setText(currentColor);
            correctLabel.setText("");
        } else {
            if(e.getActionCommand().equals(currentColor)) {
                correctLabel.setText("Correct Color!");
            } else {
                correctLabel.setText("Wrong Color! Try Again!");
            }
        }
            
    }
    
    private String randomColor() {
        return name[(int)(name.length*Math.random())];
    }
    
    public static void main(String[] args) {
       ColorGame game = new ColorGame();
       game.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       game.setSize(290,140);
       game.setVisible(true);
    }
}