| Last Name:_________________ | First Name:________________ |
| Student ID:_________________ |
| Question | Q1 | Q2 | Q3 | Q4 | Q5 | Q6 | Total(85pts) |
| Score |
java ETest some arguments. If there is a new file created, write down the
file name. Leave it blank if no file is created.
import java.io.*;
public class ETest {
public static void main(String[] args) {
String filename = null;
int num;
FileOutputStream out = null;
try {
filename = args[0];
System.out.print(0);
String temp = args[1];
System.out.print(1);
num = Integer.parseInt(args[1]);
if(num == -1) {
System.out.print(-1);
out.write(9);
System.out.print(9);
System.exit(0);
} else {
System.out.print('X');
out = new FileOutputStream(filename);
}
/* print out the first character
of the arguments
starts from the second argment */
for(int i = 0; i < num; i++) {
// first character of args[i+2]
System.out.print(args[i+2].charAt(0));
out.write(args[i+2].charAt(0));
}
} catch (NullPointerException ex) {
System.out.print("A");
} catch(ArrayIndexOutOfBoundsException ex) {
System.out.print("B");
} catch (NumberFormatException ex) {
System.out.print("C");
} catch(FileNotFoundException ex) {
System.out.print("D");
} catch (IOException ex) {
System.out.print("E");
}
}
}
|
(a) java ETestOutput:____________________ File created:_______________ (b) java ETest test1Output:_____________________ File created:_______________ (c) java ETest test2 abcOutput:_____________________ File created:_______________ (d) java ETest folder 1 abc , here folder is the name of a folder exists in the folder contains the program. Output:_____________________ File created:_______________ (e) java ETest test3 2 abc def ghiOutput:_____________________ File created:_______________ (f) java ETest test4 -1 abc def ghiOutput:_____________________ File created:_______________ (g) java ETest test5 3 xxx yyyOutput:_____________________ File created:_______________ (h) java ETest test6 3 xxx yyyThe programs fails to write to the file. Output:_____________________ File created:_______________ (i) java ETest test7 2 aaa bbbtest7 already exists in the working folder. Output:_____________________ File created:_______________ |
java Picture?
import java.awt.*;
import javax.swing.*;
public class Picture extends JFrame {
public void paint(Graphics g) {
g.setColor(Color.red);
g.fillOval(50,50,200,200);
g.setColor(Color.blue);
g.fillOval(100,100,100,100);
g.setColor(Color.green);
g.drawRect(150,150,100,50);
g.drawString("A", 150,150);
}
public static void main(String[] args) {
Picture p = new Picture();
p.setVisible(true);
p.setSize(300,300);
}
}
Answerfolder1 | +------folder2 | | | +-------- file1 | | | +-------- file2 | | +------file3 | +------file4What is the output of
java FileTest ?
import java.io.*;
public class FileTest {
public static void main(String[] args) {
File f = new File("folder1");
System.out.println(f.listFiles().length);
System.out.println(f.exists());
System.out.println(f.isFile());
}
}
Answer:java Test?
public class Test {
public static void main(String[] args) {
Holder h = new Holder();
h.held = 100;
h.bump(h);
System.out.println(h.held);
}
}
public class Holder {
public int held = 0;
public void bump(Holder theHolder) {
theHolder.held++;
}
}
Answerjava StringTest?
public class StringTest {
public static void main(String[] args) {
String s = "abcdeabcde";
System.out.println(s.lastIndexOf("cd"));
System.out.println(s.indexOf('X'));
System.out.println(s.replace('c', 'X'));
System.out.println(s.charAt(2));
s.toUpperCase();
System.out.println(s);
}
}
Answer:java SwingTest?
import java.awt.*;
import javax.swing.*;
public class SwingTest extends JFrame {
Container contentPane = getContentPane();
public SwingTest() {
contentPane.setLayout(new GridLayout(2,3));
for(char ch = 'a'; ch <= 'e'; ch++) {
contentPane.add(new JLabel("" + ch));
}
JLabel label = new JLabel("X");
contentPane.add(label,2);
}
public static void main(String[] args) {
SwingTest test = new SwingTest();
test.pack();
test.setVisible(true);
}
}
Answer:
import java.util.*;
public class Program1 {
public static void main(String[] args) {
Vector v = new Vector();
int[] a = {1,2,3};
for(int i = 0; i < 3; i++) {
v.add(a[i]);
}
}
}
(b)
import java.util.*;
public class Program2 {
public static void main(String[] args) {
String[] s = {"A","B", "XYZ", "123"};
Vector v = new Vector();
for(int i = 0; i < s.length; i++) {
v.add(s[i]);
}
Vector v2 = new Vector();
for(int i = 0; i < 4; i++) {
v2.add(v.get(i).toLowerCase());
}
}
}
(c)
import java.awt.*;
import javax.swing.*;
import java.util.*;
public class Program3 {
public static void main(String[] args) {
LinkedList list = new LinkedList();
Object obj0 = Color.red;
Object obj1 = Color.green;
Object obj2 = Color.blue;
list.add(obj0);
list.add(obj1);
list.add(obj2);
JLabel[] labels = new JLabel[10];
for(int i = 0; i < 20; i++) {
labels[i].setBackground(list.get(i));
}
}
}
import java.io.*;
public class Program4 {
public static void main(String[] args) {
FileOutputStream out = new FileOutputStream("hi.txt");
out.write(10);
out.write('a');
}
}
(e)
public class Program5 {
public static void main(String[] args) {
int a = Integer.parseInt(args[0]);
int b = Integer.parseInt(args[1]);
try {
int result = a/b;
} catch (Exception ex) {
return;
}
if(result < 0) {
System.out.println("Negative");
} else {
System.out.println("Positive");
}
}
}
Q4 (15pts)equals(Object obj) returns true
if and only if obj is Employee and they have the same ID.
public class Employee {
public String ID; // 6 digit ID
public String name;
public char gender;
public double salary;
public Employee(String ID, String name, char gender, double salary) {
this.ID = ID;
this.name = name;
this.gender = gender;
this.salary = salary;
}
public boolean equals(Object obj) {
// your codes
}
}
(b)Vector v is a Vector of Employees. Write a method to
find the average salary of all the male (i.e. gender is 'M' or 'm') employee.
Return 0 if there is no male employee.
public double averageSalary(Vector v) {
// your codes
}
(c)LinkedList list is a LinkedList of Doubles. Write a program to
go through all the elements in the list, if the element is negative, set it to 0.
public void removeNegative(Vector v) {
// your codes
}
Q5(15pts)
You are going wo write a program named MoveSmiley.java



