This repository has been archived by the owner on Oct 28, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TestRational.java
120 lines (105 loc) · 4.07 KB
/
TestRational.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
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.*;
public class TestRational {
public static void main(String args[]) {
Rational group1[] = generate();
Rational group2[] = generate();
JFrame form = new JFrame();
JPanel rootPanel = new JPanel();
JLabel mainlabel = new JLabel();
JTextArea group1Text = new JTextArea();
JTextArea group2Text = new JTextArea();
JTextArea group3Text = new JTextArea();
JButton quitButton = new JButton("Quit");
JButton addButton = new JButton("Add");
JButton subtractButton = new JButton("Subtract");
JButton multiplyButton = new JButton("Multiply");
JButton divideButton = new JButton("Divide");
JScrollPane sp1 = new JScrollPane(group1Text);
JScrollPane sp2 = new JScrollPane(group2Text);
JScrollPane sp3 = new JScrollPane(group3Text);
group1Text.setColumns(10);
group1Text.setRows(30);
group2Text.setColumns(10);
group2Text.setRows(30);
group3Text.setColumns(10);
group3Text.setRows(30);
for(int i = 0; i < group1.length; i++){
group1Text.append(group1[i].printRational()+"\n");
}
for(int i = 0; i < group2.length; i++){
group2Text.append(group2[i].printRational()+"\n");
}
quitButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent actionEvent) {
form.dispose();
}
});
addButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent actionEvent) {
group3Text.setText(null);
for(int i = 0; i < group1.length; i++) {
Rational result = group1[i].add(group2[i]);
group3Text.append(result.printRational()+"\n");
}
}
});
subtractButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent actionEvent) {
group3Text.setText(null);
for(int i = 0; i < group1.length; i++) {
Rational result = group1[i].subtract(group2[i]);
group3Text.append(result.printRational()+"\n");
}
}
});
multiplyButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent actionEvent) {
group3Text.setText(null);
for(int i = 0; i < group1.length; i++) {
Rational result = group1[i].multiply(group2[i]);
group3Text.append(result.printRational()+"\n");
}
}
});
divideButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent actionEvent) {
group3Text.setText(null);
for(int i = 0; i < group1.length; i++) {
Rational result = group1[i].divide(group2[i]);
group3Text.append(result.printRational()+"\n");
}
}
});
form.setContentPane(rootPanel);
form.add(mainlabel);
form.add(sp1);
form.add(sp2);
form.add(sp3);
form.add(quitButton);
form.add(addButton);
form.add(subtractButton);
form.add(multiplyButton);
form.add(divideButton);
form.pack();
form.setVisible(true);
form.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static Rational[] generate() {
Rational[] group = new Rational[10];
for(int i = 0; i < group.length; i++){
Random randomGenerator = new Random();
int num = randomGenerator.nextInt((8 - 1) + 1) + 1;
int den = randomGenerator.nextInt((9 - num) + 1) + num;
group[i] = new Rational(num, den);
}
return group;
}
}