Results 1 to 12 of 12
Thread: Dice class for java course
- 07-14-2010, 08:41 AM #1
Member
- Join Date
- Jul 2010
- Posts
- 18
- Rep Power
- 0
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.
- 07-14-2010, 01:15 PM #2
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.return the value rolled and return a representation of a die face as a String object
What should the "representation of a die face" look like?
Not sure what you are asking here.way to make that program display that String dataLast edited by Norm; 07-14-2010 at 01:53 PM.
- 07-14-2010, 06:42 PM #3
Senior Member
- Join Date
- Mar 2009
- Posts
- 552
- Rep Power
- 5
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!
- 07-14-2010, 11:19 PM #4
Member
- Join Date
- Jun 2010
- Posts
- 5
- Rep Power
- 0
i assume the return type of the result of the roll to be int so u can use it toString method
- 07-15-2010, 12:00 AM #5
Member
- Join Date
- Jul 2010
- Posts
- 18
- Rep Power
- 0
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.
- 07-15-2010, 01:53 AM #6
Seems a bit awkward. What if you add roll1 to roll2 and test if the results is == to 7if (roll1 == 2 && roll2 == 5 || roll1 == 5 && roll2 == 2 || roll1 == 1 && roll2 == 6 || roll6 == 6 && roll2 == 1 || roll1 == 3 && roll2 == 4 || roll1 == 4 && roll2 == 3)
I've lost what your problem/question is now.
- 07-15-2010, 04:20 AM #7
Senior Member
- Join Date
- Feb 2010
- Location
- Waterford, Ireland
- Posts
- 748
- Rep Power
- 4
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.
- 07-16-2010, 02:26 AM #8
Member
- Join Date
- Jul 2010
- Posts
- 18
- Rep Power
- 0
- 07-16-2010, 02:31 AM #9
How are you coming with your problem?
- 07-16-2010, 02:38 AM #10
Member
- Join Date
- Jul 2010
- Posts
- 18
- Rep Power
- 0
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.
- 07-17-2010, 02:11 AM #11
Senior Member
- Join Date
- Feb 2010
- Location
- Waterford, Ireland
- Posts
- 748
- Rep Power
- 4
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
for example in case 1, thats what it seems the instructor wants but without more information on the question its hard to tell.Java Code:dieFace = "o"
- 07-17-2010, 03:40 AM #12
Member
- Join Date
- Jul 2010
- Posts
- 18
- Rep Power
- 0
I posted the entire program letter for letter the way it was given to me.
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
-
Java rolling dice
By fifa4ever in forum Java AppletsReplies: 18Last Post: 06-10-2010, 08:43 PM -
Java Program (rolling the dice)
By genocist in forum New To JavaReplies: 8Last Post: 03-01-2010, 03:15 PM -
Dice game issue- any Java gamblers ableto help?:P
By mambalamba in forum New To JavaReplies: 2Last Post: 12-17-2009, 06:49 PM -
Roll dice class with three dices
By nube07 in forum New To JavaReplies: 4Last Post: 07-14-2008, 01:37 AM -
java help please- two sided dice
By pat8 in forum Advanced JavaReplies: 3Last Post: 07-01-2008, 02:25 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks