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
Solved

What's wrong with this code?

Posted on 2004-10-14
21
215 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
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.

 
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
difference of if loops 23 60
arguments to jar 5 33
login form jsp example 2 35
maven disable workspace resolution 1 16
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

791 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