Java Class definition errors--Pls HELP

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • rimizin
    New Member
    • Feb 2008
    • 2

    Java Class definition errors--Pls HELP

    Hi Guys,

    This is the first time am doing java and i have been struggling to find out whats wrong with this program....when ever i try running it ...it says Java Class Definition error

    public class Ex0603
    {
    public static void main(String[] args)
    {
    System.out.prin tln("Circles");
    showCircle(1.5) ;
    showCircle(2);
    showCircle(5.7) ;
    showCircle(7);
    showCircle(25);
    }

    static void showCircle(doub le r)
    {
    Circle c = new Circle(r);
    String str = "Radius: " + c.getRadius() + "\t"
    + "Area: " + c.getArea() + "\t"
    + "Circumfere nce: " + c.getCircumfere nce();
    System.out.prin tln(str);
    }
    }
    class Circle

    {
    double radius1;

    Circle(double r)
    {

    radius1=r;
    }

    double getRadius()
    {
    double rad;
    return(rad);
    }


    double getArea()
    {
    double Area;
    Area=Math.PI*ra dius1*radius1;
    return(Area);
    }

    double getCircumferenc e()
    {

    double Circumference,r adius;
    Circumference=2 * Math.PI * radius1;
    return(Circumfe rence);
    }

    }
  • sukatoa
    Contributor
    • Nov 2007
    • 539

    #2
    you forgot bro....

    If your main class is named Ex0603, you should save it as Ex0603.java

    And @ this method below,

    double getRadius(){
    double rad;
    return(rad);
    }

    The JVM won't allow to compile because of the rad, that have not initialized...

    If the JVM will allow this, any byte value from the general register DX or Data Register that the system used will be temporarily store at rad...

    Then you may experience wrong flow to your code...if it happens

    I've test your code and the "double rad" has only the problem....


    Correct me if im wrong,
    Sukatoa (Shadow Shaman)
    Last edited by sukatoa; Feb 24 '08, 04:20 AM. Reason: Add

    Comment

    • rimizin
      New Member
      • Feb 2008
      • 2

      #3
      Hi, I am zeeni.......... .thanks for ur post..........i tried making the changes below........wh at did u mean by initializing... ..u mean i should write double ro=0;? can u check if my code below is right? Also i noticed a strange thing while compiling....de spite i changed the code from int to double radius1 ...my compiler shows Error int radius1........ ......why is it not updating the compilation?


      class Circle

      {
      double radius1;

      Circle(double rad)
      {
      radius1=rad;
      }

      double getRadius()
      {
      double ro;
      ro= input.nextdoubl e(); //Read radius from user
      radius1=ro;
      return(radius1) ;
      }

      double getArea()
      { double area;
      area=Math.PI*ra dius1*radius1;
      return(area);
      }

      double getCircumferenc e()
      { double circumference;
      circumference=2 * Math.PI * radius1;
      return(circumfe rence);
      }

      }

      Comment

      • sukatoa
        Contributor
        • Nov 2007
        • 539

        #4
        Originally posted by rimizin
        Hi, I am zeeni.......... .thanks for ur post..........i tried making the changes below........wh at did u mean by initializing... ..u mean i should write double ro=0;? can u check if my code below is right? Also i noticed a strange thing while compiling....de spite i changed the code from int to double radius1 ...my compiler shows Error int radius1........ ......why is it not updating the compilation?


        class Circle

        {
        double radius1;

        Circle(double rad)
        {
        radius1=rad;
        }

        double getRadius()
        {
        double ro;
        ro= input.nextdoubl e(); //Read radius from user
        radius1=ro;
        return(radius1) ;
        }

        double getArea()
        { double area;
        area=Math.PI*ra dius1*radius1;
        return(area);
        }

        double getCircumferenc e()
        { double circumference;
        circumference=2 * Math.PI * radius1;
        return(circumfe rence);
        }

        }

        I mean,

        double getRadius()
        {
        double rad;
        return(rad);
        }

        If i will call the getRadius() method, thus it returns a real value? what value?
        About my previous post, i try it to refer to assembly, Sorry if you couldn't understand some phrase there...

        But here is the logic,

        If your house is getRadius() and you and your family's surname is rad,

        I would like to call you but i don't know your name, just the surname shouting outside your house, rad?!! rad?!! would your parents go outside to find out who's calling? is it for you? is it for your parents?

        They don't really know because i call you rad, since that is you surname(Assumin g), so, i must call your name just to catch only your attention and not disturbing the others...

        In assembly, we must put every data in a stack in every mov, because, The system always uses those registers priority to there processes...

        If JVM let this allow, probably the rad might initialized any of the data being stored in a stack... for example, the system stores a hexadecimal value 0AFF,

        and your code needs only a 5 double value, then the flow of your program is incorrect, according to your code, wondering why is it happening...

        for example,

        int value = double value + int value;

        is it correct? JVM doesn't allow this because the range of int is much smaller that a double value... i can't remind, JoshaH knows this...
        if this will be allowed, the result value at int value will receive a wrong format of bit...

        Comment

        • JosAH
          Recognized Expert MVP
          • Mar 2007
          • 11453

          #5
          Originally posted by sukatoa
          int value = double value + int value;

          is it correct? JVM doesn't allow this because the range of int is much smaller that a double value... i can't remind, JoshaH knows this...
          if this will be allowed, the result value at int value will receive a wrong format of bit...
          I just know where to look it up: in the Java Language Specification:

          Originally posted by JLS
          A narrowing conversion of a floating-point number to an integral type T takes two steps:


          In the first step, the floating-point number is converted either to a long, if T is long, or to an int, if T is byte, short, char, or int, as follows:
          If the floating-point number is NaN (§4.2.3), the result of the first step of the conversion is an int or long 0.
          Otherwise, if the floating-point number is not an infinity, the floating-point value is rounded to an integer value V, rounding toward zero using IEEE 754 round-toward-zero mode (§4.2.3). Then there are two cases:
          If T is long, and this integer value can be represented as a long, then the result of the first step is the long value V.
          Otherwise, if this integer value can be represented as an int, then the result of the first step is the int value V.
          Otherwise, one of the following two cases must be true:
          The value must be too small (a negative value of large magnitude or negative infinity), and the result of the first step is the smallest representable value of type int or long.
          The value must be too large (a positive value of large magnitude or positive infinity), and the result of the first step is the largest representable value of type int or long.
          In the second step:
          If T is int or long,the result of the conversion is the result of the first step.
          If T is byte, char, or short, the result of the conversion is the result of a narrowing conversion to type T (§5.1.3) of the result of the first step.
          Casting a double to a more narrow int is allowed but Java wants you to tell the
          compiler explicitly about your intentions; you do that by applying the 'cast' operator:

          [code=java]
          int value = (int)(double_va lue + int_value);
          [/code]

          kind regards,

          Jos

          Comment

          • r035198x
            MVP
            • Sep 2006
            • 13225

            #6
            Just reminding everyone to use code tags when posting code like Jos did above.
            Posting code without using code tags actually violates the site's guidelines.

            Comment

            Working...