-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLab5.java
More file actions
263 lines (227 loc) · 8.52 KB
/
Copy pathLab5.java
File metadata and controls
263 lines (227 loc) · 8.52 KB
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.VBox;
import javafx.scene.text.TextAlignment;
import javafx.stage.Stage;
/**
* @author Clara
* @version 1.0 2/21/2020
*
* Creates 10 digit number problem
* A Java algorithm and a UI created with the JavaFX graphics library which both verifies a user imputed ten-digit number and finds a unique ten-digit number where each sub number is divisible by its length (ex. 38165/5 = 7633).
*/
public class Lab5 extends Application {
private TextField uInput = new TextField("");
private Label message = new Label("");
private Label tenDigNum = new Label("");
private int divis = 0;
private Label number = new Label("");
/**
* @param field a textField that the user enters the number they want to check into
* @param newVal the new value the user has input into the textField
*
* Makes sure the value the user is trying to enter into the textField is an integer
*/
private static void checkInt (TextField field, String newVal) {
field.setText(field.getText().trim());
if (!newVal.matches("\\d*")) {
field.setText(newVal.replaceAll("[^\\d]", ""));
}
}
/**
* Makes sure the user entered number cannot be longer than 10 characters
* Calculates whether the user entered number is properly divisible and change a label to represent the findings
*/
private void intActions () {
uInput.textProperty().addListener((obs, oldVal, newVal) -> {
checkInt(uInput, newVal);
if (!uInput.getText().equals("")) {
if (uInput.getText().length() > 10) {
uInput.setText(oldVal);
}
}
});
uInput.setOnAction(new EventHandler<ActionEvent>() {
public void handle(ActionEvent ae) {
if (uInput.getText().equals("")) {
uInput.setText("0");
}
divisible();
if (divis == uInput.getText().length()) {
message.setText("YES");
} else {
message.setText("NO: " + uInput.getText().substring(0, divis + 1) + " is not divisible by " + (divis + 1));
}
}
});
}
/**
* checks if a user entered number is divisible by all of its parts
*/
private void divisible () {
int i;
divis = 0;
for (i = 0; i < uInput.getText().length(); i++) {
if ((Long.parseLong(uInput.getText().substring(0, i+1)) % (i + 1) == 0) && (divis == i))divis = i + 1;
}
}
/**
* calls the methods to make part one function
*/
private void part1() {
uInput.setPromptText("Enter an integer");
intActions();
}
/**
* @param num a long which is the current long being checked for correct divisibility
* @return the length of the long num
*
* Finds and returns the length of long num
*/
private int length(long num) {
int temp = 1;
int length = 0;
while (temp < num) {
length ++;
temp *= 10;
}
return length;
}
/**
* @param num a long which is the current long being checked for correct divisibility
* @return returns true if the long num could be divided by its last digit
*
* checks if the long num can be divided without remainder by its last digit
*/
private boolean check(long num) {
boolean divid = true;
int length;
length = length(num);
if (num % length != 0) divid = false;
return divid;
}
/**
* @param num a long which is the current long being checked for correct divisibility
* @param newNum an integer which is the last digit of long num
* @return the boolean unique which is true if all of the digits in long num are unique
*
* Checks whether int newNum is already in long num
*/
private boolean integerIn(long num, int newNum) {
String sNum = Long.toString(num);
Character sNewNum = Integer.toString(newNum).charAt(0);
boolean unique = true;
int i;
for (i = 0; i < sNum.length()-1; i++) {
if (sNewNum.equals(sNum.charAt(i))) unique = false;
}
return unique;
}
/**
* @param num a long which is the current long being checked for correct divisibility
* @param add the number being added to the end of the long num
*
* This method is called if num is unique and divisible without a remainder
* If the length of long num is not 9, 9 is added to the end of num and part2 is recalled
* If the length of long num is 9 0 is added to it and the label for the 10 digit number is changed
*/
private void works (long num, int add) {
if (length(num) < 9) {
add = 9;
num = num * 10 + add;
part2(num, add);
} else {
number.setText(Long.toString(num * 10));
part2(((num / 10) * 10) + ((num % 10) - 1), (int)((num % 10) - 1));
}
}
/**
* @param num a long which is the current long being checked for correct divisibility
* @param add the number being added to the end of the long num
*
* This method is called if the last digit of long num is not unique or not divisible and add = 1
* It recalls part two but num is now one place smaller and the last digit and add are 9
*/
private void moveBackPlace (long num, int add) {
num = (num / 10) - 1;
if (num % 10 == 0) num = (num / 10) - 1;
if (num == 1 ) {
add = 9;
num = 19;
}
else add = (int) num % 10;
part2(num, add);
}
/**
* @param num a long which is the current long being checked for correct divisibility
* @param add the number being added to the end of the long num
*
* This method uses recursion and backtracking to start at number 9 and find all of the unique digit 10 digit numbers that each sub-number is divisible by its length
*/
private void part2(long num, int add) {
boolean uniq = true;
boolean div;
if (num != 123) {
div = check(num);
if (num > 10) {
uniq = integerIn(num, add);
} else {
uniq = true;
}
if (div && uniq) {
works (num, add);
} else if ((!uniq && add > 1) || (uniq && add > 1)) {
num = ((num / 10) * 10) + (add - 1);
part2(num, add - 1);
} else if (add < 2) {
moveBackPlace(num, add);
}
}
}
/**
* prints out proof that sub-number of the 10 digit unique numbers are divisible by their lengths
*/
private void print() {
String numb = number.getText();
int i;
for (i = 1; i < 11; i++) {
number.setText(number.getText() + "\n" + numb.substring(0,i) + " / " + i + " = " + Long.parseLong(numb) / i);
}
}
/**
* @param args arguments
*
* main method which launches the program
*/
public static void main(String[] args) {
launch(args);
}
/**
* @param myStage stage which the scene is set on
*
* Calls methods for parts 1 and 2, sets up formatting
*/
public void start(Stage myStage) {
myStage.setTitle("Ten Digit Number Problem");
Label title = new Label("Enter an integer to check if every sub-number from the left is divisible by its size");
VBox rootNode = new VBox(20);
Scene myScene = new Scene(rootNode, 500, 630);
myStage.setScene(myScene);
rootNode.setAlignment(Pos.CENTER);
rootNode.setPadding(new Insets(10,30,10,30));
title.setWrapText(true);
number.setTextAlignment(TextAlignment.CENTER);
part1();
part2(9, 8);
tenDigNum.setText("The ten digit numbers are: ");
print();
rootNode.getChildren().addAll(title, uInput, message, tenDigNum, number);
myStage.show();
}
}