Solved

What's wrong with this code?

Posted on 2004-10-14
21
210 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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…

708 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

13 Experts available now in Live!

Get 1:1 Help Now