Reputation: 31
for some reason when i call on this class it doesn't return me anything. class will need two instance properties, die1 and die2, which will be used to store the value of each die(1-6).
//define class variables here
Random rd = new Random();
int die1;
int die2;
//define class methods here
method requires no arguments. When called, the method will generate random numbers between 1-6 and assign them to die1 and die2. A different random number will be generated for each die. The return type should be void.
public void roll () {
this.die1 = 1 + rd.nextInt(6);
this.die2 = 1 + rd.nextInt(6);
}
method will return the sum of both die values as an integer when called. This should be a number between 2 and 12.
public int getTotal() {
int sum = die1 + die2;
return sum;
}
Getter method that returns the value of the first die.
public int getDice1(int dice1) {
this.die1 = dice1;
return this.die1;
}
Getter method that returns the value of the second die.
public int getDice2(int dice2) {
this.die2 = dice2;
return this.die2;
}
}
Upvotes: 1
Views: 979
Reputation: 159
Main problem is, you have implemented setters with the name of getters.
Getter are the methods with which we return a value. Setter are use to set values to variables.
According to your getters, you have to provide an integer to the method and it will be assign to the die1 or die2 variable. It is the task of a setter.
Following getter will help you to return the value.
public int getDice1() {
return this.die1;
}
public int getDice2() {
return this.die2;
}
If you want to see the output, I have modify your code below. Please run it on your text editor (not here) and see.
import java.util.Random;
public class Main {
public static void main(String[] args) {
Game game = new Game();
game.roll();
System.out.println(game.getDice1());
System.out.println(game.getDice2());
System.out.println(game.getTotal());
}
}
class Game{
Random rd = new Random();
int die1;
int die2;
public void roll () {
this.die1 = 1 + rd.nextInt(6);
this.die2 = 1 + rd.nextInt(6);
}
public int getTotal() {
int sum = die1 + die2;
return sum;
}
public int getDice1() {
return this.die1;
}
public int getDice2() {
return this.die2;
}
}
Hope this will help you.
Upvotes: 1