Solved

Java Exercise-Cant find class

Posted on 2008-10-08
9
336 Views
Last Modified: 2012-05-05
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;
}
0
Comment
Question by:ca1358
  • 5
  • 4
9 Comments
 
LVL 59

Expert Comment

by:Kevin Cross
ID: 22675439
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
0
 

Author Comment

by:ca1358
ID: 22675476
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)

0
 
LVL 59

Expert Comment

by:Kevin Cross
ID: 22675793
Well, java is case sensitive, so If is not equivalent to if.  
Change this: If (randNum < chanceForSix)
To this: if (randNum < chanceForSix)
0
 

Author Comment

by:ca1358
ID: 22675796
Can anybody explain what I am missing?
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:ca1358
ID: 22675808
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?
0
 
LVL 59

Expert Comment

by:Kevin Cross
ID: 22675827
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.
0
 

Author Comment

by:ca1358
ID: 22675829
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));

    }
}
0
 
LVL 59

Expert Comment

by:Kevin Cross
ID: 22675847
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.
0
 
LVL 59

Accepted Solution

by:
Kevin Cross earned 500 total points
ID: 22675861
Yes, you now have a Player object.

Now that player object needs to know about your Die object to roll it.  Instead of instantiating "Random myDie = new Random();" try instantiating your class LoadedDie then call your roll method.

Hopefully that helps.

And hopefully being clear enough.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Modeling a class in java 5 47
Java Loop 6 48
Java SE 8u111  Lot of stuff broke 11 54
reverse digits of a number using for loop 5 36
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
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 will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

912 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

24 Experts available now in Live!

Get 1:1 Help Now