| Last Name:_________________ | First Name:________________ |
| Student ID:_________________ |
| Qs | Q1(5pts) | Q2(16pts) | Q3(12pts) | Q4(8pts) | Q5(15pts) | Q6(17pts) | Q7(17pts) | Total(90pts) |
| Pts |
Q1 (5pts)
Circle the correct answer. Unless otherwise stated, you should circle only one answer.
(a). Which of the following is an invalid identifier?
A) _hello
B) us$1
C) car-Weight
D) abc2def
(b) You execute the code below in an empty directory. What is the result?
1. File f1 = new File("dirname");
2. File f2 = new File(f1, "filename");
A) A new directory called dirname is created in the current working directory.
B) A new directory called dirname is created in the current working directory.
A new file called filename is created in directory dirname.
C) A new directory called dirname and a new file called filename are created, both in the current working directory.
D) A new file called filename is created in the current working directory.
E) No directory is created, and no file is created.
(c).Consider the following code:
import javax.swing.*;
import java.awt.*;
public class Temp {
public void test() {
System.out.println(JLabel.NORTH);
System.out.println(BorderLayout.NORTH);
System.out.println(JButton.CENTER);
System.out.println(Color.SOUTH);
}
}
Which line causes compiling error?
A) System.out.println(JLabel.NORTH);
B) System.out.println(BorderLayout.NORTH);
C) System.out.println(JButton.CENTER);
D) System.out.println(Color.SOUTH);
(d) Which of the following way of creating an instance of Color is incorrect?
A) Color color = Color.red
B) Color color = new Color(1,2,3)
C) Color color = new Color(0.5,0.5,0.5)
D) Color color = new Color(0.1f, 0.2f, 0.3f)
(e) Which of the followings are correct way to declare class or interface?
A) public class ClassA extends ClassB, ClassC
B) public class ClassA extends ClassB implements InterfaceA
C) public class ClassA implements InterfaceA, InterfaceB
D) public class ClassA extends ClassB implements InterfaceA, InterfaceB
E) public interface InterfaceA extends InterfaceB
Q2(16pts)
(a)
What is the output of java StaticTest?
public class StaticTest {
private static int x=1;
private static int y=2;
private void method() {
x++;
y--;
}
private static void method2() {
x-=2;
y+=2;
}
public static void main(String[] args) {
StaticTest t1 = new StaticTest();
StaticTest t2 = new StaticTest();
t1.method();
t2.y++;
StaticTest.y++;
System.out.println("Output 1:");
System.out.println("x = " + x);
System.out.println("y = " + y);
StaticTest.method2();
StaticTest.x+=t2.y;
t1.x*=2;
System.out.println("Output 2:");
System.out.println("x = " + x);
System.out.println("y = " + y);
}
}
Answser:
(b) What is the output of java FunctionCall?
public class FunctionCall {
public static void main(String[] args) {
CallMe me = new CallMe(3);
System.out.println("Output1 : " + me.x);
change1(me, 5);
System.out.println("Output2 : " + me.x);
me.x++;
change2(me, 8);
System.out.println("Output3 : " + me.x);
}
public static void change1(CallMe me, int x) {
me = new CallMe(x);
}
public static void change2(CallMe me, int x) {
me.setX(x);
}
}
class CallMe {
public int x;
public CallMe(int x) {
this.x = x;
}
public void setX(int x) {
this.x = x;
}
}
Answer:
(c) Sketch (draw) the output of java SwingTest
import java.awt.*;
import javax.swing.*;
public class SwingTest extends JFrame {
Container contentPane = getContentPane();
public SwingTest() {
contentPane.setLayout(new GridLayout(0,3));
for(int i = 0; i <= 10; i++) {
contentPane.add(new JButton("" + i));
}
JButton label = new JButton("X");
contentPane.add(label,7);
contentPane.remove(2);
}
public static void main(String[] args) {
SwingTest test = new SwingTest();
test.pack();
test.setVisible(true);
}
}
Answer:
(d) For the following program:
public class SplitTest {
public static void main(String[] args) {
String s = args[0];
String split = args[1];
String[] terms = s.split(split);
int sum = 0;
try {
for(int i = 0; i < 4; i++) {
sum+=Integer.parseInt(terms[i]);
}
} catch (NumberFormatException ex) {
System.out.println("NumberFormatException");
} catch (ArrayIndexOutOfBoundsException ex) {
System.out.println("ArrayIndexOutOfBoundsException");
} catch (NullPointerException ex) {
System.out.println("NullPointerException");
}
System.out.println(sum);
}
}
(i) What is the output of java SplitTest 1ccc2ccc3ccc4 ccc
(ii) What is the output of java SplitTest 1xx2xxAxx4 xx
(iii) What is the output of java SplitTest 3abc4abc5 abc
Q3(12pts)
Find out the compiling errors of the following programs. Circle the errors and fix them. Explain briefly why they are errors. You have to convince the grader that you really know the reasons. Write your corrections next to the errors you circle. There is exactly one error in each program.
(a)
import java.io.*;
public class Error1 {
FileInputStream in;
public Error1(FileInputStream in) {
this.in = in;
}
public void method(String[] args) {
int a = in.read();
if(a == 'A') {
System.out.println(a);
}
}
}
(b)
public class Error2 {
public static void main(String[] args) {
try {
int a = Integer.parseInt(args[0]);
} catch (NumberFormatException ex) {
return;
}
System.out.println(2*a);
}
}
(c)
import java.util.*;
public class Error3 {
private double x;
public Error3(double x) {
this.x = x;
}
public void triple() {
x*=3;
}
public static void main(String[] args) {
Error3 e1 = new Error3(0.5);
Error3 e2 = new Error3(0.9);
Error3 e3 = new Error3(3.45);
Vector v = new Vector();
v.add(e1);
v.add(e2);
v.add(e3);
Error3 e = v.firstElement();
e.triple();
System.out.println(e.x);
}
}
(d)
public interface Error4 {
int method(int x);
int method(String x);
}
class ClassA implements Error4 {
public int method(int x) {
return x;
}
public int method() {
return 1;
}
}
Q4(8pts)
(a)Write a method boolean isPicture(File file), the method returns true
if the name of the file ends with .gif or .jpg. The method returns false otherwise.
Hint: use toString
You can assume the file exists and is not a folder
Fill in your codes below, you can assume that all the necessary classes are properly imported.
boolean isPicture(File file) {
// Your codes
}
(b) Rewrite the following code without import statement:
Recall: Graphics and Color classes are in java.awt package.
JFrame is in javax.swing package.
import java.awt.*;
import javax.swing.*;
public class GraphicsTest extends JFrame {
public void paint(Graphics g) {
Color color = Color.black;
g.setColor(color);
}
}
Answer:
Q5 (15pts)
(a)
Consider the following class Person. The class has lastName(String),
firstName(String) and gender(char, 'm', 'M', 'f' or 'F').
Fill in the equals method such that it returns true if and only if they have the same
firstName and lastName.
Fill in your codes below
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;
}
public boolean equals(Object obj) {
//Your codes
}
}
(b)
Suppose v is a vector consists of Persons only.
Write a function Vector male(Vector v)
The function returns a new vector that contains only male (i.e. gender is m or M)
from the vector v. The original vector is unchanged.
Fill in your codes below, you can assume that all the necessary classes are properly imported.
You are only allowed to use methods from the table.
public static Vector male(Vector v) {
// your codes
}
(c)
Suppose v1 is a vector of distinct Persons.
v2 is a vector an of distinct Persons.
Write a function int common(Vector v1, Vector v2).
The function returns the number of Persons that appears in both v1 and v2.
Hint: you can go through v1 and see which Person is in v2
Fill in your codes below, you can assume that all the necessary classes are properly imported.
You are only allowed to use methods from the table.
int common(Vector v1, Vector v2) {
//Your codes
}
Q6(17pts)
(a)
Write a function boolean search(File file, String keyword).
The function returns true if the content of the file contains the keyword.
It returns false if (i) the keyword doesn't appear in the file. or
(ii) any kind of exception occurs.
(b)
Write a program name Search.java such that Search folder keyword
search the keyword in the files directly under the folder.
(i.e. we don't care about the files in the subfolder.)
The list of the file that contains of the keyword will be written to a file named out.txt.
Example, Search myhwfolder Charles, if file1.txt, file2.txt, file5.java, file9.html are the only files
under the folder myhwfolder contain
the word Charles, then a sample output will be
The files that contains the keywords: myhwfolder/file1.txt myhwfolder/file2.txt myhwfolder/file5.java myhwfolder/file9.html
Remark: I don't care if you use the full path name or a partial path name. toString
method gives the path name relative to the working folder.
If folder doesn't exists, then the following error message is written to out.txt and the program ends.
The folder doesn't exists.
If folder is not a directory, then the following error message is written to out.txt and the program ends.
The folder is not a directory.
You can assume that
out.txt for writting.Fill in your code below
public class Search {
boolean static boolean search(File file, String keyword) {
// your codes
}
}
public static void main(String[] args) throws Exception {
String folder = args[0];
String keyword = args[1];
// your codes
}
}
Q7(17pts)
You are going to write a simple game called GetMoney.java. If you don't understand how the program works,
you can test the program on my computer notebook.
Smiley.gif. random method in the code). There are 2 predefined functions. Please take a look at them before you write your program
random(Object[]) : randomizes the array disableAll() : disable all the buttons
In below are the description:
smiley.gif

