Here is part of the codes
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class ChooseColor extends JFrame implements ActionListener {
// your codes
public static void main(String[] args) {
JFrame f = new ChooseColor();
f.setSize(250,150);
f.setVisible(true);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
(b)
Rewrite the above program by using anonymous class
Question 2
Write a swing program with 3 buttons. Each buttons has a number on it. When you click on the buttons,
the number on it will be increased by 1.
At the beginning

After you click the first button twice, the second button 3 times, and the last button once

Fill in the codes
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class Click extends JFrame implements ActionListener {
// your codes
public Click() {
// your codes
}
public void actionPerformed(ActionEvent e) {
// your codes
}
public static void main(String[] args) {
JFrame f = new Click();
f.pack();
f.setVisible(true);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
Question 3
(a) What is the difference between RuntimeException and checked exception ?
(b)
Suppose ExceptionA is a subclass of RuntimeException, ExceptionB is
not a subclass of RuntimeException.
Suppose methodA() throws ExceptionA, methodB() throws ExceptionB,
methodAB() throws both ExceptionA and ExceptionB.
Which of the following codes can be compiled.
(i)
public void method() {
methodA();
}
(ii)
public void method() {
methodB();
}
(iii)
public void method() throws ExceptionB {
methodB();
}
(iv)
public void method {
try {
methodA();
methodB();
} catch(ExcetpionB ex) {
// nothing
}
}
Question 4
(i)What is the output of the following program?
public class Test {
public static void main(String[] args) {
int a[] = {0,1,2,3,4};
try {
System.out.println(a[3]/a[0]);
a[2] = 5;
a[10] = 10;
} catch (ArrayIndexOutOfBoundsException ex) {
System.out.println("ArrayIndexOutOfBoundsException");
} catch (ArithmeticException ex) {
System.out.println("ArithmeticException");
} finally {
System.out.println("Finally");
System.out.println(a[2]);
}
}
}
Question 5
Write a class Date represents a date. Overwrite
- equals method
- toString method. The string is in month/day/year format
public class Date {
int day;
int month;
int year;
Date(int day, int month, int year) {
// your codes
}
}
Question 6
(a)
What is so important about type wrapper class?
(b)
Write a function Integer sum(Integer num1, Integer num2). The function
returns the sum of the 2 arguments.
(c)
How to use parseInt ? what kind of exceptions does it throw?
Question 7
(a)
Write a method long getSize(File file).
- It returns 0 when
file doesn't exist.
- If
file is a file, it return to size of the file.
- If it is a directory, it returns the total size of the files in this
directory and its subdirectories.
Example
dir1
|
+------subdir
| |
| +-------- file1 (size 100)
| |
| +-------- file2 (size 200)
|
|
+------file3 (size 300)
|
+------file4 (size 400)
Then getSize(new File("dir1")) returns
100 + 200 + 300 + 400 = 1000.
(b)
Write a function public void count(file folder). The function prints out the number of files and the number of folders
in the given folder.
Example, if the folder has 3 files and 2 folder. It prints out
File: 3
Folder: 2
(c)
Create directories :
dir
|
+-----dir1
|
+-----dir2
|
+-----dir3
Question 8
(a)
Write a method int countChar(File file, char ch) throws IOException. The method returns
the number of occurence of ch in the file.
(b)
Write a class with a main method. It counts the number of characters
(from 'a' to 'z') of a file named "in.txt" and print the output to a file name "count.txt". The output should
look like
a: 10
b: 25
.....
or
IOException
Fill in the codes
import java.io.*;
public class Count {
public static int countChar(File file, char ch) throws IOException {
// your codes
}
public static void main(String[] args) {
// your codes
}
}
Question 9
(a)
Suppose Vector v is a vector consist of Integers. Write a function
public void increaseByOne(Vector v). The function increases every element in v
by 1.
(b)
Suppose Vector v is a vector consist of Strings. Write a function
Vector remove(Vector v, char ch).
The function returns a new Vector consists of Strings
from Vector v without character ch .
Example, if v consists of {"hello", "how", "are", "you"}. Then remove(v, 'h') returns
a vector with {"are", "you"}. All the strings with character 'h' are removed.
(c)
Explain how to use contains and indexOf method in Vector class.
Question 10
Assume this class hierarchy:
Animal
|
Mammal
|
--------------------------------------------
| | | |
Dog Cat Raccoon SwampThing
(implements (implements
Washer) Washer)
(a)
Consider the following code:
1. Dog rover, fido;
2. Animal anim;
3.
4. rover = new Dog();
5. anim = rover;
6. fido = (Dog)anim;
Which of the statements below is true? (Choose one.)
A) Line 5 will not compile
B) Line 6 will not compile
C) The code will compile but will throw an exception at line 6.
D) The code will compile and run.
E) The code will compile and run, but the cast in line 6 is not required and can be eliminated.
(b)
Consider the following code:
1. Cat sunflower;
2. Washer wawa;
3. SwampThing pogo;
4.
5. sunflower = new Cat();
6. wawa = sunflower;
7. pogo = (SwampThing)wawa;
Which of the statements below is true? (Choose one.)
A) Line 6 will not compile; an explicit cast is required to convert a Cat to a Washer.
B) Line 7 will not compile, because you cannot cast an interface to a class.
C) The code will compile and run, but the cast in line 7 is not required and can be eliminated.
D) The code will compile but will throw an exception at line 7, because runtime conversion from an interface to a class is not permitted.
E) The code will compile but will throw an exception at line 7, because the runtime class of wawa cannot be converted to type SwampThing.