###Use a class to manage state instead of passing it around.
Use a class to manage state instead of passing it around.
Right now your program has 3 variables that represent the programs state. min
, max
and randomNumber
.
The state is being passed around to wherever it's needed as function arguments:
public static int getRandom(int min, int max) {...}
getRandom(min, max);
public static void playRandom(int min, int max, int randomNumber) {...}
playRandom(min, max, randomNumber);
You could simplify these method calls and method signatures by using a class that holds this state as fields, and making these methods part of the class.
public class Game {
private int min;
private int max;
private int randomNumber;
...
public void getInterval() {...} // parameter no longer needed
public void getRandom() {...}
public void playRandom() {...}
}
Instead of returning the min and max from getInterval
, you would assign the result to these fields:
public void getInterval() {
Scanner inputMin = new Scanner(System.in);
System.out.println("Please Input Min number for guessing: ");
// return Integer.parseInt(inputMin.nextLine());
this.min = Integer.parseInt(input.nextLine()); // the "this." is optional here.
System.out.println("Please Input Max number for guessing: ");
this.max = Integer.parseInt(input.nextLine());
}
Then in getRandom
, you would use the fields instead of method parameters:
// return rand.nextInt((max - min) + 1) + min;
// this.randomNumber = rand.nextInt((this.max - this.min) + 1) + this.min;
randomNumber = rand.nextInt((max - min) + 1) + min; // leaving out "this." here.
The result would be simpler method calls and signatures, since there is no more need for the arguments:
Game game = new Game();
public void getRandom() {...}
game.getRandom();
public void playRandom() {...}
game.playRandom();
The state that was previously passed around is now managed internally by the class.
###Use a class to manage state instead of passing it around.
Right now your program has 3 variables that represent the programs state. min
, max
and randomNumber
.
The state is being passed around to wherever it's needed as function arguments:
public static int getRandom(int min, int max) {...}
getRandom(min, max);
public static void playRandom(int min, int max, int randomNumber) {...}
playRandom(min, max, randomNumber);
You could simplify these method calls and method signatures by using a class that holds this state as fields, and making these methods part of the class.
public class Game {
private int min;
private int max;
private int randomNumber;
...
public void getInterval() {...} // parameter no longer needed
public void getRandom() {...}
public void playRandom() {...}
}
Instead of returning the min and max from getInterval
, you would assign the result to these fields:
public void getInterval() {
Scanner inputMin = new Scanner(System.in);
System.out.println("Please Input Min number for guessing: ");
// return Integer.parseInt(inputMin.nextLine());
this.min = Integer.parseInt(input.nextLine()); // the "this." is optional here.
System.out.println("Please Input Max number for guessing: ");
this.max = Integer.parseInt(input.nextLine());
}
Then in getRandom
, you would use the fields instead of method parameters:
// return rand.nextInt((max - min) + 1) + min;
// this.randomNumber = rand.nextInt((this.max - this.min) + 1) + this.min;
randomNumber = rand.nextInt((max - min) + 1) + min; // leaving out "this." here.
The result would be simpler method calls and signatures, since there is no more need for the arguments:
Game game = new Game();
public void getRandom() {...}
game.getRandom();
public void playRandom() {...}
game.playRandom();
The state that was previously passed around is now managed internally by the class.
Use a class to manage state instead of passing it around.
Right now your program has 3 variables that represent the programs state. min
, max
and randomNumber
.
The state is being passed around to wherever it's needed as function arguments:
public static int getRandom(int min, int max) {...}
getRandom(min, max);
public static void playRandom(int min, int max, int randomNumber) {...}
playRandom(min, max, randomNumber);
You could simplify these method calls and method signatures by using a class that holds this state as fields, and making these methods part of the class.
public class Game {
private int min;
private int max;
private int randomNumber;
...
public void getInterval() {...} // parameter no longer needed
public void getRandom() {...}
public void playRandom() {...}
}
Instead of returning the min and max from getInterval
, you would assign the result to these fields:
public void getInterval() {
Scanner inputMin = new Scanner(System.in);
System.out.println("Please Input Min number for guessing: ");
// return Integer.parseInt(inputMin.nextLine());
this.min = Integer.parseInt(input.nextLine()); // the "this." is optional here.
System.out.println("Please Input Max number for guessing: ");
this.max = Integer.parseInt(input.nextLine());
}
Then in getRandom
, you would use the fields instead of method parameters:
// return rand.nextInt((max - min) + 1) + min;
// this.randomNumber = rand.nextInt((this.max - this.min) + 1) + this.min;
randomNumber = rand.nextInt((max - min) + 1) + min; // leaving out "this." here.
The result would be simpler method calls and signatures, since there is no more need for the arguments:
Game game = new Game();
public void getRandom() {...}
game.getRandom();
public void playRandom() {...}
game.playRandom();
The state that was previously passed around is now managed internally by the class.
###Use a class to manage state instead of passing it around.
Right now your program has 3 variables that represent the programs state. min
, max
and randomNumber
.
The state is being passed around to wherever it's needed as function arguments:
public static int getRandom(int min, int max) {...}
getRandom(min, max);
public static void playRandom(int min, int max, int randomNumber) {...}
playRandom(min, max, randomNumber);
You could simplify these method calls and method signatures by using a class that holds this state as fields, and making these methods part of the class.
public class Game {
private int min;
private int max;
private int randomNumber;
...
public void getInterval(String text) {...} // parameter no longer needed
public void getRandom() {...}
public void playRandom() {...}
}
Instead of returning the min and max from getInterval
, you would assign the result to these fields:
public void getInterval() {
Scanner inputMin = new Scanner(System.in);
System.out.println("Please Input Min number for guessing: ");
// return Integer.parseInt(inputMin.nextLine());
this.min = Integer.parseInt(input.nextLine()); // the "this." is optional here.
System.out.println("Please Input Max number for guessing: ");
this.max = Integer.parseInt(input.nextLine());
}
Then in getRandom
, you would use the fields instead of method parameters:
// return rand.nextInt((max - min) + 1) + min;
// this.randomNumber = rand.nextInt((this.max - this.min) + 1) + this.min;
randomNumber = rand.nextInt((max - min) + 1) + min; // leaving out "this." here.
The result would be simpler method calls and signatures, since there is no more need for the arguments:
Game game = new Game();
public void getRandom() {...}
game.getRandom();
public void playRandom() {...}
game.playRandom();
The state that was previously passed around is now managed internally by the class.
###Use a class to manage state instead of passing it around.
Right now your program has 3 variables that represent the programs state. min
, max
and randomNumber
.
The state is being passed around to wherever it's needed as function arguments:
public static int getRandom(int min, int max) {...}
getRandom(min, max);
public static void playRandom(int min, int max, int randomNumber) {...}
playRandom(min, max, randomNumber);
You could simplify these method calls and method signatures by using a class that holds this state as fields, and making these methods part of the class.
public class Game {
private int min;
private int max;
private int randomNumber;
...
public void getInterval(String text) {...}
public void getRandom() {...}
public void playRandom() {...}
}
Instead of returning the min and max from getInterval
, you would assign the result to these fields:
// return Integer.parseInt(inputMin.nextLine());
this.min = Integer.parseInt(input.nextLine()); // the "this." is optional here.
Then in getRandom
, you would use the fields instead of method parameters:
// return rand.nextInt((max - min) + 1) + min;
// this.randomNumber = rand.nextInt((this.max - this.min) + 1) + this.min;
randomNumber = rand.nextInt((max - min) + 1) + min; // leaving out "this." here.
The result would be simpler method calls and signatures, since there is no more need for the arguments:
Game game = new Game();
public void getRandom() {...}
game.getRandom();
public void playRandom() {...}
game.playRandom();
The state that was previously passed around is now managed internally by the class.
###Use a class to manage state instead of passing it around.
Right now your program has 3 variables that represent the programs state. min
, max
and randomNumber
.
The state is being passed around to wherever it's needed as function arguments:
public static int getRandom(int min, int max) {...}
getRandom(min, max);
public static void playRandom(int min, int max, int randomNumber) {...}
playRandom(min, max, randomNumber);
You could simplify these method calls and method signatures by using a class that holds this state as fields, and making these methods part of the class.
public class Game {
private int min;
private int max;
private int randomNumber;
...
public void getInterval() {...} // parameter no longer needed
public void getRandom() {...}
public void playRandom() {...}
}
Instead of returning the min and max from getInterval
, you would assign the result to these fields:
public void getInterval() {
Scanner inputMin = new Scanner(System.in);
System.out.println("Please Input Min number for guessing: ");
// return Integer.parseInt(inputMin.nextLine());
this.min = Integer.parseInt(input.nextLine()); // the "this." is optional here.
System.out.println("Please Input Max number for guessing: ");
this.max = Integer.parseInt(input.nextLine());
}
Then in getRandom
, you would use the fields instead of method parameters:
// return rand.nextInt((max - min) + 1) + min;
// this.randomNumber = rand.nextInt((this.max - this.min) + 1) + this.min;
randomNumber = rand.nextInt((max - min) + 1) + min; // leaving out "this." here.
The result would be simpler method calls and signatures, since there is no more need for the arguments:
Game game = new Game();
public void getRandom() {...}
game.getRandom();
public void playRandom() {...}
game.playRandom();
The state that was previously passed around is now managed internally by the class.
###Use a class to manage state instead of passing it around.
Right now your program has 3 variables that represent the programs state. min
, max
and randomNumber
.
The state is being passed around to wherever it's needed as function arguments:
public static int getRandom(int min, int max) {...}
getRandom(min, max);
public static void playRandom(int min, int max, int randomNumber) {...}
playRandom(min, max, randomNumber);
You could simplify these method calls and method signatures by using a class that holds this state as fields, and making these methods part of the class.
public class Game {
private int min;
private int max;
private int randomNumber;
...
public void getInterval(String text) {...}
public void getRandom() {...}
public void playRandom() {...}
}
Instead of returning the min and max from getInterval
, you would assign the result to these fields:
// return Integer.parseInt(inputMin.nextLine());
this.min = Integer.parseInt(input.nextLine()); // the "this." is optional here.
Then in getRandom
, you would use the fields instead of method parameters:
// return rand.nextInt((max - min) + 1) + min;
// this.randomNumber = rand.nextInt((this.max - this.min) + 1) + this.min;
randomNumber = rand.nextInt((max - min) + 1) + min; // leaving out "this." here.
The result would be simpler method calls and signatures, since there is no more need for the arguments:
Game game = new Game();
public void getRandom() {...}
game.getRandom();
public void playRandom() {...}
game.playRandom();
The state that was previously passed around is now managed internally by the class.
###Use a class to manage state instead of passing it around.
Right now your program has 3 variables that represent the programs state. min
, max
and randomNumber
.
The state is being passed around to wherever it's needed as function arguments:
public static int getRandom(int min, int max) {...}
getRandom(min, max);
public static void playRandom(int min, int max, int randomNumber) {...}
playRandom(min, max, randomNumber);
You could simplify these method calls and method signatures by using a class that holds this state as fields, and making these methods part of the class.
public class Game {
private int min;
private int max;
private int randomNumber;
...
public void getInterval(String text) {...}
public void getRandom() {...}
public void playRandom() {...}
}
Instead of returning the min and max from getInterval
, you would assign the result to these fields:
// return Integer.parseInt(inputMin.nextLine());
this.min = Integer.parseInt(input.nextLine()); // the "this." is optional here.
Then in getRandom
, you would use the fields instead of method parameters:
// return rand.nextInt((max - min) + 1) + min;
// this.randomNumber = rand.nextInt((this.max - this.min) + 1) + this.min;
randomNumber = rand.nextInt((max - min) + 1) + min; // leaving out "this." here.
The result would be simpler method calls and signatures, since there is no more need for the arguments:
Game game = new Game();
public void getRandom() {...}
game.getRandom();
public void playRandom() {...}
game.playRandom();
The state that was previously passed around is now managed internally by the class.
###Use a class to manage state instead of passing it around.
Right now your program has 3 variables that represent the programs state. min
, max
and randomNumber
.
The state is being passed around to wherever it's needed as function arguments:
public static int getRandom(int min, int max) {...}
getRandom(min, max);
public static void playRandom(int min, int max, int randomNumber) {...}
playRandom(min, max, randomNumber);
You could simplify these method calls and method signatures by using a class that holds this state as fields, and making these methods part of the class.
public class Game {
private int min;
private int max;
private int randomNumber;
...
public void getInterval(String text) {...}
public void getRandom() {...}
public void playRandom() {...}
}
Instead of returning the min and max from getInterval
, you would assign the result to these fields:
// return Integer.parseInt(inputMin.nextLine());
this.min = Integer.parseInt(input.nextLine()); // the "this." is optional here.
Then in getRandom
, you would use the fields instead of method parameters:
// return rand.nextInt((max - min) + 1) + min;
// this.randomNumber = rand.nextInt((this.max - this.min) + 1) + this.min;
randomNumber = rand.nextInt((max - min) + 1) + min; // leaving out "this." here.
The result would be simpler method calls and signatures, since there is no more need for the arguments:
Game game = new Game();
public void getRandom() {...}
game.getRandom();
public void playRandom() {...}
game.playRandom();
The state that was previously passed around is now managed internally by the class.
- 271
- 2
- 9