Worksheet: J4 | CS 2113 Software Engineering - Fall 2021

Worksheet: J4

Worksheets are self-guided activities that reinforce lectures. They are not graded for accuracy, only for completion. Worksheets are due by Sunday night before the next lecture.

Questions

  1. How does object-oriented programming pair so closely with GUIs?

    Reveal Solution

  2. What is the relationship between WindowListener and WindowAdapter?

    Reveal Solution

  3. Go to the Java docs for WindowAdapter

    What other kinds of listening does it do other than just those in WindowListener?

    Reveal Solution

  4. Assume we define a class as below:

    class ParentJFrame extends JFrame {
        private JFrame[] frames = new JFrame[5];
    
        private JFrame[] frames = new JFrame[5];
    
        public ParentJFrame() {
            for (int i = 0; i < 5; i++) {
                JFrame f = new JFrame();
                f.setTitle("Worksheet Child" + i);
                f.setSize(100, 100);
                // Make windows appear on a diagonal line
                f.setLocation(100 * i, 100 * i);
                f.addWindowListener(new ChildAdapter());
                f.setVisible(true);
                frames[i] = f;
            }
            this.addWindowListener(new ParentAdapter());
        }
    
        private static class ChildAdapter extends WindowAdapter {
            // Called when window closes
            @Override
            public void windowClosing(WindowEvent e) {
                System.out.println("Closed!");
            }
    
        }
        
        private static class ParentAdapter extends WindowAdapter {
    
            @Override
            public void windowClosing(WindowEvent e) {
                System.out.println("Parent Closed!");
            }
    
        }
    
    }
    

    What would be the output of the code below if we close the ParentJFrame immediately after it appears?

    public static void main(final String args[]) {
            ParentJFrame frame = new ParentJFrame();
            frame.setTitle("Quiz Parent");
            frame.setSize(100, 100);
            frame.setLocation(100, 100);
            frame.setVisible(true);
        }
    

    Reveal Solution

  5. If we close all children windows instead of the parent, how does that change the output?

    Reveal Solution

  6. Assume we add this line to the end of main to the above program and then close the parents. How does this change the behaviour of the program?

     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // makes it so that closing window exits program
    

    Reveal Solution

  7. Now consider the case in which we close the children windows, and then the parents (from question 1). What would the output be in this case?

    Reveal Solution

  8. “Closed!” 5 times, then parent closed, and the program will stop.

    Reveal Solution

  9. What does the program below produce for a GUI? (You can sketch and upload an image or describe it).

     // 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);
        }
    

    Reveal Solution

  10. What is the output of the below program if we click the Multiply window button?

    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;
        }
    }
    
    
    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);
        }
    }
    

    Reveal Solution

  11. From the previous question, what would happen if we instead had clicked the Add window button?

    Reveal Solution

  12. If we change SimpleAdder to a JPanel instead of a JFrame and we add it Multiplier, how will that affect the behavior of the program if we click the SimpleAdder button? The changes can be seen below.

    //                        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;
        }
    }
    
    
    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);
        }
    }
    

    Reveal Solution

  13. Consider the following Java swing gui

    public class RedPillBluePill extends JFrame{
        JLabel label;
    
        public RedPillBluePill(){
            this.setSize(300,300);
            this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
            JPanel panel = new JPanel(new BorderLayout());        
            JButton red = new JButton("red");
            JButton blue = new JButton("blue");
            panel.add(red,BorderLayout.EAST);
            panel.add(blue,BorderLayout.WEST);
            label = new JLabel("click a button");
            this.add(label, BorderLayout.NORTH);
            this.add(panel, BorderLayout.SOUTH);
    
            red.addActionListener(new ActionListener(){
                @Override
                public void actionPerformed(ActionEvent e) {
                    // TODO Auto-generated method stub
                    label.setText("RED");        
                }
            });
    
            blue.addActionListener(new ActionListener(){
    
                @Override
                public void actionPerformed(ActionEvent e) {
                    // TODO Auto-generated method stub
                    label.setText("BLUE");
                }
    
            });
        }
    }
    

    Convert the ActionListeners to Lambda Functions.

    Reveal Solution

  14. Explain why for ActionListener you can use a Lambda function but for WindowListener you cannot?

    Reveal Solution

  15. Write a program that allows you to enter a 6-digit PIN, like you would on your smartphone to unlock it. It should have the following layout:

    
     [ DISPLAY PIN AS TYPED ]
    
       [ 1 ]  [ 2 ] [ 3 ] 
       
       [ 4 ]  [ 5 ] [ 6 ]    
       
       [ 7 ]  [ 8 ] [ 9 ]       
    
       [ < ]  [ 0 ] 
    
    

    Where [ < ] is a “backspace” button. The display should show the PIN as it is typed, and when the user enters the PIN 202113, the display changes to “YOU MAY ENTER!”

    Reveal Solution

  16. For the above program you wrote above, add a new feature. This could be to allow variable length PINs, match different PINs, allow users to select a PIN and then confirm it later, etc.

    Describe your extension.

    Reveal Solution