Java Notes

Programming: Complete the Rainfall GUI

Name ________________________________________

Fill in the blanks

  1 
  2 
  3 
  4 
  5 
  6 
  7 
  8 
  9 
 10 
 11 
 12 
 13 
 14 
 15 
 16 
 17 
 18 
 19 
 20 
 21 
 22 
 23 
 24 
 25 
 26 
 27 
 28 
 29 
 30 
 31 
 32 
 33 
 34 
 35 
 36 
 37 
 38 
 39 
 40 
 41 
 42 
 43 
 44 
 45 
 46 
 47 
 48 
 49 
 50 
 51 
 52 
 53 
 54 
 55 
 56 
 57 
 58 
 59 
 60 
 61 
 62 
 63 
 64 
 65 
 66 
 67 
 68 
 69 
 70 
 71 
 72 
 73 
 74 
 75 
 76 
 77 
 78 
 79 
 80 
 81 
 82 
 83 
 84 
 85 
 86 
 87 
 88 
 89 
 90 
 91 
 92 
 93 
 94 
 95 
 96 
 97 
 98 
 99 
100 
101 
102 
103 
104 
105 
106 
107 
108 
109 
110 
111 
112 
113 
114 
115 
116 
117 
118 
119 
120 
121 
122 
123 
124 
125 
126 
127 
128 
129 
130 
131 
132 
133 
134 
135 
136 
137 
138 
139 
140 
141 
142 
143 
144 
145 
146 
147 
148 
149 
150 
151 
152 
153 
154 
155 
156 
157 
158 
159 
160 
161 
162 
163 
164 
165 
166 
167 
168 
169 
170 
171 
172 
173 
174 
175 
176 
177 
178 
179 
180 
181 
182 
183 
// Rainfall GUI

import javax.________________.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;

class RainAverageGUI extends ________________ {
    //============================================ GUI component instance vars
    private JTextArea  ________________    = new JTextArea(10, 40);
    private JTextField _numberOfValuesTF = new JTextField(10);
    private JTextField _totalRainTF      = new JTextField(10);
    private JTextField _averageRainTF    = new JTextField(10);
    private JTextField _numberAboveAverageTF = new JTextField(10);
    
    private JMenuItem _openItem  = new JMenuItem("Open...");
    private JMenuItem _saveItem  = new JMenuItem("Save...");
    private JMenuItem _quitItem  = new JMenuItem("Quit");
    private JMenuItem _aboutItem = new JMenuItem("About");
    
    ________________ JFileChooser _fileChooser = new JFileChooser();
    
    //===================================================== model instance var
    RainData _rainLogic;
    
    //===================================================== constructor
    public ________________() {
        //... Initialize the "model".
        _rainLogic = new ________________(500);
        
        JButton computeBtn = new JButton("Compute");
        computeBtn.________________(new ComputeBtnListener());
        
        JPanel resultsPanel = ________________ JPanel();
        resultsPanel.setLayout(new GridLayout(2, 4, 5, 5));
        
        resultsPanel.add(new JLabel("Number"));
        resultsPanel.add(_numberOfValuesTF);
        resultsPanel.add(new JLabel("Total"));
        resultsPanel.add(_totalRainTF);
        resultsPanel.add(new JLabel("Average"));
        resultsPanel.add(_averageRainTF);
        resultsPanel.add(new JLabel("Above Average"));
        resultsPanel.add(_numberAboveAverageTF);
        
        JPanel btnPanel = new JPanel();
        btnPanel.setLayout(new FlowLayout());
        btnPanel.add(computeBtn);
        
        JPanel content = new JPanel();
        content.setLayout(new BorderLayout(5, 5));
        content.add(_inputValuesTA, BorderLayout.________________);
        content.add(btnPanel      , BorderLayout.CENTER);
        content.add(resultsPanel  , BorderLayout.SOUTH);
        
        this.setJMenuBar(createMenubar());
        this.________________(content);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setTitle("Rain Average");
        this.________________();
    }
    
