H:\CSC120\Java\lec34_s21\src\MUFrame.java
  1 /* CSC 120 Lecture # 34
  2  *
  3  * Finding sum, avg, min and max of a numeric property in 
  4  * and array of objects
  5  *
  6  */
  7 
  8 import java.awt.*;
  9 import javax.swing.*;
 10 
 11 public class MUFrame extends javax.swing.JFrame {
 12 
 13     // declare private data here
 14     
 15     private Integer[] arr;
 16     
 17     
 18     // constructor method
 19     public MUFrame() {
 20         initComponents();
 21         
 22         arr = new Integer[8];
 23         for (Integer x = 0; x < arr.length; x++) {
 24             arr[x] = (x-3)*(x-3) + 1;
 25             outputArea.append("arr[" + x + "] = " + arr[x] + "\n");
 26         } // end for
 27         
 28         // find the sum
 29         Integer sum;
 30         Integer count;
 31         sum = 0;
 32         count = 0;
 33         for (Integer x = 0; x < arr.length; x++) {
 34             sum = sum + arr[x];
 35             count++;
 36             outputArea.append("    sum so far: " + sum 
 37                     + "; count so far: " + count + "\n");
 38             
 39         }
 40         outputArea.append("\nafter the loop, sum = " + sum + "\n\n");
 41                     
 42         // find the average
 43         //    first of all, find the sum
 44         //    then divide by the number of elements that were added
 45         
 46         Double average = 1.0 * sum / count;
 47         outputArea.append("average value in array = " + average + "\n\n");
 48         
 49         // find the minimum value in the array
 50         
 51         Integer max;
 52         max = Integer.MIN_VALUE;
 53         for (Integer x = 0; x < arr.length; x++) {
 54             if (arr[x] > max) {
 55                 max = arr[x];
 56             }
 57             outputArea.append("    maximum so far: " + max + "\n");
 58         }
 59         outputArea.append("\nafter the loop, maximum = " + max + "\n\n");
 60         
 61         // find the minimum value in the array
 62         
 63         Integer min;
 64         min = Integer.MAX_VALUE;
 65         for (Integer x = 0; x < arr.length; x++) {
 66             if (arr[x] < min) {
 67                 min = arr[x];
 68             }
 69             outputArea.append("    minimum so far: " + min + "\n");
 70         }
 71         outputArea.append("\nafter the loop, minimum = " + min + "\n\n");
 72         
 73         
 74         processSoccerStats();
 75         
 76     } // end of constructor
 77     
 78     // declare other methods here
 79     
 80 
 81     
 82     public void processSoccerStats(){
 83         
 84         SoccerTeam[] team;
 85         
 86         team = new SoccerTeam[7];
 87         team[0] = new SoccerTeam(2019, 9, 10);
 88         team[1] = new SoccerTeam(2018, 12, 7);
 89         team[2] = new SoccerTeam(2017, 12, 6);
 90         team[3] = new SoccerTeam(2016, 12, 5);
 91         team[4] = new SoccerTeam(2015, 9, 9);
 92         team[5] = new SoccerTeam(2014, 4, 14);
 93         team[6] = new SoccerTeam(2013, 6, 13);
 94 
 95         outputArea.append("----------------------------\n\n");
 96         for (Integer t = 0; t < team.length; t++) {
 97             outputArea.append(team[t].toString() + "\n");
 98         }
 99         outputArea.append("\n\n");
100         
101         
102         
103         Integer totalWins;
104         totalWins = 0;
105         Integer minLosses = Integer.MAX_VALUE;
106         Integer minPosition = -1;
107         Integer maxLosses = Integer.MIN_VALUE;
108         Integer maxPosition = -1;
109         
110         for (Integer t = 0; t < team.length; t++) {
111 
112             totalWins += team[t].getWins();
113             
114             if (team[t].getLosses() < minLosses) {
115                 minLosses = team[t].getLosses();
116                 minPosition = t;
117             }
118             if (team[t].getLosses() > maxLosses) {
119                 maxLosses = team[t].getLosses();
120                 maxPosition = t;
121             }
122 
123         }
124         
125         outputArea.append("Total wins over the years = " + totalWins + "\n");
126         Double avgWins = totalWins / (team.length * 1.0);
127         outputArea.append("Average wins in any year = " + avgWins + "\n");
128         outputArea.append("Most losses in any year = " + maxLosses + "\n");
129         outputArea.append("\tthat was the team from year " + team[maxPosition].getYear() + "\n");
130         outputArea.append("Fewest losses in any year = " + minLosses + "\n");
131         outputArea.append("\tthat was the team from year " + team[minPosition].getYear() + "\n");
132         
133         
134         
135         
136         
137         
138     }
139     
140     /** This method is called from within the constructor to
141      * initialize the form.
142      * WARNING: Do NOT modify this code. The content of this method is
143      * always regenerated by the Form Editor.
144      */
145     @SuppressWarnings("unchecked")
146     // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
147     private void initComponents() {
148 
149         jScrollPane1 = new javax.swing.JScrollPane();
150         outputArea = new javax.swing.JTextArea();
151 
152         setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
153 
154         outputArea.setColumns(20);
155         outputArea.setFont(new java.awt.Font("Monospaced", 0, 18)); // NOI18N
156         outputArea.setRows(5);
157         jScrollPane1.setViewportView(outputArea);
158 
159         javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
160         getContentPane().setLayout(layout);
161         layout.setHorizontalGroup(
162             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
163             .addGroup(layout.createSequentialGroup()
164                 .addGap(20, 20, 20)
165                 .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 609, Short.MAX_VALUE)
166                 .addContainerGap())
167         );
168         layout.setVerticalGroup(
169             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
170             .addGroup(layout.createSequentialGroup()
171                 .addContainerGap()
172                 .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 559, javax.swing.GroupLayout.PREFERRED_SIZE)
173                 .addContainerGap(32, Short.MAX_VALUE))
174         );
175 
176         pack();
177     }// </editor-fold>                        
178 
179     /**
180      * @param args the command line arguments
181      */
182     public static void main(String args[]) {
183         java.awt.EventQueue.invokeLater(new Runnable() {
184 
185             public void run() {
186                 new MUFrame().setVisible(true);
187             }
188         });
189     }
190     // Variables declaration - do not modify                     
191     private javax.swing.JScrollPane jScrollPane1;
192     private javax.swing.JTextArea outputArea;
193     // End of variables declaration                   
194 }
195