1. Start of the game. 25 buttons are randomly placed. One of them has a smiley face. The other 24 have
different numbers (1-24) that represent the amount of money you can get when you click on it.

2. You can click on any button to get the money. Add the money to the total. The new total is displayed at the top.
Notice that once a button is clicked, the button will be disabled.

3. If you click on the button with the smiley face, you lost all your money. All the buttons will be disabled.
The game ends.

4. At any time you can click on the bottom button and take all the money. The total amount will be displayed on
the top. All the buttons will be disabled. The game ends.

Fill in your codes.
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class GetMoney extends JFrame implements ActionListener {
JButton[] buttons = new JButton[25];
JLabel moneyLabel = new JLabel("Total Amount: $0");
JButton bottomButton = new JButton("Leave and take all the money");
Container contentPane = getContentPane();
// some other variables
JPanel centerPanel = _________________________________________;
public GetMoney() {
super("Get Money");
// your codes
contentPane.add(moneyLabel, BorderLayout.NORTH);
contentPane.add(centerPanel);
contentPane.add(bottomButton, BorderLayout.SOUTH);
// You must use annoymous class for the bottomButton
bottomButton.addActionListener(
//Your codes
);
}
public void actionPerformed(ActionEvent e) {
// your codes
}
//disableAll() disables all the buttons
public void disableAll() {
for(int i = 0; i < 25; i++) {
buttons[i].setEnabled(false);
}
bottomButton.setEnabled(false);
}
//random(Object[]) randomizes the array
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);
}
}