    //==================================================== createMenuBar
    private JMenuBar createMenubar(){
        JMenuBar ________________ = new JMenuBar();
        JMenu fileMenu = new ________________("File");
        menubar.add(fileMenu);
        JMenu helpMenu = new JMenu("Help");
        menubar.add(helpMenu);
        fileMenu.add(_openItem);
        fileMenu.add(_saveItem);
        fileMenu.add(_quitItem);
        helpMenu.add(_aboutItem);
        
        _openItem.addActionListener(new OpenAction());
        _saveItem.addActionListener(new SaveAction());
        _quitItem.addActionListener(new QuitAction());
        _aboutItem.addActionListener(new AboutAction());
        
        return ________________;
    }
    
    /////////////////////////////////////////////////////// ComputeBtnListener
    class ComputeBtnListener ________________ ActionListener{
        public void actionPerformed(ActionEvent evt){
            
            String oneDataString = "";
            
            try {
                _rainLogic.________________();
                
                String inputData = _inputValuesTA.getText().trim();
                String [] inputStrings = inputData.________________("\\s+");
                
                for (int i=0; i<inputStrings.________________; i++){
                    oneDataString = inputStrings[________________];
                    double data = Double.parseDouble(oneDataString);
                    _rainLogic.add(________________);
                }
                
                _totalRainTF.________________(""+_rainLogic.getTotal());
                _numberOfValuesTF.setText(""+_rainLogic.getNumber());
                _averageRainTF.setText(""+_rainLogic.getAverage());
                _numberAboveAverageTF.setText(""+_rainLogic.getAboveAverage());
                
            } catch (NumberFormatException unused){
                
                JOptionPane.showMessageDialog(null, "Bad input value : "
                        + oneDataString);
                _totalRainTF.setText("");
                _numberOfValuesTF.setText("");
                _averageRainTF.setText("");
                _numberAboveAverageTF.setText("");
            }
        }
    }
    
    //////////////////////////////////////////////////////////// OpenAction
    private class OpenAction implements ActionListener {
        public void actionPerformed(ActionEvent ae){
            int retval = ________________.showOpenDialog(RainAverageGUI.this);
            if (retval == JFileChooser.APPROVE_OPTION){
                File textFile = _fileChooser.getSelectedFile();
                ________________.setText(readFile(textFile));
            }
        }
    }
    
    //////////////////////////////////////////////////////////// SaveAction
    private class SaveAction implements ActionListener {
        public void actionPerformed(ActionEvent ae){
            int retval = _fileChooser.showSaveDialog(RainAverageGUI.this);
            if (retval == JFileChooser.APPROVE_OPTION){
                File textFile = _fileChooser.getSelectedFile();
                writeFile(textFile, _inputValuesTA.________________());
            }
        }
    }
    
    //////////////////////////////////////////////////////////// QuitAction
    private class QuitAction implements ActionListener {
        public void actionPerformed(ActionEvent e){
            ________________.exit(0);
        }
    }
    
    //////////////////////////////////////////////////////////// AboutAction
    private class AboutAction implements ________________ {
        public void actionPerformed(ActionEvent ae){
            ________________.showMessageDialog(RainAverageGUI.this,
                    "Maus, Michael");
        }
    }
    
    //========================================================== readFile
    private String readFile(File f){
        StringBuilder text = new StringBuilder(4000);
        try {
            Scanner wordScanner = new ________________(f);
            while(wordScanner.hasNextLine()){
                text.append(wordScanner.nextLine());
                text.append('\n');
                
            }
        } ________________ (FileNotFoundException fnfex){
            JOptionPane.showMessageDialog(null, "Can't Read file" + fnfex);
        }
        return text.toString();
    }
    
    //========================================================= writeFile
    private void writeFile(File ________________, String txt){
        ________________ {
            ________________ writer = new BufferedWriter(new FileWriter(f));
            writer.write(txt);
            writer.________________();
        } catch (IOException e){
            System.err.println(e);
            System.exit(1);
        }
    }
}