Sibling Survey Application) The Sibling Survey application displays the siblings selected by the user in a dialog. If the user checks either the Brother(s) or Sister(s) JCheck- Box, and the No Siblings JCheckBox, the user is asked to verify the selection. Otherwise, the user’s selection is displayed in a JOptionPane message dialog. While testing this application, you noticed that it does not execute properly. Use the debugger jdb to find and correct the logic error(s) in the code. Figure 7.32 shows the correct output for the application.
```
a) Copying the template to your working directory. Copy the C:Examples Tutorial07ExercisesDebuggerSiblingSurvey directory to your C:Simply- Java directory.
b) Opening the Command Prompt window and changing directories. Open the Command Prompt window by selecting Start > Programs > Accessories > Command Prompt. Change to your working directory by typing cd C:SimplyJavaDebug- gerSiblingSurvey.
c) Running the application. Run the Sibling Survey application by typing java SiblingSurvey. Test the different options. Notice that when you select either the Brother(s) or the Sister(s) JCheckBox, the desired result is not performed. Also, notice that when you select the No Siblings JCheckBox as well as one of the other JCheckBoxes, the desired result is not performed.
d) Starting the debugger. Close the application (but leave the Command Prompt open)
and start the debugger by typing jdb.
e) Opening the template file. Open the SiblingSurvey.java file in your text
```
1 // SiblingSurvey.java
2 // This application gives the user three options regarding their
3 // siblings and displays the results in a pop up box.
4 import java.awt.*;
5 import java.awt.event.*;
6 import javax.swing.*;
7
8 public class SiblingSurvey extends JFrame
9 {
10 // JLabel that displays prompt
11 private JLabel selectSiblingsJLabel;
12
13 // JCheckBoxes for user options
14 private JCheckBox brotherJCheckBox;
15 private JCheckBox sisterJCheckBox;
16 private JCheckBox noSiblingsJCheckBox;
17
18 // JButton for user to submit input
19 private JButton submitSurveyJButton;
20
21 // no-argument constructor
22 public SiblingSurvey()
23 {
24 createUserInterface();
25 }
26
27 // create and position GUI components; register event handlers
28 private void createUserInterface()
29 {
30 // get content pane for attaching GUI components
31 Container contentPane = getContentPane();
32
33 // enable explicit positioning of GUI components
34 contentPane.setLayout( null );
35
36 // set up selectSiblingsJLabel
37 selectSiblingsJLabel = new JLabel();
38 selectSiblingsJLabel.setText(
39 "Please select the siblings you have:" );
40 selectSiblingsJLabel.setBounds( 16, 16, 204, 23 );
41 contentPane.add( selectSiblingsJLabel );
42
43 // set up brotherJCheckBox
44 brotherJCheckBox = new JCheckBox();
45 brotherJCheckBox.setText( "Brother(s)" );
46 brotherJCheckBox.setBounds( 16, 56, 88, 21 );
47 contentPane.add( brotherJCheckBox );
48
49 // set up sisterJCheckBox
50 sisterJCheckBox = new JCheckBox();
51 sisterJCheckBox.setText( "Sister(s)" );
52 sisterJCheckBox.setBounds( 16, 96, 88, 21 );
53 contentPane.add( sisterJCheckBox );
54
55 // set up noSiblingsJCheckBox
56 noSiblingsJCheckBox = new JCheckBox();
57 noSiblingsJCheckBox.setText( "No Siblings" );
58 noSiblingsJCheckBox.setBounds( 16, 136, 88, 21 );
59 contentPane.add( noSiblingsJCheckBox );
60
61 // set up submitSurveyJButton
62 submitSurveyJButton = new JButton();
63 submitSurveyJButton.setText( "Submit Survey" );
64 submitSurveyJButton.setBounds( 56, 176, 116, 23 );
65 contentPane.add( submitSurveyJButton );
66 submitSurveyJButton.addActionListener(
67
68 new ActionListener() // anonymous inner class
69 {
70 // event handler called when submitSurveyJButton
71 // is clicked
72 public void actionPerformed( ActionEvent event )
73 {
74 submitSurveyJButtonActionPerformed( event );
75 }
76
77 } // end anonymous inner class
78
79 ); // end call to addActionListener
80
81 // set properties of application’s window
82 setTitle( "Sibling Survey" ); // set title bar string
83 setSize( 236, 233 ); // set window size
84 setVisible( true ); // display window
52 sisterJCheckBox.setBounds( 16, 96, 88, 21 );
53 contentPane.add( sisterJCheckBox );
54
55 // set up noSiblingsJCheckBox
56 noSiblingsJCheckBox = new JCheckBox();
57 noSiblingsJCheckBox.setText( "No Siblings" );
58 noSiblingsJCheckBox.setBounds( 16, 136, 88, 21 );
59 contentPane.add( noSiblingsJCheckBox );
60
61 // set up submitSurveyJButton
62 submitSurveyJButton = new JButton();
63 submitSurveyJButton.setText( "Submit Survey" );
64 submitSurveyJButton.setBounds( 56, 176, 116, 23 );
65 contentPane.add( submitSurveyJButton );
66 submitSurveyJButton.addActionListener(
67
68 new ActionListener() // anonymous inner class
69 {
70 // event handler called when submitSurveyJButton
71 // is clicked
72 public void actionPerformed( ActionEvent event )
73 {
74 submitSurveyJButtonActionPerformed( event );
75 }
76
77 } // end anonymous inner class
78
79 ); // end call to addActionListener
80
81 // set properties of application’s window
82 setTitle( "Sibling Survey" ); // set title bar string
83 setSize( 236, 233 ); // set window size
84 setVisible( true ); // display window
85
86 } // end method createUserInterface
87
88 // display message based on user selection
89 public void submitSurveyJButtonActionPerformed(
90 ActionEvent event )
91 {
92 // check if user selects brothers or sisters and no siblings
93 if ( noSiblingsJCheckBox.isSelected() &&
94 ( brotherJCheckBox.isSelected() ||
95 sisterJCheckBox.isSelected() ) )
96 {
97 JOptionPane.showMessageDialog( null,
98 "Selected combination is not possible.",
99 "Invalid Input", JOptionPane.WARNING_MESSAGE );
100 }
101 else if ( !noSiblingsJCheckBox.isSelected() &&
102 !brotherJCheckBox.isSelected() &&
103 !sisterJCheckBox.isSelected() )
104 {
105 // if user has not selected a JCheckBox
106 JOptionPane.showMessageDialog( null,
107 "Please select at least one option.", "Invalid Input",
108 JOptionPane.WARNING_MESSAGE );
109 }
110 else if ( brotherJCheckBox.isSelected() &&
111 sisterJCheckBox.isSelected() )
112 {
113 // if user has brothers and sisters
114 JOptionPane.showMessageDialog( null,
115 "You have at least one brother and one sister.",
116 "Siblings", JOptionPane.INFORMATION_MESSAGE );
117 }
118 else if ( brotherJCheckBox.isSelected() )
119 {
120 // if user has brothers
121 JOptionPane.showMessageDialog( null,
122 "You have at least one brother.", "Siblings",
123 JOptionPane.INFORMATION_MESSAGE );
124 }
125 else if ( sisterJCheckBox.isSelected() )
126 {
127 // if user has sisters
128 JOptionPane.showMessageDialog( null,
129 "You have at least one sister.", "Siblings",
130 JOptionPane.INFORMATION_MESSAGE );
131 }
132 else // user has no siblings
133 {
134 JOptionPane.showMessageDialog( null,
135 "You have no siblings.", "Siblings",
136 JOptionPane.INFORMATION_MESSAGE );
137 }
138
139 } // end method submitSurveyJButtonActionPerformed
140
141 // main method
142 public static void main( String [] args )
143 {
144 SiblingSurvey application = new SiblingSurvey();
145 application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
146 }
147
148 } // end class SiblingSurvey
```
You might also like to view...
Reports are Access objects that are created using the data contained or produced in ________ and ________
Fill in the blank(s) with correct word
Which of the following methods of system development stresses intense team-based effort and reflects a set of community-based values??
A. ?Object-oriented analysis B. ?Agile method C. ?Structured analysis D. ?Rapid application development