Results 1 to 12 of 12
  1. #1
    asmodean is offline Member
    Join Date
    Jul 2010
    Posts
    18
    Rep Power
    0

    Default Dice class for java course

    My instructor wants us to write a dice class that will roll a die, return the value rolled and return a representation of a die face as a String object. Now, none of that is a problem. The problem for me is that she wants this class to do all of that using her pre-written program that has no variables that will hold String data.

    Am I missing something? Is there some way to make that program display that String data even though there are only integer variables in the program? We're not allowed to modify her program so I'm lost. Can anyone clue me in? Thanks.

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

    Default

    return the value rolled and return a representation of a die face as a String object
    That would say that the method must create a String using one of its constructors or that of another class that takes the data type you are using and returns a String.

    What should the "representation of a die face" look like?

    way to make that program display that String data
    Not sure what you are asking here.
    Last edited by Norm; 07-14-2010 at 01:53 PM.

  3. #3
    Singing Boyo is offline Senior Member
    Join Date
    Mar 2009
    Posts
    552
    Rep Power
    6

    Default

    Quote Originally Posted by asmodean View Post
    The problem for me is that she wants this class to do all of that using her pre-written program that has no variables that will hold String data.
    You must have code for said program. Can we see it? It would give us a much clearer idea of what you are having trouble with.
    If the above doesn't make sense to you, ignore it, but remember it - might be useful!
    And if you just randomly taught yourself to program, well... you're just like me!

  4. #4
    chimanet is offline Member
    Join Date
    Jun 2010
    Posts
    5
    Rep Power
    0

    Default

    i assume the return type of the result of the roll to be int so u can use it toString method

  5. #5
    asmodean is offline Member
    Join Date
    Jul 2010
    Posts
    18
    Rep Power
    0

    Default

    Quote Originally Posted by Norm View Post
    That would say that the method must create a String using one of its constructors or that of another class that takes the data type you are using and returns a String.

    What should the "representation of a die face" look like?
    switch (roll)
    {
    case 1:
    {
    dieFace = "one\n\n *\n\n\n";
    break;
    }
    case 2:
    {
    dieFace = "two\n*\n\n *\n\n";
    break;
    }
    case 3:
    {
    dieFace = "three\n*\n *\n *\n\n";
    break;
    }
    case 4:
    {
    dieFace = "four\n* *\n\n* *\n\n";
    break;
    }
    case 5:
    {
    dieFace = "five\n* *\n *\n* *\n\n";
    break;
    }
    case 6:
    {
    dieFace = "six\n* *\n* *\n* *\n\n";
    }
    }

    but here is the program that she wrote for us to create a class for:

    import java.util.Scanner;

    public class *********
    {
    public static void main (String[] args)
    {
    int rolls, roll1, roll2, numberRolls, sevenRolled = 0;
    Scanner input = new Scanner(System.in);

    Dice die1 = new Dice();
    Dice die2 = new Dice();

    System.out.print ("Enter the number of dice rolls: ");
    noRolls = input.nextInt();

    for (rolls = 1; rolls <= numberRolls; rolls++)
    {
    System.out.println("Roll #: " + rolls);
    roll1 = die1.diceRoll();
    roll2 = die2.diceRoll();


    if (roll1 == 2 && roll2 == 5 || roll1 == 5 && roll2 == 2 || roll1 == 1 && roll2 == 6 || roll6 == 6 && roll2 == 1 || roll1 == 3 && roll2 == 4 || roll1 == 4 && roll2 == 3)
    sevenRolled++;
    }

    System.out.println ("Number of Dice Rolls: " + numberRolls);
    System.out.println ("Number of Times Seven Was Rolled: " + sevenRolled);


    }
    }


    So, this program isn't meant to display die faces. I was just wondering if there was some magical way to make it do something that isn't in the code or if the instructor made a mistake or what.

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

    Default

    if (roll1 == 2 && roll2 == 5 || roll1 == 5 && roll2 == 2 || roll1 == 1 && roll2 == 6 || roll6 == 6 && roll2 == 1 || roll1 == 3 && roll2 == 4 || roll1 == 4 && roll2 == 3)
    Seems a bit awkward. What if you add roll1 to roll2 and test if the results is == to 7

    I've lost what your problem/question is now.

  7. #7
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default

    what in the program makes the dice roll AND what format is said dice roll entering the program as e.g. is (dice roll of one coming into your program as an integer or a String) and can we see the tutors original question please.

  8. #8
    asmodean is offline Member
    Join Date
    Jul 2010
    Posts
    18
    Rep Power
    0

    Default

    Quote Originally Posted by al_Marshy_1981 View Post
    what in the program makes the dice roll AND what format is said dice roll entering the program as e.g. is (dice roll of one coming into your program as an integer or a String) and can we see the tutors original question please.

    nothing in the program makes the dice roll, its all done in the class. The roll is returned as an integer. It has to be right? There's nothing to hold string data in the program right?

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

    Default

    How are you coming with your problem?

  10. #10
    asmodean is offline Member
    Join Date
    Jul 2010
    Posts
    18
    Rep Power
    0

    Default

    Well, so far I just wrote methods that did what the instructor asked for, even though the program he wrote for us doesn't use all of them.

  11. #11
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default

    Quote Originally Posted by asmodean View Post
    nothing in the program makes the dice roll, its all done in the class. The roll is returned as an integer. It has to be right? There's nothing to hold string data in the program right?
    I could not tell you from what you have posted only assume you are right.

    but anyway a String representation of int one might be o, a String of int two could be oo, etc. use


    Java Code:
    dieFace = "o"
    for example in case 1, thats what it seems the instructor wants but without more information on the question its hard to tell.

  12. #12
    asmodean is offline Member
    Join Date
    Jul 2010
    Posts
    18
    Rep Power
    0

    Default

    Quote Originally Posted by al_Marshy_1981 View Post
    I could not tell you from what you have posted only assume you are right.
    I posted the entire program letter for letter the way it was given to me.

    Quote Originally Posted by al_Marshy_1981 View Post
    but anyway a String representation of int one might be o, a String of int two could be oo, etc. use


    Java Code:
    dieFace = "o"
    for example in case 1, thats what it seems the instructor wants but without more information on the question its hard to tell.
    I posted what the instructor wants in the die face representations in the case statement I posted earlier. The same post that has the instructor's program class.

Similar Threads

  1. Java rolling dice
    By fifa4ever in forum Java Applets
    Replies: 18
    Last Post: 06-10-2010, 08:43 PM
  2. Java Program (rolling the dice)
    By genocist in forum New To Java
    Replies: 8
    Last Post: 03-01-2010, 03:15 PM
  3. Dice game issue- any Java gamblers ableto help?:P
    By mambalamba in forum New To Java
    Replies: 2
    Last Post: 12-17-2009, 06:49 PM
  4. Roll dice class with three dices
    By nube07 in forum New To Java
    Replies: 4
    Last Post: 07-14-2008, 01:37 AM
  5. java help please- two sided dice
    By pat8 in forum Advanced Java
    Replies: 3
    Last Post: 07-01-2008, 02:25 AM

Posting Permissions

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