Skip to content

Commit 83533f2

Browse files
вынесение констант из Utils.java, замена if на тернарный оператор в Prime.java
1 parent 2e1fefe commit 83533f2

File tree

6 files changed

+23
-20
lines changed

6 files changed

+23
-20
lines changed
Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,9 @@
11
package hexlet.code;
22

33
public class Utils {
4-
//границы случайных чисел не были обозначены сделал от 1 до 100. требуется уточнение у аналитика
5-
public static final int MIN_NUMBER = 1;
6-
public static final int MAX_NUMBER = 100;
74

85
public static int generateNumber(int min, int max) {
96
return (int) Math.floor(Math.random() * (max - min + 1)) + min;
107
}
118

12-
public static int generateNumber() {
13-
return generateNumber(MIN_NUMBER, MAX_NUMBER);
14-
}
159
}

app/src/main/java/hexlet/code/games/Calc.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,16 @@
44
import hexlet.code.Utils;
55

66
public class Calc {
7+
public static final int MIN_NUMBER = 1;
8+
public static final int MAX_NUMBER = 100;
9+
710
public static void play() {
811
String optionDescription = "What is the result of the expression?";
912
String[][] questionsAndCorrectAnswers = new String[Engine.ROUNDS][2];
1013

1114
for (int i = 0; i < Engine.ROUNDS; i++) {
12-
int firstValue = Utils.generateNumber();
13-
int secondValue = Utils.generateNumber();
15+
int firstValue = Utils.generateNumber(MIN_NUMBER, MAX_NUMBER);
16+
int secondValue = Utils.generateNumber(MIN_NUMBER, MAX_NUMBER);
1417
char[] signs = {'+', '-', '*'};
1518
char randomSign = signs[Utils.generateNumber(0, signs.length - 1)];
1619

app/src/main/java/hexlet/code/games/Even.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,15 @@
44
import hexlet.code.Utils;
55

66
public class Even {
7+
public static final int MIN_NUMBER = 1;
8+
public static final int MAX_NUMBER = 100;
9+
710
public static void play() {
811
String optionDescription = "Answer 'yes' if the number is even, otherwise answer 'no'.";
912
String[][] questionsAndCorrectAnswers = new String[Engine.ROUNDS][2];
1013

1114
for (int i = 0; i < Engine.ROUNDS; i++) {
12-
int random = Utils.generateNumber();
15+
int random = Utils.generateNumber(MIN_NUMBER, MAX_NUMBER);
1316
String question = String.valueOf(random);
1417
boolean isEven = random % 2 == 0;
1518
String correctAnswer = isEven ? "yes" : "no";

app/src/main/java/hexlet/code/games/Gcd.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,16 @@
44
import hexlet.code.Utils;
55

66
public class Gcd {
7+
public static final int MIN_NUMBER = 1;
8+
public static final int MAX_NUMBER = 100;
9+
710
public static void play() {
811
String optionDescription = "Find the greatest common divisor of given numbers.";
912
String[][] questionsAndCorrectAnswers = new String[Engine.ROUNDS][2];
1013

1114
for (int i = 0; i < Engine.ROUNDS; i++) {
12-
int firstValue = Utils.generateNumber();
13-
int secondValue = Utils.generateNumber();
15+
int firstValue = Utils.generateNumber(MIN_NUMBER, MAX_NUMBER);
16+
int secondValue = Utils.generateNumber(MIN_NUMBER, MAX_NUMBER);
1417

1518
String question = firstValue + " " + secondValue;
1619
String correctAnswer = String.valueOf(gcd(firstValue, secondValue));

app/src/main/java/hexlet/code/games/Prime.java

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,18 @@
44
import hexlet.code.Utils;
55

66
public class Prime {
7+
public static final int MIN_NUMBER = 1;
8+
public static final int MAX_NUMBER = 100;
9+
710
public static void play() {
811
String optionDescription = "Answer 'yes' if given number is prime. Otherwise answer 'no'.";
912
String[][] questionsAndCorrectAnswers = new String[Engine.ROUNDS][2];
1013

1114
for (int i = 0; i < Engine.ROUNDS; i++) {
12-
int random = Utils.generateNumber();
15+
int random = Utils.generateNumber(MIN_NUMBER, MAX_NUMBER);
1316

1417
String question = String.valueOf(random);
15-
String correctAnswer;
16-
if (isPrime(random)) {
17-
correctAnswer = "yes";
18-
} else {
19-
correctAnswer = "no";
20-
}
18+
String correctAnswer = isPrime(random) ? "yes" : "no";
2119

2220
questionsAndCorrectAnswers[i][0] = question;
2321
questionsAndCorrectAnswers[i][1] = correctAnswer;

app/src/main/java/hexlet/code/games/Progression.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
import hexlet.code.Utils;
55

66
public class Progression {
7+
public static final int MIN_NUMBER = 1;
8+
public static final int MAX_NUMBER = 100;
79
private static final int STEP_COUNT = 10;
810
private static final int VALUE_COUNT = 10;
911

@@ -12,8 +14,8 @@ public static void play() {
1214
String[][] questionsAndCorrectAnswers = new String[Engine.ROUNDS][2];
1315

1416
for (int i = 0; i < Engine.ROUNDS; i++) {
15-
int start = Utils.generateNumber();
16-
int step = Utils.generateNumber(Utils.MIN_NUMBER, STEP_COUNT);
17+
int start = Utils.generateNumber(MIN_NUMBER, MAX_NUMBER);
18+
int step = Utils.generateNumber(MIN_NUMBER, STEP_COUNT);
1719
int hideIndex = Utils.generateNumber(0, VALUE_COUNT - 1);
1820

1921
String[] sequence = addSequence(start, step, VALUE_COUNT);

0 commit comments

Comments
 (0)