1
2
3
4
5
6
7
8
9
10
11 import java.awt.*;
12 import javax.swing.*;
13
14 public class MUFrame extends JPanel {
15
16 private Car[] imported, domestic;
17
18 public MUFrame() {
19 setUpGUI();
20
21
22
23 imported = new Car[6];
24 domestic = new Car[8];
25
26
27
28 imported[0] = new Car(Color.GREEN, 101, 35, "Mitsubishi");
29 imported[1] = new Car(Color.WHITE, 86, 40, "Toyota");
30 imported[2] = new Car(Color.RED, 200, 16, "Jaguar");
31 imported[3] = new Car(Color.YELLOW, 70, 25, "Honda");
32 imported[4] = new Car(Color.BLUE, 62, 23, "Nissan");
33 imported[5] = new Car(Color.RED, 110, 26, "Hyundai");
34
35 domestic[0] = new Car(Color.WHITE, 80, 33, "Chrysler");
36 domestic[1] = new Car(Color.YELLOW, 71, 40, "Ford");
37 domestic[2] = new Car(Color.BLUE, 90, 13, "Cadillac");
38 domestic[3] = new Car(Color.MAGENTA, 75, 34, "Chevy");
39 domestic[4] = new Car(Color.WHITE, 64, 28, "Dodge");
40 domestic[5] = new Car(Color.GREEN, 110, 24, "Buick");
41 domestic[6] = new Car(Color.CYAN, 99, 17, "Lincoln");
42 domestic[7] = new Car(Color.RED, 66, 21, "Jeep");
43
44
45
46 System.out.print("For the imported car array: ");
47 displayFastestSpeed( imported );
48 System.out.print("For the domestic car array: ");
49 displayFastestSpeed( domestic );
50 displayABlankLine();
51
52 System.out.println("Fastest Domestic Car goes " + returnFastestSpeed( domestic ) + " miles per hour");
53 System.out.println("Fastest Imported Car goes " + returnFastestSpeed( imported ) + " miles per hour");
54 displayABlankLine();
55
56
57
58 System.out.println("All Domestic Cars:");
59 System.out.println("==================");
60 displayAllCars(domestic);
61 displayABlankLine();
62 System.out.println("All Imported Cars:");
63 System.out.println("==================");
64 displayAllCars(imported);
65 displayABlankLine();
66
67 System.out.print("For the imported cars, ");
68 displayAvgMPGFor(imported);
69 displayABlankLine();
70 System.out.print("For the domestic cars, ");
71 displayAvgMPGFor(domestic);
72 displayABlankLine();
73
74 System.out.println("The domestic car array contains " + returnNumberOfRedCars(domestic) + " red cars, and");
75 System.out.println("The imported car array has " + returnNumberOfRedCars(imported) + " cars that are red");
76 displayABlankLine();
77
78
79
80 System.out.println("The imported car with the fastest speed is the " + returnNameOfFastestCar(imported));
81 System.out.println("The domestic car with the fastest speed is the " + returnNameOfFastestCar(domestic));
82 displayABlankLine();
83
84
85
86 if (thereIsAtLeastOneCyanCar(imported) == true) {
87 System.out.println("There are Cyan-colored cars in the imported array");
88 }
89 else {
90 System.out.println("There DEFINITELY are NOT ANY Cyan cars in the imported array");
91 }
92 if (thereIsAtLeastOneCyanCar(domestic)) {
93 System.out.println("There are domestic Cyan-colored cars in the array");
94 }
95 else {
96 System.out.println("There DEFINITELY are NOT ANY domestic cars that are Cyan");
97 }
98 displayABlankLine();
99
100 }
101
102 public void displayABlankLine() {
103 System.out.println();
104 }
105
106
107 public void displayFastestSpeed( Car[] theArray ) {
108
109 Integer fastestSpeed = theArray[0].getMaxMph();
110
111 for (Integer i = 0; i < theArray.length; i++) {
112
113 if ( theArray[i].getMaxMph() > fastestSpeed ) {
114
115 fastestSpeed = theArray[i].getMaxMph();
116
117 }
118
119 }
120
121 System.out.println("The fastest car in this collection goes "
122 + fastestSpeed
123 + " miles per hour.");
124 }
125
126
127 public Integer returnFastestSpeed( Car[] theArray ) {
128
129 Integer fastestSpeed = Integer.MIN_VALUE;
130
131 for (Integer i = 0; i < theArray.length; i++) {
132
133 if ( theArray[i].getMaxMph() > fastestSpeed ) {
134
135 fastestSpeed = theArray[i].getMaxMph();
136
137 }
138
139 }
140
141 return fastestSpeed;
142 }
143
144
145
146
147 public void displayAllCars( Car[] theArray ) {
148
149 for (Integer i = 0; i < theArray.length; i++) {
150
151 System.out.println(theArray[i].toString());
152
153 }
154
155 }
156
157
158 public void displayAvgMPGFor( Car[] theArray ) {
159
160 Double total = 0.0;
161
162 for (Integer i = 0; i < theArray.length; i++) {
163
164 total = total + theArray[i].getMpg();
165
166 }
167
168 Double avg = total / theArray.length;
169
170 System.out.println("average MPG for this array is " + avg);
171
172 }
173
174
175
176
177
178
179
180
181
182 public Integer returnNumberOfRedCars( Car[] theArray ) {
183
184 Integer count = 0;
185
186 for (Integer x = 0; x < theArray.length; x++) {
187 if (theArray[x].getCarColor().equals(Color.RED)) {
188 count++;
189 }
190 }
191
192 return count;
193 }
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210 public String returnNameOfFastestCar( Car[] theArray ) {
211
212 String fastestCarName = "";
213 Integer fastestCarPosition = 0;
214
215 for (Integer i = 0; i < theArray.length; i++) {
216 if (theArray[i].getMaxMph() > theArray[fastestCarPosition].getMaxMph()) {
217 fastestCarPosition = i;
218 }
219 }
220
221 fastestCarName = theArray[fastestCarPosition].getName();
222
223 return fastestCarName;
224 }
225
226
227
228
229
230
231
232
233
234
235 public Boolean thereIsAtLeastOneCyanCar( Car[] theArray ) {
236
237 Boolean answer = false;
238
239
240 for (Integer num = 0; num < theArray.length; num++) {
241 if (theArray[num].getCarColor().equals(Color.CYAN)) {
242 answer = true;
243 }
244 }
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260 return answer;
261 }
262
263
264
265
266
267
268 public void setUpGUI() {
269 setLayout(null);
270 setName("CSC 120 Lab 10");
271 setPreferredSize(new Dimension(570, 250));
272 setBackground(Color.WHITE);
273
274 }
275
276 @Override
277 public void paintComponent(Graphics g) {
278 super.paintComponent(g);
279
280 g.setFont(new Font("Serif", Font.PLAIN, 18));
281 g.drawString("Domestic Cars:", 6, 20);
282 g.setFont(new Font("SansSerif", Font.PLAIN, 12));
283 for (int i = 0; i<domestic.length; i++) {
284 domestic[i].draw(g, 6 + i*73, 40);
285 }
286 g.setFont(new Font("Serif", Font.PLAIN, 18));
287 g.drawString("Imported Cars:", 6, 150);
288 g.setFont(new Font("SansSerif", Font.PLAIN, 12));
289 for (int i = 0; i<imported.length; i++) {
290 imported[i].draw(g, 6 + i*73, 170);
291 }
292 }
293
294 public void frame() {
295 for (Component c: getComponents())
296 c.setSize(c.getPreferredSize());
297 JFrame f = new JFrame(getName());
298 f.setContentPane(this);
299 f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
300 f.pack();
301 f.setVisible(true);
302 }
303
304 public static void main(String args[]){new MUFrame().frame();}
305
306 }