-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathProblem$21.java
205 lines (168 loc) · 6.68 KB
/
Problem$21.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
package chapter_thirty_one;
import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
/**
* *31.21 (Use tab panes) Write a program using tab panes for performing integer and rational
* number arithmetic as shown in Figure 31.58.
*
*
* @author Sharaf Qeshta
* */
public class Problem$21 extends Application
{
@Override
public void start(Stage stage)
{
TabPane tabPane = new TabPane();
Tab integersOperations = new Tab("Integer Operations");
Label integerCalculationLabel = new Label("Integer Calculation");
HBox integerNumbersContainer = new HBox(5);
integerNumbersContainer.setAlignment(Pos.CENTER);
Label number1IntegerLabel = new Label("Number 1: ");
TextField number1Integer = new TextField();
number1Integer.setPrefColumnCount(6);
Label number2IntegerLabel = new Label("Number 2: ");
TextField number2Integer = new TextField();
number2Integer.setPrefColumnCount(6);
Label resultIntegerLabel = new Label("Result: ");
TextField resultInteger = new TextField();
resultInteger.setPrefColumnCount(6);
resultInteger.setEditable(false);
integerNumbersContainer.getChildren().addAll(number1IntegerLabel, number1Integer,
number2IntegerLabel, number2Integer, resultIntegerLabel, resultInteger);
Button addIntegers = new Button("Add");
Button subtractIntegers = new Button("Subtract");
Button multiplyIntegers = new Button("Multiply");
Button divideIntegers = new Button("Divide");
HBox integerOperationsContainer = new HBox(addIntegers, subtractIntegers,
multiplyIntegers, divideIntegers);
integerOperationsContainer.setAlignment(Pos.CENTER);
VBox integersCalculation = new VBox(5, integerCalculationLabel,
integerNumbersContainer, integerOperationsContainer);
integersOperations.setContent(integersCalculation);
Tab rationalsOperations = new Tab("Rational Operations");
Label rationalCalculationLabel = new Label("Rational Calculation");
HBox rationalNumbersContainer = new HBox(5);
rationalNumbersContainer.setAlignment(Pos.CENTER);
Label number1RationalLabel = new Label("Number 1: ");
TextField number1Rational = new TextField();
number1Rational.setPrefColumnCount(6);
Label number2RationalLabel = new Label("Number 2: ");
TextField number2Rational = new TextField();
number2Rational.setPrefColumnCount(6);
Label resultRationalLabel = new Label("Result: ");
TextField resultRational = new TextField();
resultRational.setPrefColumnCount(6);
resultRational.setEditable(false);
rationalNumbersContainer.getChildren().addAll(number1RationalLabel, number1Rational,
number2RationalLabel, number2Rational, resultRationalLabel, resultRational);
Button addRationals = new Button("Add");
Button subtractRationals = new Button("Subtract");
Button multiplyRationals = new Button("Multiply");
Button divideRationals = new Button("Divide");
HBox rationalOperationsContainer = new HBox(addRationals, subtractRationals,
multiplyRationals, divideRationals);
rationalOperationsContainer.setAlignment(Pos.CENTER);
VBox rationalsCalculation = new VBox(5, rationalCalculationLabel,
rationalNumbersContainer, rationalOperationsContainer);
rationalsOperations.setContent(rationalsCalculation);
tabPane.getTabs().addAll(integersOperations, rationalsOperations);
Scene scene = new Scene(tabPane, 500, 250);
stage.setTitle("Problem$21");
stage.setScene(scene);
stage.show();
addIntegers.setOnAction(e ->
{
int x, y;
try
{
x = Integer.parseInt(number1Integer.getText());
y = Integer.parseInt(number2Integer.getText());
resultInteger.setText((x + y) + "");
}
catch (Exception ignored) { }
});
subtractIntegers.setOnAction(e ->
{
int x, y;
try
{
x = Integer.parseInt(number1Integer.getText());
y = Integer.parseInt(number2Integer.getText());
resultInteger.setText((x - y) + "");
}
catch (Exception ignored) { }
});
multiplyIntegers.setOnAction(e ->
{
int x, y;
try
{
x = Integer.parseInt(number1Integer.getText());
y = Integer.parseInt(number2Integer.getText());
resultInteger.setText((x * y) + "");
}
catch (Exception ignored) { }
});
divideIntegers.setOnAction(e ->
{
int x, y;
try
{
x = Integer.parseInt(number1Integer.getText());
y = Integer.parseInt(number2Integer.getText());
resultInteger.setText((x / y) + "");
}
catch (Exception ignored) { }
});
addRationals.setOnAction(e ->
{
double x, y;
try
{
x = Double.parseDouble(number1Rational.getText());
y = Double.parseDouble(number2Rational.getText());
resultRational.setText((x + y) + "");
}
catch (Exception ignored) { }
});
subtractRationals.setOnAction(e ->
{
double x, y;
try
{
x = Double.parseDouble(number1Rational.getText());
y = Double.parseDouble(number2Rational.getText());
resultRational.setText((x - y) + "");
}
catch (Exception ignored) { }
});
multiplyRationals.setOnAction(e ->
{
double x, y;
try
{
x = Double.parseDouble(number1Rational.getText());
y = Double.parseDouble(number2Rational.getText());
resultRational.setText((x * y) + "");
}
catch (Exception ignored) { }
});
divideRationals.setOnAction(e ->
{
double x, y;
try
{
x = Double.parseDouble(number1Rational.getText());
y = Double.parseDouble(number2Rational.getText());
resultRational.setText((x / y) + "");
}
catch (Exception ignored) { }
});
}
}