Random math questions generator for Android - java

Any help or advice would be greatly appreciated. I'm trying to create a simple game which generates ten different, random questions. The questions can contain 2, 3 or 4 integers. So something like this: 55 2 − 4 − 101, 102/3/3, 589 − 281, 123 + 5 6 + 2.
The question will be displayed in a textview and then the user can take a guess, entering values into an edittext and then upon clicking a key on a custom keypad I have created it will check the answer, and then display the next question in the sequence of 10.
I know how to create random numbers, just struggling to work out how to create a whole question with random operators (+, -, /, *).
Big thank you to anyone who has the time to construct a reply.

A little of spare time produced a complete example for your case. Create new RandomMathQuestionGenerator.java file and it is cooked for compilation.
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Random;
public class RandomMathQuestionGenerator {
private static final int NUMBER_OF_QUESTIONS = 10;
private static final int MIN_QUESTION_ELEMENTS = 2;
private static final int MAX_QUESTION_ELEMENTS = 4;
private static final int MIN_QUESTION_ELEMENT_VALUE = 1;
private static final int MAX_QUESTION_ELEMENT_VALUE = 100;
private final Random randomGenerator = new Random();
public static void main(String[] args) {
RandomMathQuestionGenerator questionGenerator = new RandomMathQuestionGenerator();
List<Question> randomQuestions = questionGenerator.getGeneratedRandomQuestions();
for (Question question : randomQuestions) {
System.out.println(question);
}
}
public List<Question> getGeneratedRandomQuestions() {
List<Question> randomQuestions = new ArrayList<Question>(NUMBER_OF_QUESTIONS);
for (int i = 0; i < NUMBER_OF_QUESTIONS; i++) {
int randomQuestionElementsCapacity = getRandomQuestionElementsCapacity();
Question question = new Question(randomQuestionElementsCapacity);
for (int j = 0; j < randomQuestionElementsCapacity; j++) {
boolean isLastIteration = j + 1 == randomQuestionElementsCapacity;
QuestionElement questionElement = new QuestionElement();
questionElement.setValue(getRandomQuestionElementValue());
questionElement.setOperator(isLastIteration ? null
: Operator.values()[randomGenerator.nextInt(Operator.values().length)]);
question.addElement(questionElement);
}
randomQuestions.add(question);
}
return randomQuestions;
}
private int getRandomQuestionElementsCapacity() {
return getRandomIntegerFromRange(MIN_QUESTION_ELEMENTS, MAX_QUESTION_ELEMENTS);
}
private int getRandomQuestionElementValue() {
return getRandomIntegerFromRange(MIN_QUESTION_ELEMENT_VALUE, MAX_QUESTION_ELEMENT_VALUE);
}
private int getRandomIntegerFromRange(int min, int max) {
return randomGenerator.nextInt(max - min + 1) + min;
}
}
class Question {
private List<QuestionElement> questionElements;
public Question(int sizeOfQuestionElemets) {
questionElements = new ArrayList<QuestionElement>(sizeOfQuestionElemets);
}
public void addElement(QuestionElement questionElement) {
questionElements.add(questionElement);
}
public List<QuestionElement> getElements() {
return questionElements;
}
public int size() {
return questionElements.size();
}
#Override
public String toString() {
StringBuilder sb = new StringBuilder();
for (QuestionElement questionElement : questionElements) {
sb.append(questionElement);
}
return sb.toString().trim();
}
}
class QuestionElement {
private int value;
private Operator operator;
public int getValue() {
return value;
}
public void setValue(int value) {
this.value = value;
}
public Operator getOperator() {
return operator;
}
public void setOperator(Operator operator) {
this.operator = operator;
}
#Override
public String toString() {
return value + (operator == null ? "" : " " + operator.getDisplayValue()) + " ";
}
}
enum Operator {
PLUS("+"), MINUS("-"), MULTIPLIER("*"), DIVIDER("/");
private String displayValue;
private Operator(String displayValue) {
this.displayValue = displayValue;
}
public String getDisplayValue() {
return displayValue;
}
}
Run and preview. Hope this helps.
Thanks to:
Generating random number in
range
Retrieving random
element from array

