Results 1 to 18 of 18
  1. #1
    igmikey is offline Member
    Join Date
    Mar 2012
    Posts
    9
    Rep Power
    0

    Default 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.

    Java 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;
       }
    }

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,755
    Rep Power
    19

    Default 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.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    skynet357 is offline Member
    Join Date
    Mar 2012
    Location
    Hyderabad, India
    Posts
    8
    Rep Power
    0

    Default 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 ;)

  4. #4
    igmikey is offline Member
    Join Date
    Mar 2012
    Posts
    9
    Rep Power
    0

    Default 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?

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,274
    Rep Power
    25

    Default 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.

  6. #6
    igmikey is offline Member
    Join Date
    Mar 2012
    Posts
    9
    Rep Power
    0

    Default 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?

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,274
    Rep Power
    25

    Default 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.

  8. #8
    igmikey is offline Member
    Join Date
    Mar 2012
    Posts
    9
    Rep Power
    0

    Default 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

  9. #9
    igmikey is offline Member
    Join Date
    Mar 2012
    Posts
    9
    Rep Power
    0

    Default 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

    --------------------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.

    Java 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;
       }
    }
    Last edited by igmikey; 03-10-2012 at 11:40 AM.

  10. #10
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,274
    Rep Power
    25

    Default 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?

  11. #11
    igmikey is offline Member
    Join Date
    Mar 2012
    Posts
    9
    Rep Power
    0

    Default 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.

  12. #12
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,274
    Rep Power
    25

    Default Re: demonstrating 2 variables in a sequence.

    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

  13. #13
    igmikey is offline Member
    Join Date
    Mar 2012
    Posts
    9
    Rep Power
    0

    Default 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];

  14. #14
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,274
    Rep Power
    25

    Default 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();

  15. #15
    igmikey is offline Member
    Join Date
    Mar 2012
    Posts
    9
    Rep Power
    0

    Default 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



    Java 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;
       }
    }

  16. #16
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,274
    Rep Power
    25

    Default 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.

  17. #17
    igmikey is offline Member
    Join Date
    Mar 2012
    Posts
    9
    Rep Power
    0

    Default 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


    Java 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

    Java 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);
    		}
    }

    Java 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.

  18. #18
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,274
    Rep Power
    25

    Default Re: demonstrating 2 variables in a sequence.

    Glad you got it working.

Similar Threads

  1. Linked list sequence and array sequence
    By Predz in forum New To Java
    Replies: 1
    Last Post: 12-31-2009, 01:30 AM
  2. Demonstrating the MouseWheelListener
    By Java Tip in forum java.awt
    Replies: 0
    Last Post: 04-23-2008, 08:26 PM
  3. Demonstrating the MenuListener
    By Java Tip in forum java.awt
    Replies: 0
    Last Post: 04-23-2008, 08:25 PM
  4. Demonstrating the ItemListener
    By Java Tip in forum java.awt
    Replies: 0
    Last Post: 04-23-2008, 08:25 PM
  5. Demonstrating the InternalFrameListener
    By Java Tip in forum javax.swing
    Replies: 0
    Last Post: 04-23-2008, 08:24 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •