PIC20A Midterm II 2004 Spring

PIC20A Midterm II 2004 Spring

Instructions

  1. The exam consists of 6 pages(including this page). You are responsible for checking the number of pages
  2. Please read and answer all the questions carefully. The point value for each question is indicated. The total number of points available is 30. If you have trouble with a question, leave it and try another.
  3. Unless otherwise stated, you may assume when code is given that the code compiles and is correct. When you are asked to write code, you are expected to write code in good style and with proper indentation.
  4. Good luck!




Last Name:_________________ First Name:________________
Student ID:_________________






Question
Q1(10pts)
Q2(10pts)
Q3(10pts)
Total
Scores




















Question 1(10pts)
(a) (3pts)
I have an image file named redspot.gif:

Sketch (draw) the output of java Swing1?
import java.awt.*;
import javax.swing.*;

public class Swing1 extends JFrame {
    public Swing1(String s) {
        getContentPane().setLayout(new BorderLayout());
        ImageIcon icon = new ImageIcon("redspot.gif");
        JLabel label = new JLabel(s, icon, JLabel.CENTER);
        label.setHorizontalTextPosition(JLabel.LEFT);
        label.setVerticalTextPosition(JLabel.BOTTOM);
        getContentPane().add(label);
    }
    
    public static void main(String[] args) {
        JFrame f = new Swing1("X");
        f.pack();
        f.setVisible(true);
    }
}
Answer:















(b) (7pts)
Sketch (draw) the output of java Swing2:
import java.awt.*;
import javax.swing.*;

public class Swing2 extends JFrame {
    public Swing2() {
        JPanel panel1 = new JPanel(new GridLayout(1,0));
        JPanel panel2 = new JPanel(new GridLayout(0,1));
        
        for(int i = 0; i < 3; i++) {
            panel1.add(new JButton(Integer.toString(i)));
        }
        
        for(int i = 0; i < 4; i++) {
            panel2.add(new JButton(Integer.toString(i)));
        }
        
        getContentPane().setLayout(new GridLayout(2, 3));
        
        for(int i = 0; i  < 5; i++) {
            getContentPane().add(new JButton(Integer.toString(i)));
        }
        getContentPane().remove(3);
        getContentPane().add(panel1, 2);
        getContentPane().add(panel2, 4);
    }
    
    public static void main(String[] args) {
        JFrame f = new Swing2();
        f.setSize(400,300);
        f.setVisible(true);
    }
}
Answer:























Question 2 (10pts)
Consider the following program. Run the program by java NS:

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

public class NS extends JFrame {
    JButton northButton = new JButton("North");
    JButton southButton = new JButton("South");
    JButton eastButton = new JButton("East");
    JButton centerButton = new JButton("C");
    
    public NS() {
        getContentPane().setLayout(new BorderLayout()); 
        getContentPane().add(northButton, BorderLayout.NORTH);
        getContentPane().add(southButton, BorderLayout.SOUTH);
        getContentPane().add(eastButton, BorderLayout.EAST);
        getContentPane().add(centerButton);
        
        northButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                String text = centerButton.getText() + "N";
                centerButton.setText(text);
            }
        });
        
        southButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                String text = "S" + centerButton.getText();
                centerButton.setText(text);
            }
        });
        
        eastButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                centerButton.setText("E");
            }
        });
    }
    
    public static void main(String[] args) {
        JFrame f = new NS();
        f.setSize(200, 200);
        f.setVisible(true);
    }
}
Step 1: Immediately after you start the program, what is the text on centerButton after you click southButton twice?

Answer:_________________________________
Step 2: after step 1, what is the text on centerButton after you click northButton three times?

Answer:_________________________________
Step 3: after step 2, what is the text on centerButton after you click eastButton once?

Answer:_________________________________
Step 4: after step 3, what is the text on centerButton after you click centerButton three times?

Answer:_________________________________
Step 5: after step 4, what is the text on centerButton after you click southButton once and northButton twice?

Answer:_________________________________

Question 3
The program consists of 15 JButtons, with numbers from 0 to 14 and one JLabel at the lower right corner.


When you click on an odd number (example, 5), the text on the JLabel is changed to odd.


When you click on an even number (example, 10), the text on the JLabel is changed to even.


Fill your codes below:
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

public class OddEven extends JFrame implements ActionListener {
    JButton[] buttons = new JButton[15];
    JLabel label = new JLabel();
    Container contentPane = getContentPane();
    
    public OddEven() {
        contentPane.setLayout(new GridLayout(4,4));
        for(int i = 0; i < 15; i++) {
            buttons[i] = new JButton(Integer.toString(i));
            // your codes (if any)
            









} // your others codes(if any)














} public void actionPerformed(ActionEvent e) { // your codes














} public static void main(String[] args) { JFrame f = new OddEven(); f.pack(); f.setVisible(true); } }