-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExercise14_1.java
223 lines (197 loc) · 6.05 KB
/
Exercise14_1.java
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
package exercises;
/** Exercise 14.1:Data handler Write a program that uses a 4 * 7 array of numbers similar to the rainfall
* program (with output as shown in Figure 14.2). Extend the program to carry out the following operations:
* When a button marked ‘sums’ is pressed, add up the values for each of the seven columns and add up all
* the values of each of the four rows and display them. When a button marked ‘largest’ is pressed,
* find the largest value in each row, the largest in each column and the largest value in the complete array.
* When a button marked ‘scale’ is pressed, multiply every number in the array by a number
* entered into a text field. (This could be used to convert from centimetres to inches. (p274 – 6th Edition)
Filename: Exercise14_1.java
@author: © Gary Hill (200WXYZ)
Course: BSc Computing
Module: CSY1020 Problem Solving & Programming
Tutor: Gary Hill
@version: 1.0
Date: 11/06/19
*/
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class Exercise14_1 {
private JFrame frmExercise;
private JTextField textField;
private JTextField textField_1;
private JTextField textField_2;
private JTextField textField_3;
int[][] number;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Exercise14_1 window = new Exercise14_1();
window.frmExercise.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public Exercise14_1() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frmExercise = new JFrame();
frmExercise.setTitle("Exercise14_1");
frmExercise.setBounds(100, 100, 450, 300);
frmExercise.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frmExercise.getContentPane().setLayout(null);
JLabel lblInsert = new JLabel("Insert");
lblInsert.setBounds(10, 25, 48, 14);
frmExercise.getContentPane().add(lblInsert);
textField = new JTextField();
textField.setBounds(68, 25, 96, 20);
frmExercise.getContentPane().add(textField);
textField.setColumns(10);
textField_1 = new JTextField();
textField_1.setBounds(10, 119, 96, 20);
frmExercise.getContentPane().add(textField_1);
textField_1.setColumns(10);
textField_2 = new JTextField();
textField_2.setBounds(131, 119, 96, 20);
frmExercise.getContentPane().add(textField_2);
textField_2.setColumns(10);
textField_3 = new JTextField();
textField_3.setBounds(249, 119, 96, 20);
frmExercise.getContentPane().add(textField_3);
textField_3.setColumns(10);
JLabel lblColumns = new JLabel("Columns");
lblColumns.setBounds(131, 103, 66, 14);
frmExercise.getContentPane().add(lblColumns);
JLabel lblNewLabel = new JLabel("New label");
lblNewLabel.setBounds(158, 78, 201, 14);
frmExercise.getContentPane().add(lblNewLabel);
JLabel lblRows = new JLabel("Rows");
lblRows.setBounds(10, 103, 66, 14);
frmExercise.getContentPane().add(lblRows);
number = new int[4][7];
for(int i =0;i<4;i++) {
for(int j=0; j<7;j++) {
number[i][j] = 0;
}
}
JLabel lblArra = new JLabel("Array");
lblArra.setBounds(258, 103, 66, 14);
frmExercise.getContentPane().add(lblArra);
JButton btnInsert = new JButton("Insert");
btnInsert.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(number.length<28) {
for(int i=0;i<4;i++) {
for(int j=0;j<7;j++) {
if(number[i][j]== 0) {
number[i][j] = Integer.parseInt(textField.getText());
break;
}
}break;
}
}
textField.setText(null);
for(int i =0;i<4;i++) {
for(int j=0; j<7;j++) {
lblNewLabel.setText(Integer.toString(number[i][j]));
}
}
}
});
btnInsert.setBounds(20, 56, 89, 23);
frmExercise.getContentPane().add(btnInsert);
JButton btnSums = new JButton("Sums");
btnSums.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int sum =0, csum=0, rsum=0;
for (int i =0;i<4;i++) {
for (int j=0;j<7;j++) {
csum += number[j][i];
break;
}
}
for (int i =0;i<4;i++) {
for (int j=0;j<7;j++) {
rsum += number[i][j];
break;
}
}
for(int i =0;i<4;i++) {
for(int j=0; j<7;j++) {
sum += number[i][j];
}
}
textField_3.setText(Integer.toString(sum));
textField_2.setText(Integer.toString(rsum));
textField_1.setText(Integer.toString(csum));
}
});
btnSums.setBounds(10, 169, 89, 23);
frmExercise.getContentPane().add(btnSums);
JButton btnLargest = new JButton("Largest");
btnLargest.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int big =0, cbig=0, rbig=0;
for (int i =0;i<4;i++) {
for (int j=0;j<7;j++) {
if(number[i][j]>cbig) {
cbig = number[i][j];
}
}break;
}
for (int i =0;i<4;i++) {
for (int j=0;j<7;j++) {
if(number[i][j]>rbig) {
rbig = number[i][j];
break;
}
}
}
for(int i =0;i<4;i++) {
for(int j=0; j<7;j++) {
if(number[i][j]>big) {
big = number[i][j];
}
}
textField_3.setText(Integer.toString(big));
textField_2.setText(Integer.toString(rbig));
textField_1.setText(Integer.toString(cbig));
}
}
});
btnLargest.setBounds(126, 169, 89, 23);
frmExercise.getContentPane().add(btnLargest);
JButton btnScale = new JButton("Scale");
btnScale.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int scale = Integer.parseInt(textField.getText());
int[][] mux = new int [4][7];
for(int i =0;i<4;i++) {
for(int j=0; j<7;j++) {
mux[i][j] = scale * number[i][j];
}
}
}
});
btnScale.setBounds(235, 169, 89, 23);
frmExercise.getContentPane().add(btnScale);
}
}