Solved

Java Exercise-Cant find class

Posted on 2008-10-08
9
335 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Can anybody explain what I am missing?
0
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 

Author Comment

by:ca1358
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Understanding websocket example in spring 1 41
pairs challenge 5 44
JAVA part two 5 39
maven example error 3 34
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 …
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

762 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

12 Experts available now in Live!

Get 1:1 Help Now