
CMSC 132 Quiz 6 Worksheet
The next quiz of the course will be on Wednesday, November 29 during your lab (discussion) session. The
following list provides more information about the quiz:
• The quiz will be a written quiz (no computer).
• Closed book, closed notes quiz.
• Answers must be neat and legible. We recommend that you use pencil and eraser.
The following exercises cover the material to be included in this quiz. Solutions to these exercises will not
be provided, but you are welcome to discuss your solutions with TAs and instructors during office hours.
Regular Expressions
1.
Write a Java regular expression that represents any number of a’s, followed by any number of b’s.
2.
Write a Java regular expression that represents valid phone numbers. A valid phone number has
an area code, a dash, a three-digit number, a dash, and a four-digit number.
3.
Write a Java regular expression that represents a valid e-mail address.
4.
What language is represented by the following Java regular expression?
abcd*[0-9]+
Inner Classes
1.
What is the difference between an inner class a nd a nested class?
2.
An interface named ElectricDevice declares the following method:
public int getWatts();
Using anonymous classes, replace the comment in the following main with an object that
implements the ElectricDevice interface. The getWatts() method should return 100.
public static void main(String[] args) {
ElectricDevice device = /* PUT YOUR CODE HERE */
}
3.
Modify the following class, so the ActionListener used for the postButton is defined using an
inner class instead of an anonymous inner class.
public class GuiExample extends JPanel {
public GuiExample() {
JFrame frame = new JFrame("Example");
frame.setContentPane(this); // adds the panel
JButton postButton = new JButton("Post");
add(postButton);
postButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent evt) {
System.out.println("Posting message");
}
});
frame.pack();
frame.setVisible(true);
}
}