smiley.gif![]() |
left.gif![]() |
right.gif![]() |
setIcon(null).setIndex(int newIndex) to reset the index. You don't have to use my hint.
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class MoveSmiley extends JFrame {
int currentIndex = 4;
JButton[] buttons = new JButton[9];
JButton leftButton = __________________________;
JButton rightButton = _________________________;
JPanel centerPanel = new JPanel(new GridLayout(1,0));
JPanel southPanel = new JPanel(new GridLayout(1,0));
// other variables
public MoveSmiley() {
super("Move smiley");
for(int i = 0; i &llt; 9; i++) {
buttons[i] = new JButton(Integer.toString(i));
// your codes
//Your must use anonymous class
buttons[i].addActionListener(
);
} // end for loop
// use seperate classes for left button action and right button action
leftButton.addActionListener(new Listener(this, "left"));
rightButton.addActionListener(new Listener(this, "right"));
southPanel.add(leftButton);
southPanel.add(rightButton);
getContentPane().add(centerPanel);
getContentPane().add(southPanel, BorderLayout.SOUTH);
// your codes (if any)
} // end constructor
// your codes(if any)
public static void main(String[] args) {
MoveSmiley f = new MoveSmiley();
f.setSize(450,100);
f.setVisible(true);
}
}
// seperate class
class Listener implements ActionListener {
MoveSmiley smiley;
String direction; // "left" or "right"
public Listener(MoveSmiley smiley, String direction) {
this.direction = direction;
this.smiley = smiley;
}
// your codes for the action
}
Q6(15pts)
I have my cell phone bill information in a file named bill.txt.
11/22/2003 # 15:40 # 1(310)222-3333 # 25 11/22/2003 # 18:40 # 1(310)444-5555 # 45 11/22/2003 # 22:30 # 1(818)111-2222 # 103 11/22/2003 # 23:10 # 1(323)444-5555 # 93 11/23/2003 # 3:00 # 1(323)222-4444 # 105 11/23/2003 # 9:20 # 1(310)222-3333 # 125 11/23/2003 # 10:43 # 1(310)444-5555 # 145 11/23/2003 # 13:30 # 1(818)111-2222 # 203 11/23/2003 # 13:50 # 1(323)444-5555 # 31 11/23/2003 # 19:23 # 1(510)111-8888 # 69 11/23/2003 # 20:45 # 1(626)888-9009 # 13 |
date of the calling # time you made the call # phone number # minutes of the callEach data is seperated by " # " (space sharp space).
hour:mins. They are in 24hours format.
java PhoneBill, the output of the DOS prompt is
You have to pay $168.25. |
I can't process the file. |
import java.io.*;
public class PhoneBill {
public static void main(String[] args) {
// your codes
}
}