Create an array char[] ops = { '+', '-', '/', '*' } and create a random int i in range [0,3], and chose ops[i]
You will need to take care that you do not generate a divide by zero question.
You can make it even more generic by creating an interface MathOp and creating 4 classes that implement it: Divide, Sum , ... and create an array: MathOp[] ops instead of the char[]
Using this, it will also give you much easier time to check the result later on...

Put your operators in an array (4 elements), generate a random integer from 0 to 3, and pick the operator that is at this index in the array.
Do that each time you need to have a random operator, i.e. after every number of your question except the last one.

Make an array that has one entry for each of the operators. Then generate a random number between 0 and the length of the array minus 1.

So since each operation is binary you can just worry about figuring out the base case and then building up your expressions from there.
An easy way would just to select a random number an correlate that which operation will be used.
int displayAnswer(int leftSide, int rightSide, int operation {
int answer;
string operation;
switch(operation) {
case 1:
operation = "+";
answer = leftSide + rightSide;
break;
case 2:
operation = "-";
answer = leftSide - rightSide;
break;
case 3:
operation = "*";
answer = leftSide * rightSide;
break;
case 4:
operation = "/";
answer = leftSide / rightSide:
break;
}
textView.setText(leftSide + operation + rightSide);
return answer;
}

Related

Storing a property in an instance variable or use a method to calculate it?

The first time I came across this was as such: a Box class with a list that contains items. All these items have an int weight instance variable. Now, to get the weight of the Box, should one use an instance variable weight to keep track of this? Or should you use a method calculateWeight() to get the weight?
public class Box {
private ArrayList<Item> list; // Item objects in the list, all Item objects have a weight
private int weight; // Should I: use an instance variable as is seen here <- ?
public int calculateWeight() { // or use a method to calculate the weight each time?
int sum = 0;
// for all items get the weight and add it together
return sum;
}
}
Now in this example, it makes more sense to me to use a method rather than an instance variable, because otherwise the instance variable weight of the Box would have to be updated each time an item is added or removed.
But now I am working on a readability score calculator (project on JetBrains), and I find it less obvious what I am supposed to do. The readability score calculator takes a text and finds the amount of sentences, words, chars, syllables etc. and uses a simple formula to calculate a readability score. For now, I have been storing all the amounts of sentences, words, chars etc. in instance variables by calling the methods for calculating each inside the constructor, but I am not sure whether this is a good practice or not (It feels somewhat cluttered to me). An alternative would be to simply not store them in instance variables and only use the methods to obtain the amounts each time you need them. This is what the code looks like right now:
public abstract class ReadabilityScoreCalculator {
protected String name;
protected String text;
protected int wordCount;
protected int sentenceCount;
protected int charCount;
protected int syllableCount;
protected int polySyllableCount;
protected double score;
protected int age;
public ReadabilityScoreCalculator(String text, String name) {
this.text = text;
this.name = name;
this.wordCount = this.countWords();
this.sentenceCount = this.countSentences();
this.charCount = this.countCharacters();
this.syllableCount = this.countSyllables();
this.polySyllableCount = this.countPolySyllables();
this.score = this.calculateAndReturnScore();
this.age = this.getAgeGroup();
}
private int countWords() {
return this.getArrayOfWords().length;
}
private String[] getArrayOfWords() {
return this.text.split("[ ]+");
}
private int countSentences() {
return this.text.split("[.!?]").length;
}
private int countCharacters() {
String textWithoutSpaces = this.removeSpaces();
return textWithoutSpaces.length();
}
private String removeSpaces() {
return this.text.replaceAll("[ ]+", "");
}
private int countSyllables() {
String[] words = this.getArrayOfWords();
int amountOfSyllables = Arrays.stream(words)
.mapToInt(word -> this.countSyllablesInWord(word))
.reduce(0, (previousCount, amountOfSyllablesInWord) -> previousCount + amountOfSyllablesInWord);
return amountOfSyllables;
}
private int countSyllablesInWord(String word) {
int amountOfSyllablesInWord = 0;
for (int i = 0, n = word.length(); i < n; i++) {
char character = word.charAt(i);
if (this.isCharVowel(character)) {
if (this.isCharVowel(word.charAt(i - 1)) || this.areWeAtTheLastCharAndDoesItEqualE(i, word)) {
continue;
}
amountOfSyllablesInWord++;
}
}
return (amountOfSyllablesInWord == 0) ? 1 : amountOfSyllablesInWord;
}
private boolean isCharVowel(char character) {
String charAsString = String.valueOf(character);
return charAsString.matches("(?i)[aeiouy]");
}
private boolean areWeAtTheLastCharAndDoesItEqualE(int index, String word) {
int wordLength = word.length();
char currentCharacter = word.charAt(index);
return (index == (wordLength - 1) && currentCharacter == 'e');
}
private int countPolySyllables() {
String[] words = this.getArrayOfWords();
int amountOfPolySyllables = Arrays.stream(words)
.mapToInt(word -> this.countSyllablesInWord(word))
.filter(amountOfSyllablesInWord -> amountOfSyllablesInWord > 2)
.reduce(0, (previousCount, amountOfPolySyllablesInWord) -> previousCount + amountOfPolySyllablesInWord);
return amountOfPolySyllables;
}
private double calculateAndReturnScore() {
return this.calculateScore();
}
abstract double calculateScore();
public void setScore(double score) {
this.score = score;
}
public void printResults() {
System.out.println(this.name + ": " + this.score + " (about " + this.age + " year olds).");
}
public void setAgeGroup() {
this.age = this.getAgeGroup();
}
private int getAgeGroup() {
int ageGroup = 0;
switch(this.roundUpAndParseToInt(this.score)) {
case 1:
ageGroup = 6;
break;
case 2:
ageGroup = 7;
break;
case 3:
ageGroup = 9;
break;
case 4:
ageGroup = 10;
break;
case 5:
ageGroup = 11;
break;
case 6:
ageGroup = 12;
break;
case 7:
ageGroup = 13;
break;
case 8:
ageGroup = 14;
break;
case 9:
ageGroup = 15;
break;
case 10:
ageGroup = 16;
break;
case 11:
ageGroup = 17;
break;
case 12:
ageGroup = 18;
break;
case 13:
ageGroup = 24;
break;
case 14:
ageGroup = 24;
break;
}
return ageGroup;
}
public int roundUpAndParseToInt(double number) {
return (int) Math.ceil(number);
}
}
Is one or the other considered good practice or not? Or does it really depend on the situation? I can see that the method way would be more computationally expensive, but gives more certainty. Any other problems with my code above may also be called out.
Edit: this is an abstract class, and the calculateScore() method is supposed to be filled in by the class inheriting from this one. As such, multiple different formulas to calculate the readability score can be used.
In general, omit using calculation-based instance variables in favor of methods for retrieval. More important is whether your code documents itself concisely and is easy to maintain.
By using instance variables, you will need to update them every time their respective properties are modified, and this can muddy the readability of your methods that modify these variables.
For example, here's a few simple methods that modify list and update the weight member variable:
public void addItem(Item item) {
this.list.add(item);
this.weight += item.weight;
}
public Item removeItem(int i) {
Item item = this.list.remove(i);
this.weight -= item.weight;
return item;
}
public void setList(ArrayList<Item> list) {
this.list = list;
this.weight = this.calculateWeight();
}
Because you're performing an additional operation that is not described in the method name, these methods are not self-documenting. You could instead call them something like addItemAndUpdateWeight(), but what if there are many more member variables to update? addItemAndUpdateMemberVariables() is too vague, and very quickly these methods will get very messy and hard to maintain.
You could add a helper function to update your member variables, but you'd still need to call it in every such method, and every time you add a new member variable you'll have to update this method, too.
Plus, if these methods are expensive, it's better to perform the calculations only when needed.
A better solution is to keep it simple by isolating the functionality of your methods:
public void addItem(Item item) {
this.list.add(item);
}
public Item removeItem(int i) {
return this.list.remove(i);
}
public void setList(ArrayList<Item> list) {
this.list = list;
}
Immediately, you know exactly what these methods do, and there's no sneaky code to cause headaches for you later. Plus, now you don't need to perform these operations in your constructor.
I'm sure a case could be made for more complex situations like indexing, but this seems outside the scope of this discussion.

Create a simple math game using java objects

I am trying to create a Simple math game using Java objects. My goal is to create a question class, which contains a method to show a question with random numbers, a method to check the answer, a question constructure.
How can I generate 10 random questions using these objects? With additions, substraction and multiplication questions. And print the how many correct answers, at the end.
I have created my question class and my first method shows a random question using the variables "a" and "b". and store the answer.
My second method check the answer with the user input and print the result. So far when I run my program it only shows the same question over and over again.
This is my Question class
import java.util.*;
public class Question {
private int a;
private int b;
private String Question;
private int correct;
Random rand = new Random();
Scanner input = new Scanner(System.in);
int count = 0;
Question(int c, int d) {
x = rand.nextInt(20);
y = rand.nextInt(20);
}
public void askQuestion() {
if (a > b) {
System.out.println("What is " + a + " - " + b + " ?\n");
correct = a - b;
} else {
System.out.println("What is " + a + " + " + b + " ?\n");
correct = a + b;
}
}
public void Check() {
int response = Integer.parseInt(input.next());
if (response == correct) {
System.out.printf("yes.\n");
count++;
} else {
System.out.printf("No. It is " + correct + ".\n");
}
}
}
my main method looks like this
public class Main {
public static void main(String[] args) {
Question q1 = new Question(1,2);
for (int i = 1; i < 10; i++) {
q1.askQuestion();
q1.check();
}
}
}
In my output it shows the question with two random numbers but it prints the same question over and over again. EX:
What is 13 - 1 ?
12
That is correct.
What is 13 - 1 ?
12
That is correct.
What is 13 - 1 ?
3
Wrong!. The answer is 12.
What is 13 - 1 ?
Eventually I want my output to look like:
What is 4 + 6?
What is 7 - 3?
Any help to fix this? and make this game more interactive? Appreciate it.
Your problem is due to the fact that you are creating one Question object, that generates two random numbers (13 and 1 in your case). You then go through a loop of asking 10 Questions, but you use the same Question object - so you are using the same random numbers every time. To fix this, do the following changes:
In your Question constructor, get rid of the parameters, you do not need them. Assign to variables a and b:
private Question(){
a = rand.nextInt(20);
b = rand.nextInt(20);
}
So every time you create a Question, you will generate two random numbers which you assign to your variables you declared earlier in the code at the top (in your code, a and b are declared, but not used).
Then in your main, change it to the following:
public static void main(String[] args) {
for(int i = 0; i < 10; i++) {
Question q1 = new Question();
q1.askQuestion();
q1.check();
}
System.out.println("Number correct: " + count); //print amount correct, to use this make variable "count" static.
}
The changes are that now you are creating a new Question object every time you go through the loop, and get new random values. Every time it asks a new Question, it will create a new Question object with new random values and overwrite the old ones. It will ask the question 10 times after you give and check the answer, after which the program will output number of correct answers and stop.
Example output for 3 questions:
What is 17 - 15 ?
2
yes.
What is 8 + 11 ?
19
yes.
What is 9 - 0 ?
5
No. It is 9.
Number correct: 2
If you want a way to dynamically ask a question with random numbers and operators, you can create an Operator enum as seen below to handle calculating the result of a left-hand and right-hand value.
Also, the calls to System.out.print should be in the main program as much as possible. Instead, you should return strings from the Question.
All you need to do is pass the two randomly-generated numbers to the Operator enum and ask it to calculate the result.
Exam (main)
package exam;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Exam {
private static int correctCount = 0;
private static List<Question> questions = randomQuestions(10);
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
questions.stream().forEach(question -> ask(question, input));
input.close();
stats();
}
private static void ask(Question question, Scanner input) {
System.out.print(question.askQuestion());
double guess = input.nextDouble();
boolean isCorrect = question.makeGuess(guess);
System.out.println(question.explain(isCorrect));
System.out.println();
correctCount += isCorrect ? 1 : 0;
}
private static void stats() {
double percentage = (correctCount * 1.0d) / questions.size() * 100;
System.out.printf("Correct: %.2f%% (%d/%d)%n", percentage, correctCount, questions.size());
}
private static List<Question> randomQuestions(int count) {
List<Question> questions = new ArrayList<Question>();
while (count --> 0) questions.add(new Question());
return questions;
}
}
Question (class)
package exam;
import java.util.Random;
public class Question {
private static final Random RAND = new Random(System.currentTimeMillis());
private double left;
private double right;
private Operator operator;
public Question(double left, double right, Operator operator) {
this.left = left;
this.right = right;
this.operator = operator;
}
public Question(int max) {
this(randInt(max), randInt(max), Operator.randomOperator());
}
public Question() {
this(10); // Random 0 -> 10
}
public String askQuestion() {
return String.format("What is %s? ", operator.expression(left, right));
}
public String explain(boolean correct) {
return correct ? "Correct" : String.format("Incorrect, it is: %.2f", calculate());
}
public boolean makeGuess(double guess) {
return compareDouble(guess, calculate(), 0.01);
}
private double calculate() {
return operator.calculate(left, right);
}
#Override
public String toString() {
return String.format("%s = %.2f", operator.expression(left, right), calculate());
}
private static boolean compareDouble(double expected, double actual, double threshold) {
return Math.abs(expected - actual) < threshold;
}
private static double randInt(int range) {
return Math.floor(RAND.nextDouble() * range);
}
}
Operator (enum)
package exam;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Random;
public enum Operator {
ADD("+", (left, right) -> left + right),
SUB("-", (left, right) -> left - right),
MUL("*", (left, right) -> left * right),
DIV("/", (left, right) -> left / right);
private static final Random RAND = new Random(System.currentTimeMillis());
private static final List<Operator> VALUES = Collections.unmodifiableList(Arrays.asList(values()));
private static final int SIZE = VALUES.size();
public static Operator randomOperator() {
return VALUES.get(RAND.nextInt(SIZE));
}
private String symbol;
private Operation operation;
private Operator(String symbol, Operation operation) {
this.symbol = symbol;
this.operation = operation;
}
public double calculate(double left, double right) {
return operation.calculate(left, right);
}
public String expression(double left, double right) {
return String.format("%.2f %s %.2f", left, symbol, right);
}
#Override
public String toString() {
return symbol;
}
}
Operation (interface)
package exam;
public interface Operation {
double calculate(double left, double right);
}

Reverse a number using String builder in java

Problem Statement: Given a 32-bit signed integer, reverse digits of an integer.
Example 1:
Input: 123
Output: 321
Example 2:
Input: -123
Output: -321
My Solution:
class Solution7{
public int reverse(int x) {
if(x>Integer.MAX_VALUE || x<Integer.MIN_VALUE) {
return 0;
}
StringBuilder S_rev = new StringBuilder();
String S_r_v=S_rev.append(Math.abs(x)).reverse().toString();//.toString() String builder to String
double reverse_no=Double.parseDouble(S_r_v);
if (x < 0) {
return -(int)reverse_no;
}
return (int)reverse_no;
}
}
My Solution is ok for most of the test case. But it cannot pass one test case and I got a error
Error: Line 10: java.lang.NumberFormatException: For input string: "8463847412-"
If someone know what type of error it is please discuss.
Thank you in advance.
It seems like you are trying to pass in Integer.MIN_VALUE
When you pass in the minimum integer value, Math.abs seems to return a negative number as stated here
https://docs.oracle.com/javase/8/docs/api/java/lang/Math.html#abs-int-
Note that if the argument is equal to the value of Integer.MIN_VALUE, the most negative representable int value, the result is that same value, which is negative.
You can either check for x<=Integer.MIN_VALUE and return 0 if x is Integer.MIN_VALUE or handle the special case for Integer.MIN_VALUE
if(x== Integer.MIN_VALUE)
return -8463847412;
By converting number to String and reversing the sign symbol ended up on the end of the value. This makes the number invalid.
You don't have to convert to String or double. You can use module operator % to extract digits:
public int reverse(int x) {
long result = 0;
while (x != 0) {
result *= 10;
result += x % 10;
x /= 10;
}
if (result > Integer.MAX_VALUE || result < Integer.MIN_VALUE) {
throw new IllegalArgumentException(); // overflow
}
return result;
}
If you necessarily want to implement it using StringBuilder, here it is:
public static void main(String[] args) {
ReverseNum reverseNum = new ReverseNum();
System.out.println(reverseNum.reverse(-123));
System.out.println(reverseNum.reverse(123));
System.out.println(reverseNum.reverse(0));
}
public int reverse(int x) {
int res = 1;
String xStr = String.valueOf(x);
StringBuilder builder = null;
if (xStr.startsWith("-")) {
builder = new StringBuilder(xStr.substring(1));
res = -1;
} else {
builder = new StringBuilder(xStr);
}
return res * Integer.valueOf(builder.reverse().toString());
}
Output:
-321
321
0
P.S. If you want to avoid integer overflow, then you can simply use long instead of int, like this:
public long reverse(int x) {
long res = 1;
String xStr = String.valueOf(x);
StringBuilder builder = null;
if (xStr.startsWith("-")) {
builder = new StringBuilder(xStr.substring(1));
res = -1;
} else {
builder = new StringBuilder(xStr);
}
return res * Long.valueOf(builder.reverse().toString());
}
public class ReverseString {
public static void main(String args[]) {
ReverseString rs = new ReverseString();
System.out.println(rs.reverse(-84638));
System.out.println(rs.reverse(5464867));
}
public long reverse(int number) {
boolean isNegative = number < 0;
StringBuilder reverseBuilder = new StringBuilder();
String reversedString = reverseBuilder.append(Math.abs(number)).reverse().toString();
long reversedStringValue = Long.parseLong(reversedString);
if(isNegative) {
return reversedStringValue * -1;
} else {
return reversedStringValue;
}
}
}
This code provides the output you have mentioned in the requirement. And It also supports for integer overflow. Your requirement is to convert int values. It is okay to get the converted value in the higher format since converted value may not be in the range of int. I have changed the reverse method return type to long.
I have identified a few issues in your code.
public int reverse(int x) {
if(x>Integer.MAX_VALUE || x<Integer.MIN_VALUE) {
return 0;
}
Above code segment, not point of checking whether the value is inside the int range because it is already received in the param as a string. It should throw an error before executing your code lines since it is not able to fit the larger value to int variable.
Finally, the int number you have used is not in the int range. (-8463847412)
What about this?
public class ReverseNumber {
public static void main(String[] args) {
System.out.println(reverse(123456));
System.out.println(reverse(0));
System.out.println(reverse(-987654));
}
private static int reverse(int i) {
final int signum;
if(i < 0) {
signum = -1;
} else {
signum = +1;
}
int reversedNumber = 0;
int current = Math.abs(i);
while(0 < current) {
final int cipher = current % 10;
reversedNumber = Math.addExact(Math.multiplyExact(reversedNumber, 10), cipher);
current = current / 10;
}
return signum * reversedNumber;
}
}
Output:
654321
0
-456789
This solution avoids strings and can handle negative numbers.
It throws an Arithmetic exception if an integer overflow happens.

Return the result of each iteration in the loop

I'm doing something that produces the right result. However, it is wrong from a design POV.
The point of the program is to list the result of all the powers of a number up to and including the user-defined limit.
I have a constructor which accepts the base and the exponent from the Scanner. Then a method, which utilises a for loop to calculate the power for each exponent.
Now, the problem is that I'm printing the result from each loop iteration directly from this method. This beats the point of private variables and it being void in the 1st place.
Therefore, I want to define a getter method which returns the result of each power to the output. I used to set them just fine for if/switch statements, but I don't know how to do the same for loops. If I assign the result to a variable within the loop and return that variable from the getter then it will return only the output from the final iteration.
Private implementation
package Chapter6Review;
public class Powers {
private int target;
private int power;
public Powers(int target, int power) {
this.target = target;
this.power = power;
}
public void calculatePower() {
for (int i = 0; i <= power; i++) {
System.out.println((int) Math.pow(target, i));
}
}
/*
public int getPower() {
return
}
*/
}
User interface
package Chapter6Review;
import java.util.Scanner;
public class PowersTester {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.print("Enter your base: ");
int target = in.nextInt();
System.out.print("Enter your exponent: ");
int power = in.nextInt();
Powers tester = new Powers(target, power);
tester.calculatePower();
}
}
You can simply use a List ;
public List<Integer> calculatePower() {
int p;
List<Integer> result = new ArrayList<Integer>();
for (int i = 0; i <= power; i++) {
p = (int) Math.pow(target, i);
result.add(p);
}
return result;
}
Then in you main method, you can iterate the list to print the powers like that :
List<Integer> result = new ArrayList<Integer>();
Powers tester = new Powers(target, power);
result = tester.calculatePower();
for (int i = 0; i < result.size(); i++) {
System.out.println(result.get(i));
}
You could store each of the results in a List:
List<Power> list = new ArrayList<>();
and when you call it add it as well
list.add(new Powers(target, power));
At the end you can iterate over the list like this:
for (Power power : list){
// your code
}
You might consider using streams as well
public List<Integer> calculatePower() {
return IntStream
.rangeClosed(0, power). // iterate from 0 till power inclusive
.mapToObj(i -> (int) Math.pow(target,i))
.collect(Collectors.toList()); // get result as list
}
Thanks for all the answers. Using a list seems to be a good choice.
Since I haven't covered lists yet, I resorted to this solution for now. But I don't like having code that can affect the solution in the main. Ideally, the loop should go in the private implementation.
Main
Powers tester = new Powers(target, power);
for (int i = 0; i <= power; i++) {
tester.calculatePower(i);
System.out.println(tester.getPower());
}
Private implementation
public void calculatePower(int iPower) {
result = (int) Math.pow(target, iPower);
}
public int getPower() {
return result;
}

Sequence of random numbers without repeats

I am trying to do a pvp event in my game server which uses 3 zones to do it randomly. I use the following code but always is returning me the values 1 and 2 and repeated as well. I need some sequence like this for example: 3-2-1-2-1-3 or something that never repeats the same number.
int random = Rnd.get(1, 3);
if (random == 1)
{
setstartedpvpzone1(true);
}
if (random == 2)
{
setstartedpvpzone2(true);
}
if (random == 3)
{
setstartedpvpzone3(true);
}
this is what i get in rnd:
public final class Rnd
{
/**
* This class extends {#link java.util.Random} but do not compare and store atomically.<br>
* Instead it`s using a simple volatile flag to ensure reading and storing the whole 64bit seed chunk.<br>
* This implementation is much faster on parallel access, but may generate the same seed for 2 threads.
* #author Forsaiken
* #see java.util.Random
*/
public static final class NonAtomicRandom extends Random
{
private static final long serialVersionUID = 1L;
private volatile long _seed;
public NonAtomicRandom()
{
this(++SEED_UNIQUIFIER + System.nanoTime());
}
public NonAtomicRandom(final long seed)
{
setSeed(seed);
}
#Override
public final int next(final int bits)
{
return (int) ((_seed = ((_seed * MULTIPLIER) + ADDEND) & MASK) >>> (48 - bits));
}
#Override
public final void setSeed(final long seed)
{
_seed = (seed ^ MULTIPLIER) & MASK;
}
}
and rnd.get:
/**
* Gets a random integer number from min(inclusive) to max(inclusive)
* #param min The minimum value
* #param max The maximum value
* #return A random integer number from min to max
*/
public static final int get(final int min, final int max)
{
return rnd.get(min, max);
}
If all you are looking for is a random number that doesn't equal the previous one returned then the solution is much simpler:
private Random random = new Random();
private int previousZone = 0;
public int nextZone() {
int zone;
do {
zone = random.nextInt(3) + 1;
} while (zone == previousZone);
previousZone = zone; //store last "generated" zone
return zone;
}
[not tested] It is possible that it may contain some syntax errors as I am not a Java programmer.
int a=0,b=0;
while(true)
{
int random = Rnd.get(1, 3);
if(!(a==random or b==random))
{
a=b;
b=random;
break;
}
}
if (random == 1)
{
setstartedpvpzone1(true);
}
if (random == 2)
{
setstartedpvpzone2(true);
}
if (random == 3)
{
setstartedpvpzone3(true);
}
Your problem boils down to a graph traversal in which from each current zone, you only have 2 possible next zones and those choices never change. So here is how I would implement it:
public static class EventLocator{
private int currentZone;
private Random random;
private Map<Integer, int[]> locations;
private static EventLocator instance;
private EventLocator() {
}
public static EventLocator getInstance(){
if (instance == null) {
instance = new EventLocator();
}
return instance;
}
public int getNextZone(){
if (this.currentZone == 0) {//first time called
this.random = new Random();
this.locations = new HashMap<>(3);//graph <currentZone, posibleZones>
this.locations.put(1, new int[] { 2, 3 });
this.locations.put(2, new int[] { 1, 3 });
this.locations.put(3, new int[] { 1, 2 });
this.currentZone = this.random.nextInt(3) + 1;// to 1-based Zones
return currentZone;
}
int[] possibleZones = this.locations.get(this.currentZone);
int randomIndex = this.random.nextInt(2);//0 or 1 index
this.currentZone = possibleZones[randomIndex];
return this.currentZone;
}
}
You would call it like:
EventLocator eventLocator = MyProgram.EventLocator.getInstance();
System.out.println(eventLocator.getNextZone());
System.out.println(eventLocator.getNextZone());
This code never repeats any numbers, for example if you have 1,2,3 you can get a random sequence of 4 numbers, example 2,1,3.
Create an array with all numbers you need...
int[] a = {1, 2, 3};
Then select random items
for (int i=0; i<a.length; i++){
int random = Rnd.get(0, a.length);
//remove the selected item from the array
ArrayUtils.removeElement(a, random);
if (random == 1) {
setstartedpvpzone1(true);
} else if (random == 2) {
setstartedpvpzone2(true);
} else if (random == 3) {
setstartedpvpzone3(true);
}
}
private boolean _lastevent1 = false;
public boolean lastevent1()
{
return _lastevent1;
}
public void setlastevent1(boolean val)
{
_lastevent1 = val;
}
private boolean _lastevent2 = false;
public boolean lastevent2()
{
return _lastevent2;
}
public void setlastevent2(boolean val)
{
_lastevent2 = val;
}
private boolean _lastevent3 = false;
public boolean lastevent3()
{
return _lastevent3;
}
public void setlastevent3(boolean val)
{
_lastevent3 = val;
}
if (!lastevent1())
{
setlastevent3(false);
setstartedpvpzone3(false);
setstartedpvpzone1(true);
setlastevent1(true);
}
else if (!lastevent2())
{
setstartedpvpzone1(false);
setstartedpvpzone2(true);
setlastevent2(true);
}
else if (!lastevent3())
{
setlastevent1(false);
setlastevent2(false);
setstartedpvpzone2(false);
setstartedpvpzone3(true);
setlastevent3(true);
}
hello finally i fixed using booleans and i get this secuence, 1-2-3-1-2-3-1-2-3-1-2-3 , i breaked my mind with it because is very confuse this code but it work now as a charm , thanks for all to try to help me i very appreciate it, great community.

Categories