Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Nov 27th, 2005, 6:19 PM   #1
ps2cho
Newbie
 
Join Date: Nov 2005
Posts: 2
Rep Power: 0 ps2cho is on a distinguished road
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
ps2cho is offline   Reply With Quote
Old Nov 27th, 2005, 7:02 PM   #2
Kilo
Expert Programmer
 
Kilo's Avatar
 
Join Date: Nov 2005
Location: In Pink Clam?
Posts: 542
Rep Power: 0 Kilo is an unknown quantity at this point
Send a message via AIM to Kilo
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
Kilo is offline   Reply With Quote
Old Nov 27th, 2005, 7:07 PM   #3
Java|Tera
Newbie
 
Join Date: Nov 2005
Posts: 14
Rep Power: 0 Java|Tera is on a distinguished road
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?
Java|Tera is offline   Reply With Quote
Old Nov 27th, 2005, 7:21 PM   #4
Kilo
Expert Programmer
 
Kilo's Avatar
 
Join Date: Nov 2005
Location: In Pink Clam?
Posts: 542
Rep Power: 0 Kilo is an unknown quantity at this point
Send a message via AIM to Kilo
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
Kilo is offline   Reply With Quote
Old Nov 27th, 2005, 7:28 PM   #5
robrosas
Newbie
 
robrosas's Avatar
 
Join Date: Nov 2005
Location: Puerto Rico
Posts: 12
Rep Power: 0 robrosas is an unknown quantity at this point
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
Start by trying this.
__________________
From P.R. to the World!! RoBeRt
robrosas is offline   Reply With Quote
Old Nov 27th, 2005, 7:31 PM   #6
groovicus
Programmer
 
Join Date: Nov 2004
Posts: 84
Rep Power: 5 groovicus is on a distinguished road
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
groovicus is offline   Reply With Quote
Old Nov 27th, 2005, 7:37 PM   #7
crawforddavid2006
Expert Programmer
 
crawforddavid2006's Avatar
 
Join Date: Apr 2005
Location: Not sure yet
Posts: 595
Rep Power: 0 crawforddavid2006 is an unknown quantity at this point
Send a message via AIM to crawforddavid2006 Send a message via MSN to crawforddavid2006
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.
crawforddavid2006 is offline   Reply With Quote
Old Nov 27th, 2005, 7:56 PM   #8
robrosas
Newbie
 
robrosas's Avatar
 
Join Date: Nov 2005
Location: Puerto Rico
Posts: 12
Rep Power: 0 robrosas is an unknown quantity at this point
...

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: ...
robrosas is offline   Reply With Quote
Old Nov 27th, 2005, 8:02 PM   #9
robrosas
Newbie
 
robrosas's Avatar
 
Join Date: Nov 2005
Location: Puerto Rico
Posts: 12
Rep Power: 0 robrosas is an unknown quantity at this point
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
robrosas is offline   Reply With Quote
Old Nov 27th, 2005, 8:34 PM   #10
ps2cho
Newbie
 
Join Date: Nov 2005
Posts: 2
Rep Power: 0 ps2cho is on a distinguished road
//***********************************************************************
// 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.
ps2cho is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 6:21 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC