Solved

What's wrong with this code?

Posted on 2004-10-14
21
212 Views
Last Modified: 2010-03-31
import java.util.*;
import java.math.*;

class dice{
int d1, d2;
Random r;

Dice(int n1, int n2){
d1=n1; d2=n2;
r = new Random();
}

void thro(){
d1 = 1 + r.nextInt(6);
d2 = 1 + r.nextInt(6);
}

public static void main (String[] args){
Dice Pair = new dice(1,1);
Pair.thro;
System.out.println(Pair.d1 + "  " + Pair.d2);
}

}



My professor gave me that code and it's not working.  I have no idea how to decipher it.  When answering please keep in mind that i'm in my third week of class and never programmed before.  Any help would be GREATLY appreciated.

Thanks!
0
Comment
Question by:shoescramgoaway
  • 8
  • 6
  • 4
  • +2
21 Comments
 
LVL 14

Assisted Solution

by:Tommy Braas
Tommy Braas earned 50 total points
ID: 12311209
Hi shoescramgoaway,

Look at the JDK documentation available here: http://java.sun.com/j2se/1.4.2/docs/api/index.html
You might want to have a look at the Random class in particular.

Cheers!

\tt
0
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 50 total points
ID: 12311222
>>Pair.thro;


should be

Pair.thro();
0
 
LVL 14

Expert Comment

by:Tommy Braas
ID: 12311224
shoescramgoaway,

There are some rules for programming in Java. For instance, Java is case sensitive, thus very particular about the case of class/method/variable names.

What is the name of the class in your code? Is it Dice or dice?

\t
0
 
LVL 14

Expert Comment

by:Tommy Braas
ID: 12311238
CEHJ,

Could you please explain why that is?

\t
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12311245
Yes, and this

>>Dice Pair = new dice(1,1);

should be

dice Pair = new dice(1,1);
0
 

Author Comment

by:shoescramgoaway
ID: 12311249
now it says "return type required" on the Dice(...) line
0
 

Author Comment

by:shoescramgoaway
ID: 12311260
whats the difference between Dice and dice in this?  I understand it is case sensitive, i just don't understand exactly what is happening here.
0
 

Author Comment

by:shoescramgoaway
ID: 12311268
I should add, i'm looking for help understanding the code too (thats part of the question).
0
 

Author Comment

by:shoescramgoaway
ID: 12311281
it runs now that i changed all the words of "dice" to lowercase.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12311296
>>i'm looking for help understanding the code

OK. What bit(s) don't you understand?
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 14

Expert Comment

by:Tommy Braas
ID: 12311343
shoescramgoaway,
> whats the difference between Dice and dice in this?  I understand it
> is case sensitive, i just don't understand exactly what is happening here.

The difference is the case if the first letter. As far as Java is concerned, it might as well have said:
Giant g = new midget();

I.e. two completely different classes.

When you declare a variable the type declared for that variable must be present. In your case the type of the variable was Dice, however there existed no class Dice. The class in existence is dice.
0
 

Author Comment

by:shoescramgoaway
ID: 12311389
For the sake of confusion, i'll retype the corrected code:  (I think its right)

import java.util.*;
import java.math.*;

class dice{    //define a class called "dice".
int d1, d2;    //declare two variables as integers, d1 and d2.
Random r;   //declare r as a type Random

dice(int n1, int n2){
r = new Random();  // (need help with the terminology for this line)
}

void thro(){  //define a method called thro that picks two new numbers at random, d1 and d2.
d1 = 1 + r.nextInt(6);
d2 = 1 + r.nextInt(6);
}

public static void main (String[] args){  //declare main method
dice Pair = new dice(1,1);   //help!
Pair.thro();  //help!
System.out.println(Pair.d1 + "  " + Pair.d2);  //print output to the screen.
}
}


0
 

Author Comment

by:shoescramgoaway
ID: 12311408
shoot, throw in "d1=n1; d2=n2;" after "dice(int n1, int n2){"    sorry
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12311422
>>r = new Random();  

Create a Random object that can return random values in various ways. (see api docs)

>>dice Pair = new dice(1,1);   //help!

Create a pair of dice, passing default 'pre-throw' values  of 1 and 1


>>Pair.thro();  //help!

Throw the dice (this generates random numbers)
0
 

Author Comment

by:shoescramgoaway
ID: 12311460
OK, that makes sense, now where is the "constructor", and how does it work?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12311535
This is the constructor. It creates an instance of the dice class along with its dependent objects:

dice(int n1, int n2){
      d1=n1;
      d2=n2;
      r = new Random();
}
0
 
LVL 5

Accepted Solution

by:
Naeemg earned 25 total points
ID: 12316351
Here is ur correct code working smoothly. Try it and Tell me.


import java.util.*;
import java.math.*;

class Dice
{ //define a class called "dice".
  int d1, d2; //declare two variables as integers, d1 and d2.
  Random r; //declare r as a type Random

  Dice(int n1, int n2)
  {
    r = new Random(); // (need help with the terminology for this line)
  }

  void thro()
  { //define a method called thro that picks two new numbers at random, d1 and d2.
    d1 = 1 + r.nextInt(6);
    d2 = 1 + r.nextInt(6);
  }

  public static void main(String[] args)
  { //declare main method
    Dice Pair = new Dice(1, 1); //help!
    Pair.thro(); //help!
    System.out.println(Pair.d1 + "  " + Pair.d2); //print output to the screen.
  }
}

//Naeem Shehzad Ghuman
0
 
LVL 24

Expert Comment

by:sciuriware
ID: 12316506
This is homework guys. Keep it to advises!

;JOOP!
0
 

Author Comment

by:shoescramgoaway
ID: 12335714
The points went to the effort, even though Naeemg has the most accurate answer.  Thanks to all!
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12339156

8-)
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now