Q1. (5 pts) Write a method
int numOfChar(JTextField textfield). It returns the number of characters
of the text in the textfield.
int numOfChar(JTextField textfield) {
// your codes here
}
Q2. (10 pts) Consider the following code
public class Bruin {
private String name;
private String studentID;
public Bruin(String name, String studentID) {
this.name = name;
this.studentID = studentID;
}
/** return true if obj is a Bruin and has the same student ID */
public boolean equals(Object obj) {
if(obj instanceof Bruin) {
return studentID.equals(((Bruin)obj).studentID);
} else {
return false;
}
}
public String toString() {
return "Name : " + name + "\nStudent ID : " + studentID;
}
}
(a) What is the output of the following lines
Bruin b1 = new Bruin("Charles", "102222333");
System.out.println(b1);
(b) Consider the following code, write down the output of the following program
import java.util.*;
public class LinkedListTest {
public static void main(String[] args) {
LinkedList list = new LinkedList();
Bruin b1 = new Bruin("Charles", "102222333");
Bruin b2 = new Bruin("Amy", "202333444");
Bruin b3 = new Bruin("Betty", "902555666");
list.add(b1);
list.add(b2);
list.add(b3);
list.add(new Bruin("Mike", "222333444"));
list.add(new Bruin("Eve", "733444555"));
list.add(new Bruin("Dave", "333222111"));
list.remove(b2);
System.out.println("Output1:");
System.out.println(list.get(1));
list.set(2, new Bruin("Charles", "123456789"));
list.removeFirst();
System.out.println("Output2:");
for(int i = 1; i <= 3; i++){
System.out.println(list.get(i));
}
System.out.println("Output3:");
System.out.println(list.contains(b1));
System.out.println("Output4:");
System.out.println(list.indexOf(new Bruin("Charles", "123456789")));
System.out.println("Output5:");
System.out.println(list.indexOf(new Bruin("Unknown", "333222111")));
}
}
Write the output below.
Q3.(25pts) Write a class CheckBoxTest, it has 5 JCheckBox and
1 JLabel. The JLabel displays the number of
JCheckBox checked.
- At the beginning
- After you click checkbox 3 and 5.
- After you uncheck checkbox 5.
Hint: write a method int countChecked(). It returns the number
of checkbox checked.
Here is part of the code. You can implements interfaces after the class
declaration.
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class CheckBoxTest extends JFrame
{
// write your codes here
public static void main(String[] args) {
JFrame f = new CheckBoxTest();
f.pack();
f.setVisible(true);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
Q4.(35pts)
(a) Describe 2 ways to create Threads ? (extends/implements what classes?
override which method(s), how to start threads? What happens when you start threads?)
(b) Create a class name SleepThread.
It is a thread with 2 members: int sleepTime and String name.
The thread displays name in every sleepTime seconds.
Write the main method, so that it displays my name ("Charles") in every 3
seconds. Displays Mike's name ("Mike") in every 7 seconds. Displays Michelle's name ("Michelle")
in every 10 seconds.
Here is the output of the program.
Charles
Charles
Mike
Charles
Michelle
Charles
Mike
Charles
Charles
Michelle
(keep going ....)
I write down the time to help you understand the program (the time is not part of the
output).
Charles (3s)
Charles (6s)
Mike (7s)
Charles (9s)
Michelle (10s)
Charles (12s)
Mike (14s)
Charles (15s)
Charles (18s)
Michelle (20s)
(keep going.....)
Here is part of the code.
You can implements interfaces or extends class after the class
declaration.
public class SleepThread extends Thread
{
public String name;
public int sleepTime;
public SleepThread(String name, int sleepTime) {
this.name = name;
this.sleepTime = sleepTime;
}
public void run() {
while(true) {
try {
sleep(sleepTime*1000);
}
}
}
public static void main(String[] args) {
// your code
}
}