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

Java Exercise-Cant find class

Posted on 2008-10-08
9
338 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
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.

 

Author Comment

by:ca1358
ID: 22675796
Can anybody explain what I am missing?
0
 

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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 62
type mismatch (Object[] to double[] 4 33
Java 8 to Java 6 8 20
junit as external jar or library 7 23
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.
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

861 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