Twenty students were asked to rate, on the scale from 1 to 10, the quality of the food in the student cafeteria, with 1 being “awful” and 10 being “excellent.” Allow the user input to be entered using a JComboBox. Place the 20 responses in an int array and determine the frequency of each rating. Display the frequencies as a histogram in a multiline JTextField. A histogram (also known as a bar chart) is a chart where numeric values are displayed as bars. In such a chart, longer bars represent larger numeric values. One simple way to display numeric data graphically is with a histogram that shows each numeric value as a bar of asterisks (*). Figure 16.42 demonstrates the completed application.


a) Copying   the  template to  your  working  directory. Copy  the  C:\Examples\ Tutorial16\Exercises\CafeteriaSurvey directory to your C:\SimplyJava directory.
b) Opening the template file. Open  the CafeteriaSurvey.java file in your text editor.
c)  Creating  an  array  of  the  possible   ratings.  On  lines  23–25, create  String array choices consisting of 10 consecutive integers  in String format  (such as "1", "2", etc.) to contain  the integers  in the range  1–10, inclusive. Use line 23 for a comment and lines 24–25 to create  and initialize the array.
d) Creating  an  array  to  store  the  responses.  On  lines  27–28, create  an  int array  of length  11 named  responses. This will be used to store  the number of responses in each of the 10 categories (element 0 will not be used). Use line 27 for a comment and line 28 to create  the array.
e)  Customizing the ratingJComboBox. Customize the ratingJComboBox at line 54 so it will display the possible ratings.
f)  Storing the responses. Let’s now look at method submitRatingJButtonActionPer- formed, which executes  when the Submit Rating JButton is clicked. Line 101 (pro- vided  in  the  template)  increments variable   responseCounter, which  stores   the number of responses entered. Line 102 then stores the rating entered by the user into variable  input. We have added  1 to the result  of ratingJComboBox.getSelected- Index because  the indices of a JComboBox start at 0. The value in variable  input now contains  the index for that specific rating in array responses. On line 103, use input to increment the proper element of array responses.
g) Displaying the histogram. You will now display the results  in the form of a histo- gram. Line  106 begins an if statement that  executes  when 20 responses have been entered. Within  this if statement, a for statement is defined  on lines 110–116. This statement loops once for each rating, displaying the rating on line 112 (followed  by a tab character) and a newline  character on line 114. You will add the number of stars that will be displayed  to the right of each rating. On line 114, add the header of a for statement that loops from 1 until the number of votes for the current rating (stored in
responses). On  line 115, add  the  left brace  to begin the  for statement’s body. On line 116, add an asterisk  to the output. Because this for statement will loop the same number of times as there  are votes for the current rating, the proper number of asterisks will be displayed.  On  line 118, add  the  right  brace  to end  the  for statement’s body. Follow the brace with a comment indicating  the end of the for statement.
h) Saving the application. Save your modified  source code file.
i)  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:\SimplyJava\CafeteriaSurvey.
j)  Compiling the application. Compile  your application by typing javac Cafeteria- Survey.java.
k) Running  the completed application. When  your application compiles  correctly,  run it by typing  java CafeteriaSurvey. Enter 20 different ratings  by selecting  values from ratingJComboBox then  pressing the Submit Rating JButton. After  20 ratings have  been  entered, check  that  resultJTextArea contains  the  proper number of asterisks  for each rating.
l)  Closing  the application. Close your running  application by clicking its close button.
m) Closing  the Command Prompt  window.  Close the Command Prompt window by clicking its close button.

```
1 // CafeteriaSurvey.java
2 // Application that asks 20 users to enter a rating of the quality of
3 // food in the student cafeteria and displays a histogram of the
4 // data. User can add ratings between the values of 1 and 10, with
5 // 1 being "awful" and 10 being "excellent."
6 import java.awt.*;
7 import javax.swing.*;
8 import java.awt.event.*;
9
10 public class CafeteriaSurvey extends JFrame
11 {
12 // JLabel and JComboBox for choosing a rating
13 private JLabel ratingJLabel;
14 private JComboBox ratingJComboBox;
15
16 // JButton to submit a rating
17 private JButton submitRatingJButton;
18
19 // JLabel and JTextArea for displaying all ratings
20 private JLabel resultJLabel;
21 private JTextArea resultJTextArea;
22
23 // String array to hold the user's choices
24 private String[] choices = { "1", "2", "3", "4", "5", "6",
25 "7", "8", "9", "10" };
26
27 // integer array to track user responses
28 private int[] responses = new int[ 11 ];
29
30 private int responseCounter = 0; // tracks number of responses
31
32 // no-argument constructor
33 public CafeteriaSurvey()
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 ratingJLabel
48 ratingJLabel = new JLabel();
49 ratingJLabel.setBounds( 20, 20, 140, 20 );
50 ratingJLabel.setText( "Rate cafeteria food:" );
51 contentPane.add( ratingJLabel );
52
53 // set up ratingJComboBox
54 ratingJComboBox = new JComboBox( choices );
55 ratingJComboBox.setBounds( 180, 20, 80, 20 );
56 contentPane.add( ratingJComboBox );
57
58 // set up submitJButton
59 submitRatingJButton = new JButton();
60 submitRatingJButton.setBounds( 80, 55, 130, 20 );
61 submitRatingJButton.setText( "Submit Rating" );
62 contentPane.add( submitRatingJButton );
63 submitRatingJButton.addActionListener(
64
65 new ActionListener() // anonymous inner class
66 {
67 // event handler called when
68 // submitRatingJButton is clicked
69 public void actionPerformed( ActionEvent event )
70 {
71 submitRatingJButtonActionPerformed( event );
72 }
73
74 } // end anonymous inner class
75
76 ); // end call to addActionListener
77
78 // set up resultJLabel
79 resultJLabel = new JLabel();
80 resultJLabel.setBounds( 20, 100, 100, 20 );
81 resultJLabel.setText( "Survey results:" );
82 contentPane.add( resultJLabel );
83
84 // set up resultJTextArea
85 resultJTextArea = new JTextArea();
86 resultJTextArea.setBounds( 20, 130, 240, 190 );
87 resultJTextArea.setEditable( false );
88 contentPane.add( resultJTextArea );
89
90 // set properties of application's window
91 setTitle( "Cafeteria Survey" ); // set title bar string
92 setSize( 290, 370 ); // set window size
93 setVisible( true ); // display window
94
95 } // end method createUserInterface
96
97 // submit response and display results
98 private void submitRatingJButtonActionPerformed(
99 ActionEvent event )
100 {
101 responseCounter++;
102 int input = ratingJComboBox.getSelectedIndex() + 1;
103 responses[ input ]++;
104
105 // if 20 ratings have been entered, display histogram
106 if ( responseCounter == 20 )
107 {
108 resultJTextArea.setText( "Rating\tFrequency:\n" );
109
110 for ( int i = 1; i <= 10; i++ )
111 {
112 resultJTextArea.append( i + "\t" );
113
114 for ( int j = 1; j <= responses[ i ]; j++ )
115 {
116 resultJTextArea.append( "*" );
117
118 } // end inner for loop


119
120
121
122 } // end outer for loop
123
124 submitRatingJButton.setEnabled( false );
125
126 } // end if statement
127
128 } // end submitJButtonActionPerformed
129
130 // main method
131 public static void main( String[] args )
132 {
133 CafeteriaSurvey application = new CafeteriaSurvey();
134 application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
135
136 } // end method main
137
138 } // end class CafeteriaSurvey
```

Computer Science & Information Technology

You might also like to view...

How many sizing handle are found on embedded charts?

A) 4 B) 10 C) 6 D) 8

Computer Science & Information Technology

How does Animate measure its X and Y coordinates on the Stage?

What will be an ideal response?

Computer Science & Information Technology