![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Nov 2005
Posts: 2
Rep Power: 0
![]() |
What is wrong with this code?
[code]//***********************************************************************
// xxxxx Project 4-4 11/26/05 // Design and implement a class called PairOfDice, with two six-sided // Die objects. Create a driver class called BoxCars with a main method // that rolls a PairOfDice object 1000 time, counting the number of box // cars (two sixes) that occur. //*********************************************************************** import cs1.Keyboard; import java.util.Random; import java.lang.Math; public class P4_4 { int sides = 6; // Creates the 2 die objects + and rolls them. Die Dice1 = new Die(sides); Die Dice2 = new Die(sides); Dice1.roll(); Dice2.roll(); System.out.println("You rolled a " +die1 +die2); // rolls the dice. public void roll() { DiceRoll = (int) (Math.random()*NUM_SIDES) + 1; } }[code] It keeps saying <identifier> expected on the Dice1.roll(); and the next 2 lines ARGHH. Im new to java and i dont understand what im doing wrong |
|
|
|
|
|
#2 |
|
Expert Programmer
|
im not a java programmer, but your listing the function after the main and with no prototype... i know in C++ this would conflict with the compiler.
EDIT: Hence, <identifier> expected
__________________
"When in Rome, Do as the Romans Do" "Beauty is in the eye of the BEER holder" "Save your breath your going to need it for your blow up doll later" SearchLores.org |
|
|
|
|
|
#3 |
|
Newbie
Join Date: Nov 2005
Posts: 14
Rep Power: 0
![]() |
You are calling methods in a class. The Dice1.roll(); is being called, yet you have no method called Dice1.roll or Dice2.roll...What exactly are you trying to do with this program?
|
|
|
|
|
|
#4 |
|
Expert Programmer
|
its not obvious what he is trying to do, i think it is. anyway his problem is he has errors lol and wants help.
__________________
"When in Rome, Do as the Romans Do" "Beauty is in the eye of the BEER holder" "Save your breath your going to need it for your blow up doll later" SearchLores.org |
|
|
|
|
|
#5 |
|
Newbie
Join Date: Nov 2005
Location: Puerto Rico
Posts: 12
Rep Power: 0
![]() |
Let's See...
First what is Die? A class you already implemented? .... And as the instructions say....You need a main method to run your class !! You need to build the PairO fDice class and the BoxCars class that will hold the main method!
![]() public class BoxCars {
public static void main (String [] args) {
//code
//code
//code
.
.
.
.
//code
} //end of BoxCars class
__________________
From P.R. to the World!! RoBeRt |
|
|
|
|
|
#6 |
|
Programmer
Join Date: Nov 2004
Posts: 84
Rep Power: 5
![]() |
It looks like you are having quite a few problems, so where to start first?
![]() You have no main method.... DiceRoll has no type associated with it..... You have no real constructor for your class.... die1 and die2 have no types associated with them..... NUM_SIDES is not initialized to anything..... It would be really helpful to see the all of your code, otherwise we are just making assumptions about what you do and do not know how to do. ![]()
__________________
HijackThis Team-SFDC |
|
|
|
|
|
#7 |
|
Expert Programmer
|
First of all code tages are used like html tags u have to have a[*] and a [/*] ( "*" being the word code or what ever it is) so then it looks like this
it looks a lot better this way. also would you happen to be taking A.P. Computer Science with Mr. Hillestead? If you are then just talk to me tommorow. ![]() The reason I ask if you are in that class is because the cs1.Keyboard is something that is commonly used in that course. |
|
|
|
|
|
#8 |
|
Newbie
Join Date: Nov 2005
Location: Puerto Rico
Posts: 12
Rep Power: 0
![]() |
...
Try this on your main class
public class BoxCars{
public static void main(String[] args) {
PairOfDice Dice1 = new PairOfDice();
PairOfDice Dice2 = new PairOfDice();
int result1=0,resullt2=0;
int one=0,two=0,three=0,four=0,five=0,six=0;
for (int i=0;i<1000;i++){
result1=Dice1.roll();
result2=Dice2.roll();
switch (result1){
case 1:
one++;
break;
case 2:
two++;
break;
case 3:
three++;
break;
case 4:
four++;
case 5:
five++;
break;
case 6:
six++;
break;
default:
System.out.println("Not a valid Dice");
break;
}
switch (result2){
case 1:
one++;
break;
case 2:
two++;
break;
case 3:
three++;
break;
case 4:
four++;
case 5:
five++;
break;
case 6:
six++;
break;
default:
System.out.println("Not a valid Dice");
break;
}
}
}
__________________
From P.R. to the World!! RoBeRt Last edited by robrosas; Nov 27th, 2005 at 8:06 PM. Reason: ... |
|
|
|
|
|
#9 |
|
Newbie
Join Date: Nov 2005
Location: Puerto Rico
Posts: 12
Rep Power: 0
![]() |
That code rolls both dices 1000 times and counts how many times each side appears! Get a clue from it!
__________________
From P.R. to the World!! RoBeRt |
|
|
|
|
|
#10 |
|
Newbie
Join Date: Nov 2005
Posts: 2
Rep Power: 0
![]() |
//***********************************************************************
// xxxxx Project 4-4 11/26/05
// Design and implement a class called PairOfDice, with two six-sided
// Die objects. Create a driver class called BoxCars with a main method
// that rolls a PairOfDice object 1000 time, counting the number of box
// cars (two sixes) that occur.
//***********************************************************************
import cs1.Keyboard;
import java.util.Random;
import java.lang.Math;
public class P4_4
{
// Creates the 2 die objects
class PairOfDice
{
int sides = 6;
PairOfDice Dice1 = new PairOfDice(sides);
PairOfDice Dice2 = new PairOfDice(sides);
}
// rolls the dice.
public void roll()
{
DiceRoll = (int) (Math.random()*sides) + 1;
}
class BoxCars
{
public static void main(String[] args)
{
int result1=0,resullt2=0;
int count = 0;
for (int i=0;i<1000;i++)
{
result1=Dice1.roll();
result2=Dice2.roll();
if(result1=6 && result2=6)
count++;
}
System.out.println("The total number of double sixes are: "+count);
}
}
}Ok thats what i have now. Im so confused. I used your code above, but it only asks for when two 6's are rolled. these are the errors i'm getting: cannot resolve symbol
symbol : constructor PairOfDice (int)
cannot resolve symbol
symbol : variable DiceRoll
cannot resolve symbol
symbol : variable sides
cannot resolve symbol
symbol : variable Dice1
cannot resolve symbol
symbol : variable result2
cannot resolve symbol
symbol : variable Dice2
incompatible types
found : int
required: boolean
if(result1=6 && result2=6)
^
cannot resolve symbol
symbol : variable result2
if(result1=6 && result2=6)
^
inner classes cannot have static declarations
public static void main(String[] args)
^
10 errors
Process completed.Im about to just give up as im completely fried. I dont understand this and its so frustrating. |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|