• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 228
  • Last Modified:

What's wrong with this code?

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
shoescramgoaway
Asked:
shoescramgoaway
  • 8
  • 6
  • 4
  • +2
3 Solutions
 
Tommy BraasCommented:
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
 
CEHJCommented:
>>Pair.thro;


should be

Pair.thro();
0
 
Tommy BraasCommented:
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
Tommy BraasCommented:
CEHJ,

Could you please explain why that is?

\t
0
 
CEHJCommented:
Yes, and this

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

should be

dice Pair = new dice(1,1);
0
 
shoescramgoawayAuthor Commented:
now it says "return type required" on the Dice(...) line
0
 
shoescramgoawayAuthor Commented:
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
 
shoescramgoawayAuthor Commented:
I should add, i'm looking for help understanding the code too (thats part of the question).
0
 
shoescramgoawayAuthor Commented:
it runs now that i changed all the words of "dice" to lowercase.
0
 
CEHJCommented:
>>i'm looking for help understanding the code

OK. What bit(s) don't you understand?
0
 
Tommy BraasCommented:
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
 
shoescramgoawayAuthor Commented:
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
 
shoescramgoawayAuthor Commented:
shoot, throw in "d1=n1; d2=n2;" after "dice(int n1, int n2){"    sorry
0
 
CEHJCommented:
>>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
 
shoescramgoawayAuthor Commented:
OK, that makes sense, now where is the "constructor", and how does it work?
0
 
CEHJCommented:
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
 
NaeemgCommented:
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
 
sciuriwareCommented:
This is homework guys. Keep it to advises!

;JOOP!
0
 
shoescramgoawayAuthor Commented:
The points went to the effort, even though Naeemg has the most accurate answer.  Thanks to all!
0
 
CEHJCommented:

8-)
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 8
  • 6
  • 4
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now