Q1
a) 1pt: B
b) 1pt: 0B
c) 1pt : 01C
d) 2pts: 01XD
e) 2pts :01Xad
f) 2pts : 01-1A
g) 2pts : 01XxyB
h) 1pt : 01Xa
g) 2pts : 01Xab
2 pts for the file part
Q1
a) 3pts
b) 3pts
0
true
false
c)(2pts)
101
d) (3pts)
7
-1
abXdeabXde
c
abcdeabcde
e) 3pts
Q4)
2 pts each
Q5
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 = new JButton(new ImageIcon("left.gif"));
JButton rightButton = new JButton(new ImageIcon("right.gif"));
ImageIcon icon = new ImageIcon("smiley.gif");
JPanel centerPanel = new JPanel(new GridLayout(1,0));
JPanel southPanel = new JPanel(new GridLayout(1,0));
int newIndex;
public MoveSmiley() {
super("Move smiley");
for(int i = 0; i < 9; i++) {
buttons[i] = new JButton(Integer.toString(i));
buttons[i].setActionCommand(Integer.toString(i));
centerPanel.add(buttons[i]);
buttons[i].addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
newIndex = Integer.parseInt(e.getActionCommand());
setIndex(newIndex);
}
});
}
buttons[4].setText("");
buttons[4].setIcon(icon);
southPanel.add(leftButton);
southPanel.add(rightButton);
// use seperate classes for left button action and right button action
leftButton.addActionListener(new Listener(this, "left"));
rightButton.addActionListener(new Listener(this, "right"));
getContentPane().add(centerPanel);
getContentPane().add(southPanel, BorderLayout.SOUTH);
}
public void setIndex(int newIndex) {
buttons[currentIndex].setIcon(null);
buttons[currentIndex].setText(Integer.toString(currentIndex));
buttons[newIndex].setText("");
buttons[newIndex].setIcon(icon);
currentIndex = newIndex;
}
public static void main(String[] args) {
MoveSmiley f = new MoveSmiley();
f.setSize(450,100);
f.setVisible(true);
}
}
class Listener implements ActionListener {
MoveSmiley smiley;
String direction; // "left" or "right"
public Listener(MoveSmiley smiley, String direction) {
this.direction = direction;
this.smiley = smiley;
}
public void actionPerformed(ActionEvent e) {
int index = smiley.currentIndex;
if(direction.equals("left")) {
if(index != 0) {
smiley.setIndex(index-1);
}
} else {
if(index != 8) {
smiley.setIndex(index+1);
}
}
}
}
Q6
import java.io.*;
public class PhoneBill {
public static void main(String[] args) {
String filename = "bill.txt";
BufferedReader in = null;
try {
in = new BufferedReader(new FileReader(filename));
} catch (FileNotFoundException ex) {
System.out.println("I can't process the file.");
System.exit(0);
}
String line = null;
String time=null;
int min=0;
int totalMin=0;
int hr = 0;
try {
while((line = in.readLine())!=null) {
String[] data = line.split(" # ");
time = data[1];
String[] hrmin = time.split(":");
hr = Integer.parseInt(hrmin[0]);
if(hr >= 8 && hr < 19) {
min = Integer.parseInt(data[3]);
//System.out.println(min);
totalMin+=min;
}
}
} catch (IOException ex) {
System.out.println("I can't process the file.");
System.exit(0);
}
System.out.println("You have to pay $ " + totalMin*0.25);
try {
in.close();
} catch (IOException ex) {
}
}
}