Quiz J4-2
Submit your answers via this Google Form
Question 1
What does the program below produce for a GUI?
// For reference
/* N
* |
* W--|--E
* |
* S
*/
public static void main(final String args[]) {
JFrame frame = new JFrame();
JButton bOne = new JButton("1");
JButton bTwo = new JButton("2");
JButton bThree = new JButton("3");
JButton bFour = new JButton("4");
JButton bFive = new JButton("5");
JButton bSix = new JButton("6");
JPanel primes = new JPanel();
JPanel composites = new JPanel();
primes.setLayout(new BorderLayout());
composites.setLayout(new BorderLayout());
primes.add(bTwo, BorderLayout.EAST);
primes.add(bThree,BorderLayout.WEST);
primes.add(bFive,BorderLayout.NORTH);
composites.add(bFour, BorderLayout.NORTH);
composites.add(bSix, BorderLayout.CENTER);
frame.add(primes, BorderLayout.WEST);
frame.add(composites, BorderLayout.EAST);
frame.add(bOne, BorderLayout.CENTER);
frame.pack();
frame.setTitle("Quiz J4-2");
frame.setSize(400, 400);
frame.setLocation(100, 100);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
Question 2
Consider the two simple math GUIs below:
class SimpleMultiplier extends JFrame{
public SimpleMultiplier(JButton button){
this.add(button, BorderLayout.CENTER);
this.setTitle("Multiply");
this.setSize(100, 100);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static int calculation(int a, int b){
return a * b;
}
}
class SimpleAdder extends JFrame {
public SimpleAdder(JButton button){
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println(calculation(4, 2));
}
});
this.add(button, BorderLayout.CENTER);
this.setTitle("Add");
this.setSize(100, 100);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static int calculation(int a, int b){
return a + b;
}
}
What is the output of the below program if we click the Multiply
window button?
class Main{
public static int calculation(int a, int b){
return a/b;
}
public static void main(final String args[]) {
JButton button1 = new JButton("Calculate!");
button1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println(calculation(4, 2));
}
});
JButton button2 = new JButton("Calculate!");
SimpleMultiplier multiplier = new SimpleMultiplier(button1);
SimpleAdder adder = new SimpleAdder(button2);
multiplier.setVisible(true);
adder.setVisible(true);
}
}
Question 3
Now we run the code from question 2 again, but this time click the Add
window
button. What’s the output?
Question 4
Finally we change SimpleAdder
from a JFrame
to a JPanel
:
// VVVVVV Now a JPanel
class SimpleAdder extends JPanel {
public SimpleAdder(JButton button){
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println(calculation(4, 2));
}
});
this.add(button, BorderLayout.CENTER);
// ******* No more Frame setup code
}
public static int calculation(int a, int b){
return a + b;
}
}
And run the same program from question #2, but instead adding SimpleAdder
to Multiplier
:
public class Main {
public static int calculation(int a, int b){
return a/b;
}
public static void main(final String args[]) {
JButton button1 = new JButton("Calculate!");
button1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println(calculation(4, 2));
}
});
JButton button2 = new JButton("Calculate!");
SimpleMultiplier multiplier = new SimpleMultiplier(button1);
SimpleAdder adder = new SimpleAdder(button2);
// New Line ***********
multiplier.add(adder,BorderLayout.WEST);
// End New Line
multiplier.setVisible(true);
}
}
What’s the output if we click the SimpleAdder
button?