A school cafeteria is giving an electronic survey to its stu- dents to improve their lunch menu. Create an application that will use a two-dimensional array to hold counters for the survey (Fig. 17.31). You will also provide JRadioButtons for the students to select whether they like or dislike a particular food.
a) Copying the template to your working directory. Copy the C:Examples Tutorial17ExercisesFoodSurvey directory to your C:SimplyJava directory.
b) Opening the template file. Open the FoodSurvey.java file in your text editor.
c) Declaring a two-dimensional int array. On lines 29-30, insert code to declare a two- dimensional int array named display, with 4 rows and 2 columns.
d) Declaring local variables. On line 105, in method addJButtonActionPerformed, set the text of displayJTextArea to "Food Like Dislike" for the header. Cre- ate a local int variable index. This variable should contain the index of the selected item in foodJComboBox.
e) Using a for loop to display the data. Insert a for statement that will loop through each row in the foodChoices array (0–3). In the body of the loop, append the appro- priate food to displayJTextArea. The counter variable of the for statement will be used as the index of the foodChoices array. Add the "
" escape sequence to displayJText
```
1 // FoodSurvey.java
2 // This application takes a survey of opinions on various foods.
3 import java.awt.*;
4 import java.awt.event.*;
5 import javax.swing.*;
6
7 public class FoodSurvey extends JFrame
8 {
9 // JComboBox to give choice of foods
10 private JComboBox foodJComboBox;
11
12 // JButton to add choice to JTextArea
13 private JButton addJButton;
14
15 // ButtonGroup to control like and dislike JRadioButtons
16 private ButtonGroup choiceButtonGroup;
17
18 // JRadioButtons to choose like or dislike food
19 private JRadioButton likeJRadioButton;
20 private JRadioButton dislikeJRadioButton;
21
22 // JTextArea to display results
23 private JTextArea displayJTextArea;
24
25 // String array for food choices
26 private String foodChoices[] = { "Pizza", "Hot Dog",
27 "Fish Sticks", "Mystery Meat" };
28
29 // two-dimensional array to store survey results
30 private int display[][] = new int[ 4 ][ 2 ];
31
32 // no-argument constructor
33 public FoodSurvey()
34 {
35 createUserInterface();
36 }
37
38 // create and position GUI components; register event handlers
39 private void createUserInterface()
40 {
41 // get content pane for attaching GUI components
42 Container contentPane = getContentPane();
43
44 // enable explicit positioning of GUI components
45 contentPane.setLayout( null );
46
47 // set up foodJComboBox
48 foodJComboBox = new JComboBox( foodChoices );
49 foodJComboBox.setBounds( 16, 16, 110, 23 );
50 contentPane.add( foodJComboBox );
51
52 // set up choiceButtonGroup
53 choiceButtonGroup = new ButtonGroup();
54
55 // set up likeJRadioButton
56 likeJRadioButton = new JRadioButton();
57 likeJRadioButton.setBounds( 135, 16, 60, 23 );
58 likeJRadioButton.setText( "Like" );
59 likeJRadioButton.setSelected( true );
60 choiceButtonGroup.add( likeJRadioButton );
61 contentPane.add( likeJRadioButton );
62
63 // set up dislikeJRadioButton
64 dislikeJRadioButton = new JRadioButton();
65 dislikeJRadioButton.setBounds( 195, 16, 70, 23 );
66 dislikeJRadioButton.setText( "Dislike" );
67 choiceButtonGroup.add( dislikeJRadioButton );
68 contentPane.add( dislikeJRadioButton );
69
70 // set up addJButton
71 addJButton = new JButton();
72 addJButton.setBounds( 265, 16, 67, 23 );
73 addJButton.setText( "Add" );
74 contentPane.add ( addJButton );
75 addJButton.addActionListener(
76
77 new ActionListener() // anonymous inner class
78 {
79 // event handler called when addJButton is clicked
80 public void actionPerformed( ActionEvent event )
81 {
82 addJButtonActionPerformed( event );
83 }
84
85 } // end anonymous inner class
86
87 ); // end call to addActionListener
88
89 // set up displayJTextArea
90 displayJTextArea = new JTextArea();
91 displayJTextArea.setBounds( 16, 55, 315, 93 );
92 displayJTextArea.setEditable( false );
93 contentPane.add( displayJTextArea );
94
95 // set properties of application’s window
96 setTitle( "Food Survey" ); // set title bar string
97 setSize( 355, 190 ); // set window size
98 setVisible( true ); // display window
99
100 } // end method createUserInterface
101
102 // calculate and display the student and class average
103 private void addJButtonActionPerformed( ActionEvent event )
104 {
105 // output header
106 displayJTextArea.setText( "Food\tLike\tDislike" );
107
108 // index of selected food
109 int index = foodJComboBox.getSelectedIndex();
110
111 // for loop to display 2-dimensional array
112 for ( int j = 0; j < 4; j++ )
113 {
114 // add food to output
115 displayJTextArea.append( "\n" + foodChoices[ j ] + "\t" );
116
117 // increment appropriate counter
118 if ( likeJRadioButton.isSelected() && index == j )
119 {
120 display[ j ][ 0 ]++;
121 }
122 else if ( dislikeJRadioButton.isSelected() && index == j )
123 {
124 display[ j ][ 1 ]++;
125 }
126
127 displayJTextArea.append( display[ j ][ k ] + "\t" );
128
129
130
131 }
132
133 // end for
134
135 } // end method addJButtonActionPerformed
136
137 // main
138 public
139 {
140 FoodSurvey application = new FoodSurvey();
141 application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
142
143 } // end method main
144
145 } // end
```
You might also like to view...
Why is it appropriate for channel controllers to steal cycles from processors when accessing memory?
What will be an ideal response?
What is a variable?
A. A numeric value used in a program. B. A data type and a name used in a program. C. An integer or floating point value. D. A memory location reserved by the program.
Screen shots inserted into an Excel workbook can be selected from ________
A) the Picture library B) the Documents library C) saved applications D) open applications
Which of the following is an environmental server maintenance task that should be performed every three months?
A. Replace the rubber mats in the server room B. Clean the servers to limit dust buildup C. Wash the outside of the servers D. Replace the server fans