Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   Java (http://www.programmingforums.org/forum17.html)
-   -   What is wrong with this code? (http://www.programmingforums.org/showthread.php?t=7230)

ps2cho Nov 27th, 2005 5:19 PM

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

Kilo Nov 27th, 2005 6:02 PM

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

Java|Tera Nov 27th, 2005 6:07 PM

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?

Kilo Nov 27th, 2005 6:21 PM

its not obvious what he is trying to do, i think it is. anyway his problem is he has errors lol and wants help.

robrosas Nov 27th, 2005 6:28 PM

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! :rolleyes:
:

  public class BoxCars {
public static void main (String [] args) {
//code
//code
//code
.
.
.
.
//code
} //end of BoxCars class

Start by trying this.

groovicus Nov 27th, 2005 6:31 PM

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

crawforddavid2006 Nov 27th, 2005 6:37 PM

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.
:D
The reason I ask if you are in that class is because the cs1.Keyboard is something that is commonly used in that course.

robrosas Nov 27th, 2005 6:56 PM

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


robrosas Nov 27th, 2005 7:02 PM

That code rolls both dices 1000 times and counts how many times each side appears! Get a clue from it!

ps2cho Nov 27th, 2005 7:34 PM

:

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


All times are GMT -5. The time now is 9:40 PM.

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