The following methods are used to generate Random Number Class for a Guessing Ga
ID: 3710827 • Letter: T
Question
The following methods are used to generate Random Number Class for a Guessing Game. Could you please give a detailed explanation of what each method does? And also please add comments for each of the methods and method body.
RANDOM NUMBER CLASS:
import java.util.Random;
public class RandomNumber {
int randomNumber;
public RandomNumber() {
Random rand = new Random();
this.randomNumber=rand.nextInt(99)+1;
}
public int compareNumber(int i){
if(this.randomNumber==i)
return 0;
else if (this.randomNumber
return 1;
else
return -1;
}
public int getRandomNumber() {
return randomNumber;
}
public void setRandomNumber(int randomNumber) {
this.randomNumber = randomNumber;
}
}
Explanation / Answer
see the commented code below...
===============================================================
//import Random package
import java.util.Random;
//this is the RandomNumber class
public class RandomNumber
{
//variable declration
int randomNumber;
// This is the Constructor for class RandomNumber
public RandomNumber()
{
//here i have created and object of Random
Random rand = new Random();
//generate a random number between 1 to 99
this.randomNumber=rand.nextInt(99)+1;
}
//this is the compareNumber method which comares the number
public int compareNumber(int i){
//if random number is equal to i return 0
if(this.randomNumber==i)
return 0;
//else return 1
else if (this.randomNumber)
return 1;
//else return -1
else
return -1;
}
//accessor
public int getRandomNumber() {
return randomNumber;
}
//mutator
public void setRandomNumber(int randomNumber) {
this.randomNumber = randomNumber;
}
}
===============================================================
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.