Solved

What's wrong with this code?

Posted on 2004-10-14
21
222 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

 
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
 
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

[Webinar] Learn How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

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

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
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…
Suggested Courses

623 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