demonstrating 2 variables in a sequence.
my assignment is to manipulate a pre existing java source code to allow rolls of 2 dice at the same time. it also tells me to include methods to set each die value, one to roll the die, and one that returns the current face values of the die.
i have some code here and was wondering if i was headed in the right direction. any hints/guidance would be greatly appreciated. vague help from any part is very welcome as i can just do my own research on how to do specific things if prompted.
Code:
public class PairOfDice
{
private final int MAX = 6; // max val
private int faceValue1;
private int faceValue2;
public PairOfDice() // start value
{
faceValue1 = 1;
faceValue2 = 1;
}
public int roll() // simulates dice roll
{
faceValue1 = (int)(Math.random() * MAX) + 1;
faceValue2 = (int)(Math.random() * MAX) + 1;
return faceValue;
}
public void setFaceValue (int value) //sets-changes face values based on user command
{
faceValue = value;
faceValue = value;
}
public int getFaceValue() // gets the face values
{
return faceValue1;
return faceValue2;
}
public String toString() //returns results
{
String result = Integer.toString(faceValue);
return result;
}
}
Re: demonstrating 2 variables in a sequence.
What was the existing code?
I'm wondering if you ought to be having two attributes of whatever class the original code was.
Re: demonstrating 2 variables in a sequence.
Your roll() method is doing the job of setting the faceValues i guess. So your setFaceValue() method seems redundant(one more thing... it makes a reference to faceValue. Shouldnt it be faceValue1 and faceValue2 in that method??).
Pardon me if I'm wrong. Might have misinterpreted something.
Apart from the above point everything else seems to on the money ;)
Re: demonstrating 2 variables in a sequence.
thanks i did a change on that, i def forgot to do a final run before checking that out haha. and also the original code didnt differ from this one much, it was actually just a single variable one dice scenario. my next task is to use a driver class to roll the sets of die 6 times and also display their sum face value upon each roll. should i be using some sort of a loop to get the die rolls or is a simple println statement going to be sufficient to get 6 sets of random rolls?
Re: demonstrating 2 variables in a sequence.
I'd use a loop. Then if you want to change then number of print outs, you'd only have to change one value.
Re: demonstrating 2 variables in a sequence.
in order to make the loop non infinate do i have to use a count system or does it have to be nested? also are there any simpler ways to make it have a max amount of cycles?
Re: demonstrating 2 variables in a sequence.
A counter is useful for limiting the number of cycles in a loop.
I don't see what nesting has to do with this.
Re: demonstrating 2 variables in a sequence.
oh maybe i was confused about what a nested loop actually was. i just read that it was a way to cease infinites
Re: demonstrating 2 variables in a sequence.
still a bit of a java noob, but i attempted to make a counter using an example from my book and i seem to be getting errors from other things. ive been troubleshooting the sequences of the return values from throwing errors. also could anyone confirm if my counter would actaully work here? i have to initiate the method from a driver class.
heres the errors and the code
Quote:
--------------------Configuration: ProjectTwo - JDK version 1.7.0_02 <Default> - <Default>--------------------
C:\Users\Michael\Documents\JCreator LE\MyProjects\ProjectTwo\PairOfDice.java:19: error: ';' expected
return faceValue1, faceValue2;
^
C:\Users\Michael\Documents\JCreator LE\MyProjects\ProjectTwo\PairOfDice.java:19: error: not a statement
return faceValue1, faceValue2;
^
2 errors
Process completed.
Code:
public class PairOfDice
{
private final int MAX = 6; // max val
private int faceValue1;
private int faceValue2;
public PairOfDice() // start value
{
faceValue1 = 1;
faceValue2 = 1;
}
public int roll() // simulates dice roll
{
faceValue1 = (int)(Math.random() * MAX) + 1;
faceValue2 = (int)(Math.random() * MAX) + 1;
return faceValue1, faceValue2;
}
public void rolls()
{
faceValue1 = (int)(Math.random() * MAX) + 1;
faceValue2 = (int)(Math.random() * MAX) + 1;
int sum = faceValue1 + faceValue2;
int count = 1;
while (count < 7){
System.out.println("Die 1:" + faceValue1);
System.out.println("Die 2:" + faceValue2);
System.out.println("Total value:" + sum);
count++;
}
}
public void setFaceValue (int value) //sets-changes face values based on user command
{
faceValue1 = value;
faceValue2 = value;
}
public int getFaceValue() // gets the face values
{
return faceValue1;
return faceValue2;
}
public String toString() //returns results
{
String result = Integer.toString(faceValue1);
return result;
}
}
Re: demonstrating 2 variables in a sequence.
A method can only return one item. If you want a method to return more than one value, you must create a new class to hold those values and return an instance of that class that contains those values.
Why not call roll two times, once for each value?
Re: demonstrating 2 variables in a sequence.
but if i call roll twice, how can i get a sum value for 2 rolls per each set of dice rolled.
example
die 1: 3
die 2: 2
Total: 5
die 1: 6
die 2: 1
Total: 7
but for 6 rolls and not 2.
Re: demonstrating 2 variables in a sequence.
Quote:
if i call roll twice, how can i get a sum value for 2 rolls
Can you explain what the problem is?
Roll one - save
Roll two - add to saved
If you want to save this value you should use an array to save the total value in for each set of two rolls.
array[setofRolls] = saved
Re: demonstrating 2 variables in a sequence.
not totally sure if we went over arrays in class so im not totally sure if i can use them in this project, ill take a look back and see.if i were to use an array i would have to put it in the driver class and have something like
rolled.roll();
anArray[0] = roll;
rolled.roll();
anArray[1] = roll;
int sum = anArray[0] + anArray[1];
Re: demonstrating 2 variables in a sequence.
No, you would not use an array that way. Just use two variables to receive the value returned by the method.
var1 = roll();
var2 = roll();
Re: demonstrating 2 variables in a sequence.
would i be using the array in the driver class or the class with my methods?
also ive updated my die method to each return one value
Code:
public class PairOfDice
{
private final int MAX = 6; // max val
private int faceValue1;
private int faceValue2;
public PairOfDice() // start value
{
faceValue1 = 1;
faceValue2 = 1;
}
public int roll1() // simulates dice roll
{
faceValue1 = (int)(Math.random() * MAX) + 1;
return faceValue1;
}
public int roll2() // simulates dice roll
{
faceValue2 = (int)(Math.random() * MAX) + 1;
return faceValue2;
}
public void setFaceValue1 (int value) //sets-changes face values based on user command
{
faceValue1 = value;
}
public void setFaceValue2 (int value) //sets-changes face values based on user command
{
faceValue2 = value;
}
public int getFaceValue() // gets the face values
{
return faceValue1 + faceValue2;
}
public String toString() //returns results
{
String result = Integer.toString(faceValue1);
return result;
}
}
Re: demonstrating 2 variables in a sequence.
What is the difference between the roll1 and roll2 methods? Do they do the same thing? Do you only need one of them to do what you want?
If the die has 6 faces should there be a setFaceValue method for all six faces?
Why isn't there getFaceValue method for each of the faces? Your method adds two values together.
Your logic makes no sense. A die has ONE up face when it is rolled. The other 5 faces do not count.
Each roll of the die gives one value.
If you have more than one die, each one will have its own value.
Re: demonstrating 2 variables in a sequence.
it was pretty late, no idea what i was doing -.- but i fixed the code and it looks like it works
Code:
import java.util.Random;
public class PairOfDice
{
private final int MAX = 6; // max val
private int faceValue;
public PairOfDice() // start value
{
faceValue = 1;
}
public int roll() // simulates dice roll
{
faceValue = (int)(Math.random() * MAX) + 1;
return faceValue;
}
public void setFaceValue (int value) //sets-changes face values based on user command
{
faceValue = value;
}
public int getFaceValue() // gets the face values
{
return faceValue;
}
public String toString() //returns results
{
String result = Integer.toString(faceValue);
return result;
}
}
driver class
Code:
public class RollingDice2
{
public static void main(String[] args)
{
PairOfDice dice1, dice2;
int sum;
dice1 = new PairOfDice(); //dice objects set at 1
dice2 = new PairOfDice();
dice1.roll();//initiated rolls for first set
dice2.roll();
sum = dice1.getFaceValue() + dice2.getFaceValue();
System.out.println("Die 1 is " + dice1);
System.out.println("Die 2 is " + dice2);
System.out.println("Set Total: " + sum);
System.out.println(); //spaces to keep output clean
System.out.println();
dice1.roll();//initiated rolls for second set
dice2.roll();
sum = dice1.getFaceValue() + dice2.getFaceValue();
System.out.println("Die 1 is " + dice1);
System.out.println("Die 2 is " + dice2);
System.out.println("Set Total: " + sum);
System.out.println(); //spaces to keep output clean
System.out.println();
dice1.roll();//initiated rolls for third set
dice2.roll();
sum = dice1.getFaceValue() + dice2.getFaceValue();
System.out.println("Die 1 is " + dice1);
System.out.println("Die 2 is " + dice2);
System.out.println("Set Total: " + sum);
System.out.println(); //spaces to keep output clean
System.out.println();
dice1.roll();//initiated rolls for forth set
dice2.roll();
sum = dice1.getFaceValue() + dice2.getFaceValue();
System.out.println("Die 1 is " + dice1);
System.out.println("Die 2 is " + dice2);
System.out.println("Set Total: " + sum);
System.out.println(); //spaces to keep output clean
System.out.println();
dice1.roll();//initiated rolls for fifth set
dice2.roll();
sum = dice1.getFaceValue() + dice2.getFaceValue();
System.out.println("Die 1 is " + dice1);
System.out.println("Die 2 is " + dice2);
System.out.println("Set Total: " + sum);
System.out.println(); //spaces to keep output clean
System.out.println();
dice1.roll();//initiated rolls for sixth set
dice2.roll();
sum = dice1.getFaceValue() + dice2.getFaceValue();
System.out.println("Die 1 is " + dice1);
System.out.println("Die 2 is " + dice2);
System.out.println("Set Total: " + sum);
}
}
Code:
--------------------Configuration: ProjectTwo - JDK version 1.7.0_02 <Default> - <Default>--------------------
Die 1 is 5
Die 2 is 1
Set Total: 6
Die 1 is 3
Die 2 is 4
Set Total: 7
Die 1 is 6
Die 2 is 1
Set Total: 7
Die 1 is 5
Die 2 is 2
Set Total: 7
Die 1 is 5
Die 2 is 3
Set Total: 8
Die 1 is 5
Die 2 is 5
Set Total: 10
Process completed.
Re: demonstrating 2 variables in a sequence.