Link to home
Start Free TrialLog in
Avatar of ca1358
ca1358

asked on

Java Exercise-Cant find class

I am doing this exercise in Java and when I try to run it, it says no Class found and I have 2 errors.   Any help would greatly be appreciated.

package loadeddie;
import java.util.Random;

public class LoadedDie{

   public LoadedDie(int pctChance)
{    
        chanceForSix = pctChance;
        Random generator = new Random();
}  
     public int rollDie()
     {        
         int chanceForOthers;
         int randNum;
         
         chanceForOthers = (100 - chanceForSix)/5;
         randNum = generator.nextInt(100);
       
        If (randNum < chanceForSix){
             
           return 6;}
        else {
           
           
            return(randNum - chanceForSix)/ chanceForOthers;}
       
     }
   
        private int chanceForSix;
        private Random generator;
}
Avatar of Kevin Cross
Kevin Cross
Flag of United States of America image

How are you trying to run this?  There is no static main method, so can't execute directly.  You will have to instantiate from another object or create a main method.

Hope that helps, please post back if you need more assistance after giving that a try.

Regards,
Kevin
Avatar of ca1358
ca1358

ASKER

Sorry, I am Lost.  

Running in  NetBeans IDE 6.1.

This is what the result at the bottom says:

init:
deps-jar:
Compiling 1 source file to C:\Documents and Settings\Angela\My Documents\NetBeansProjects\LoadedDie\build\classes
C:\Documents and Settings\Angela\My Documents\NetBeansProjects\LoadedDie\src\loadeddie\LoadedDie.java:19: ';' expected
         If (randNum < chanceForSix){            
C:\Documents and Settings\Angela\My Documents\NetBeansProjects\LoadedDie\src\loadeddie\LoadedDie.java:21: 'else' without 'if'
        else {                      
2 errors
BUILD FAILED (total time: 0 seconds)

Well, java is case sensitive, so If is not equivalent to if.  
Change this: If (randNum < chanceForSix)
To this: if (randNum < chanceForSix)
Avatar of ca1358

ASKER

Can anybody explain what I am missing?
Avatar of ca1358

ASKER

That fix the compile errors
init:
deps-jar:
Compiling 1 source file to C:\Documents and Settings\Angela\My Documents\NetBeansProjects\LoadedDie\build\classes
compile-single:
BUILD SUCCESSFUL (total time: 0 seconds)

Can you explain what "<No main classes found>" means?
See my first comment as I thought that was were you were at with errors. :) As the error suggests you don't have a main method.

If you need more / deeper explaination you can refer to http://java.sun.com/docs/books/tutorial/ or post back after trying to implement based on my feedback.
Avatar of ca1358

ASKER

I found this piece of code, Can I change this to fit my code?


package loadeddie;
import java.util.Random;

public class LoadedDieTester {
public static void main(String[] args)
    {
        Random myDie = new Random ();
System.out.println("The Result of the toss is " + (myDie.nextInt(6)+1));

    }
}
Think of this in object oriented terms.  You have created a dice object that is now sitting on the table.  It will never roll unless you have a Player object or something that rolls it.
ASKER CERTIFIED SOLUTION
Avatar of Kevin Cross
Kevin Cross
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial