Question 1
(a)
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class ChooseColor extends JFrame implements ActionListener {
private JLabel label = new JLabel();
private JPanel panel = new JPanel(new GridLayout(1,0));
private JButton[] buttons= new JButton[3];
private Container contentPane = getContentPane();
public ChooseColor() {
buttons[0] = new JButton("Red");
buttons[0].setBackground(Color.red);
buttons[1] = new JButton("Green");
buttons[1].setBackground(Color.green);
buttons[2] = new JButton("Blue");
buttons[2].setBackground(Color.blue);
label.setOpaque(true);
contentPane.add(label);
for(int i = 0; i < 3; i++) {
panel.add(buttons[i]);
buttons[i].addActionListener(this);
}
contentPane.add(panel, BorderLayout.SOUTH);
}
public void actionPerformed(ActionEvent e) {
if(e.getActionCommand().equals("Red")) {
label.setBackground(Color.red);
} else if(e.getActionCommand().equals("Green")) {
label.setBackground(Color.green);
} else {
label.setBackground(Color.blue);
}
}
public static void main(String[] args) {
JFrame f = new ChooseColor();
f.setSize(250,150);
f.setVisible(true);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
(b)
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class ChooseColor extends JFrame {
private JLabel label = new JLabel();
private JPanel panel = new JPanel(new GridLayout(1,0));
private JButton[] buttons= new JButton[3];
private Container contentPane = getContentPane();
public ChooseColor() {
buttons[0] = new JButton("Red");
buttons[0].setBackground(Color.red);
buttons[0].addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
label.setBackground(Color.red);
}
});
buttons[1] = new JButton("Green");
buttons[1].setBackground(Color.green);
buttons[1].addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
label.setBackground(Color.green);
}
});
buttons[2] = new JButton("Blue");
buttons[2].setBackground(Color.blue);
buttons[2].addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
label.setBackground(Color.blue);
}
});
label.setOpaque(true);
contentPane.add(label);
for(int i = 0; i < 3; i++) {
panel.add(buttons[i]);
}
contentPane.add(panel, BorderLayout.SOUTH);
}
public static void main(String[] args) {
JFrame f = new ChooseColor();
f.setSize(250,150);
f.setVisible(true);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
Question 2
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class Click extends JFrame implements ActionListener {
JButton[] buttons = new JButton[3];
Container contentPane = getContentPane();
public Click() {
contentPane.setLayout(new GridLayout(0,1));
for(int i = 0; i < buttons.length; i++) {
buttons[i] = new JButton("0");
buttons[i].addActionListener(this);
contentPane.add(buttons[i]);
}
}
public void actionPerformed(ActionEvent e) {
// we can use e.getActionCommand
// try to show you different methods of doing it
JButton button = (JButton)e.getSource();
int num = Integer.parseInt(button.getText());
button.setText(Integer.toString(num+1));
}
public static void main(String[] args) {
JFrame f = new Click();
f.pack();
f.setVisible(true);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
Question 3
(a) Read my notes
(b)(i),(iii),(iv) can be compiled. We don't have to "take care" runtime excetpion.
Question 4
ArithmeticException
Finally
2
Explanation: 3/0 throws the exception first. The codes after it won't be exceuted.
Question 5
public class Date {
int day;
int month;
int year;
Date(int day, int month, int year) {
this.day = day;
this.month = month;
this.year = year;
}
public boolean equals(Object obj) {
if(obj instanceof Date) {
return ((Date)obj).day == day &&
((Date)obj).month == month &&
((Date)obj).year == year;
} else {
return false;
}
}
public Object clone() {
return new Date(day, month, year);
}
public String toString() {
return month + "/" + day + "/" + year;
}
}
Question 6
(a) : refer to my notes
(b):
Integer Sum(Integer num1, Integer num2) {
int s = num1.intValue() + num2.intValue();
return new Integer(s);
}
(c): read my handout.
Question 7
(a)
public static long getSize(File f) {
if(f.isFile()) { // if it is a file
return f.length();
} else { // if it is a folder
long length = 0;
File[] files = f.listFiles(); // list files
for(int i = 0; i < files.length; i++) {
length+=getSize(files[i]); // recursive function
}
return length;
}
}
(b)
public static void count(File f) {
int fileNum = 0;
int folderNum = 0;
File[] files = f.listFiles();
for(int i = 0; i < files.length; i++) {
if(files[i].isFile()) { // it is a file
fileNum++;
} else {
folderNum++;
}
}
System.out.println("File: " + fileNum);
System.out.println("Folder: " + folderNum);
}
(c)
new File("dir/dir1").mkdirs();
new File("dir/dir2").mkdirs();
new File("dir/dir3").mkdirs();
Question 8
import java.io.*;
public class Count {
public static int countChar(File file, char ch) throws IOException {
FileInputStream in = new FileInputStream(file);
int c;
int count = 0;
while((c=in.read()) != -1) {
if(c == ch) {
count++;
}
}
in.close();
return count;
}
public static void main(String[] args) {
PrintStream out = null;
File f = new File("in.txt");
try {
out = new PrintStream(new FileOutputStream("count.txt"));
for(char i = 'a'; i < 'z'+1; i++) {
out.println(i + ": " + countChar(f, i));
}
} catch (IOException ex) {
if(out != null) {
out.println("IOException");
out.close();
}
}
}
}
Question 9
(a)
public static void increaseByOne(Vector v) {
for(int i = 0; i < v.size(); i++) {
Integer x = (Integer)v.get(i);// casting
int y = x.intValue();// get int value
v.set(i, new Integer(y+1));
}
}
(b)
public static Vector remove(Vector v, char ch) {
Vector result = new Vector();
for(int i = 0; i < v.size(); i++) {
String s = (String)v.get(i); // casting
if(s.indexOf(ch) == -1) { // s has no ch
result.add(s); // add
}
}
return result;
}
(c)
Read java doc
Question 10
(a)
Correct selection is: D
The code will compile and run; the cast in line 6 is required, because changing an Animal to a Dog is going "down" the tree.
(b)
Correct selection is: E
The cast in line 7 is required. Answer D is a preposterous statement expressed in a tone of authority.