Results 1 to 17 of 17
  1. #1
    pro85 is offline Member
    Join Date
    Apr 2008
    Posts
    16
    Rep Power
    0

    Default where is the class

    import java.awt.*;
    import java.applet.Applet;

    public class Rice extends Applet {

    public void paint(Graphics g) {
    int countOfSquares = 0;
    int riceOnThisSquare = 1;
    int totalRice = 0;
    int y = 20;
    do {
    countOfSquares++;
    g.drawString("On square "+countOfSquares+" are " +

    riceOnThisSquare, 10, y);
    totalRice = totalRice + riceOnThisSquare;
    riceOnThisSquare = riceOnThisSquare * 2;
    y = y +20;
    } while (totalRice < 100);
    g.drawString("Number of squares needed is " + countOfSquares,

    10, y + 20);
    }

    }
    it can find the class

  2. #2
    Join Date
    Aug 2008
    Location
    Saint-Petersburg, Russia
    Posts
    66
    Rep Power
    0

    Default

    which exact error you get and when you get it ( compilation time/runtime ) ?

  3. #3
    mtyoung is offline Senior Member
    Join Date
    Dec 2008
    Location
    Hong Kong
    Posts
    473
    Rep Power
    6

    Default

    i cant find any error in the code...

    what class do you want?

  4. #4
    angryboy's Avatar
    angryboy is offline Senior Member
    Join Date
    Jan 2009
    Posts
    742
    Rep Power
    6

    Default

    i'm guess you don't know what a class is?
    in bold:
    import java.awt.*;
    import java.applet.Applet;

    public class Rice extends Applet {

    public void paint(Graphics g) {
    USE CODE TAGS--> [CODE]...[/CODE]
    Get NotePad++ (free)

  5. #5
    pro85 is offline Member
    Join Date
    Apr 2008
    Posts
    16
    Rep Power
    0

    Default

    thank for all answer.

    angryboy, u r right <br>

    public class Rice extends Applet {
    the class doesnt declare yet how to correct the code

  6. #6
    OrangeDog's Avatar
    OrangeDog is offline Senior Member
    Join Date
    Jan 2009
    Location
    Cambridge, UK
    Posts
    838
    Rep Power
    6

    Default

    I am sorry. Your question is not understandable. What are you trying to do?

  7. #7
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    Quote Originally Posted by pro85 View Post
    thank for all answer.

    angryboy, u r right <br>

    public class Rice extends Applet {
    the class doesnt declare yet how to correct the code
    What you mean by class not declared. You have declared the class in your code. Seems you don't have any idea about what you are doing in that code.

    Better to read more about Java classes.

    Classes and Object : Suns' Tutorial.

  8. #8
    angryboy's Avatar
    angryboy is offline Senior Member
    Join Date
    Jan 2009
    Posts
    742
    Rep Power
    6

    Default

    i'm guessing you don't know how to declare a class?

    Rice bowl = new Rice();
    bowl.someMethod(someParam);

    you should read some intro on java. try googleling it.
    USE CODE TAGS--> [CODE]...[/CODE]
    Get NotePad++ (free)

  9. #9
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    Quote Originally Posted by angryboy View Post
    i'm guessing you don't know how to declare a class?

    Rice bowl = new Rice();
    bowl.someMethod(someParam);
    Is this declaring class? Defining an object of class and access instance.

  10. #10
    angryboy's Avatar
    angryboy is offline Senior Member
    Join Date
    Jan 2009
    Posts
    742
    Rep Power
    6

    Default

    oops! got my terminology mixed up.
    USE CODE TAGS--> [CODE]...[/CODE]
    Get NotePad++ (free)

  11. #11
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    Never mind lol. Those simple things can misses anyone. But, it's much better to check twice before click the Submit button. Newbies can confuse.

  12. #12
    pro85 is offline Member
    Join Date
    Apr 2008
    Posts
    16
    Rep Power
    0

    Default

    import java.awt.*;
    import java.applet.Applet;

    public class Rice extends Applet {

    public void paint(Graphics g) {
    int countOfSquares = 0;
    int riceOnThisSquare = 1;
    int totalRice = 0;
    int y = 20;
    do {
    countOfSquares++;
    g.drawString("On square "+countOfSquares+" are " +

    riceOnThisSquare, 10, y);
    totalRice = totalRice + riceOnThisSquare;
    riceOnThisSquare = riceOnThisSquare * 2;
    y = y +20;
    } while (totalRice < 100);
    g.drawString("Number of squares needed is " + countOfSquares,

    10, y + 20);
    }

    }

    C:>javac rice.java
    the error is shown
    rice.java:4: class Rice is public, should be declared in a file named Rice.java

    public class Rice extends Applet {
    ^
    my question is "how to correct the code"

  13. #13
    Join Date
    Aug 2008
    Location
    Saint-Petersburg, Russia
    Posts
    66
    Rep Power
    0

    Default

    Java is case-sensitive, rename your file "rice.java" => "Rice.java".

  14. #14
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    In Java class name and the file name should be same. Since your Java class name is Rice the file name must be Rice.java. Not rice.java

  15. #15
    pro85 is offline Member
    Join Date
    Apr 2008
    Posts
    16
    Rep Power
    0

    Default

    C:>javac Rice.java
    the error is shown
    Exception in thread "main" java.lang.NoSuchMethodError: main

  16. #16
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    This an applet, you cannot run it with the command java. Once you use that command VM check the main method in your Java class, not found here and throw an exception.

    Write a HTML code to execute this class file.

  17. #17
    angryboy's Avatar
    angryboy is offline Senior Member
    Join Date
    Jan 2009
    Posts
    742
    Rep Power
    6

    Default

    pro85: why don't you try compiling some simple app first like a hello world program. just so you get the feel of a console.
    USE CODE TAGS--> [CODE]...[/CODE]
    Get NotePad++ (free)

Similar Threads

  1. problem calling function from class to class
    By alin_ms in forum New To Java
    Replies: 3
    Last Post: 12-19-2008, 07:35 PM
  2. Calling a method on original class from created class
    By kpedersen in forum Advanced Java
    Replies: 4
    Last Post: 08-20-2008, 12:25 AM
  3. Replies: 0
    Last Post: 05-13-2008, 06:16 AM
  4. Class Reflection: Finding super class names
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-23-2008, 08:12 PM
  5. Replies: 0
    Last Post: 04-02-2008, 07:04 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •