What is the problem in this program?

Discussion in 'Java' started by f2prateek, Jan 17, 2007.

  1. f2prateek

    f2prateek Guest

    //program to find the roots of a quadratic eqaution

    import java.io.*;
    public class quadratic_roots
    {
    int r1,r2;
    public void accept(int a,int b,int c)
    {

    r1=-b/2a+(math.sqrt(b*b-4ac))/2a;
    r2=-b/2a-(math.sqrt(b*b-4ac))/2a;
    System.out.println(r1+" , "+r2);
    }
    }
    f2prateek, Jan 17, 2007
    #1
    1. Advertising

  2. f2prateek

    Daniel Dyer Guest

    On Wed, 17 Jan 2007 12:34:45 -0000, f2prateek <> wrote:

    > //program to find the roots of a quadratic eqaution
    >
    > import java.io.*;
    > public class quadratic_roots
    > {
    > int r1,r2;
    > public void accept(int a,int b,int c)
    > {
    >
    > r1=-b/2a+(math.sqrt(b*b-4ac))/2a;
    > r2=-b/2a-(math.sqrt(b*b-4ac))/2a;
    > System.out.println(r1+" , "+r2);
    > }
    > }
    >


    It doesn't compile and it has redundant imports.

    Dan.

    --
    Daniel Dyer
    http://www.uncommons.org
    Daniel Dyer, Jan 17, 2007
    #2
    1. Advertising

  3. f2prateek

    f2prateek Guest

    What do you mean?
    f2prateek, Jan 17, 2007
    #3
  4. f2prateek

    Daniel Dyer Guest

    On Wed, 17 Jan 2007 12:51:48 -0000, f2prateek <> wrote:

    > What do you mean?


    What do you want to hear? You haven't given any indication of the problem
    that you are having. If you are getting an error at compile time or at
    runtime you should post the error message. Or if the result of the program
    is wrong you should indicate how it is wrong. The only two things that
    were immediately obvious as "wrong" were the ones I mentioned.

    The code will not compile, "math.sqrt" is invalid. The Math class begins
    with an uppercase letter. I'm guessing this is the problem that you want
    help with because until you've fixed this you won't make any further
    progress.

    You are importing every class in the java.io package but not using any of
    them. This won't stop your code from working but it is unnecessary.

    Dan.

    --
    Daniel Dyer
    http://www.uncommons.org
    Daniel Dyer, Jan 17, 2007
    #4
  5. -----BEGIN PGP SIGNED MESSAGE-----
    Hash: SHA1

    f2prateek schreef:
    > What do you mean?


    Please quote some content when replying.

    He means that it doesn’t compile and contains redundant imports. Look
    up those words in a dictionary if you don’t understand them.
    Furthermore, it does not follow style conventions.

    H.
    - --
    Hendrik Maryns
    http://tcl.sfs.uni-tuebingen.de/~hendrik/
    ==================
    http://aouw.org
    Ask smart questions, get good answers:
    http://www.catb.org/~esr/faqs/smart-questions.html
    -----BEGIN PGP SIGNATURE-----
    Version: GnuPG v1.4.2 (GNU/Linux)

    iD4DBQFFrh4ye+7xMGD3itQRAhdaAJ9dsaZGQGIxyyd7+cRG+Ua49VT2VwCXYsCS
    cvi7FnyOOd2mpvYSU348sw==
    =OImi
    -----END PGP SIGNATURE-----
    Hendrik Maryns, Jan 17, 2007
    #5
  6. f2prateek

    f2prateek Guest

    Sorry i did not post the error my program.The error it is showing is it
    needs a ';' and higlights the line:

    r1=-b/2a+(math.sqrt(b*b-4ac))/2a;
    f2prateek, Jan 17, 2007
    #6
  7. Hi,

    f2prateek wrote:
    > Sorry i did not post the error my program.The error it is showing is it
    > needs a ';' and higlights the line:
    >
    > r1=-b/2a+(math.sqrt(b*b-4ac))/2a;


    Note that (besides the other errors), you must - in contradiction to the
    mathematical notation - explicitely write the multiplication *!

    Ciao,
    Ingo
    Ingo R. Homann, Jan 17, 2007
    #7
  8. f2prateek wrote:
    > Sorry i did not post the error my program.The error it is showing is it
    > needs a ';' and higlights the line:
    >
    > r1=-b/2a+(math.sqrt(b*b-4ac))/2a;


    You do not seem to be listenning carefully to what
    you are being told. I recall Daniel mentioned that
    'math.sqrt' should be 'Math.sqrt'.

    Why did you not check that method in the JavaDocs?
    (Have you heard of the JavaDocs?)

    The line should be more like this, in any case..

    r1=-b/(2*a)+(Math.sqrt(b*b-4*(a*c)))/(2*a);

    Note that '2a' is not understood by Java as
    being 2*a the programmer needs to write
    the '*' specifically.

    It also helps to use more brackets, in order
    to ensure that the multiplications and divisions
    are done in the order you wish. I may have
    got the brackets wrong in the above example.

    Then, when those problems are fixed, you
    need to fix a 'possible loss of precision'.

    It seems you are very new to Java. A good
    group for those new to Java is comp.lang.java.help.
    Also, if you haven't discovered them yet,
    look around for the JavaDocs and the Java
    tutorial. I think both would be a great help
    to you.

    You were given a number of good tips by
    the other replies on this thread - please
    review them all carefully.

    HTH

    Andrew T.
    Andrew Thompson, Jan 17, 2007
    #8
  9. f2prateek

    f2prateek Guest

    I have tried all the variations you have told me,including the 'M'
    capital in Math.sqrt .I accidentally omitted the * in the
    multiplication calculation in the program i submitted to you.
    f2prateek, Jan 17, 2007
    #9
  10. f2prateek

    f2prateek Guest

    //program to find the roots of a quadratic eqaution

    import java.io.*;
    public class quadratic_roots
    {
    float r1,r2;
    public void accept(int a,int b,int c)
    {
    r1=-b/2*a+(Math.sqrt(b*b-4*a*c))/2*a;
    r2=-b/2*a-(Math.sqrt(b*b-4*a*c))/2*a;
    System.out.println(r1+" , "+r2);
    }
    }

    now it's showing me possible loss of precision in the line:
    r1=-b/2*a+(Math.sqrt(b*b-4*a*c))/2*a;
    f2prateek, Jan 17, 2007
    #10
  11. f2prateek

    f2prateek Guest

    Sorry.Some typing mistake again.
    My program is:
    //program to find the roots of a quadratic eqaution

    import java.io.*;
    public class quadratic_roots
    {
    float r1,r2;
    public void accept(int a,int b,int c)
    {
    r1=-b/(2*a)+(Math.sqrt(b*b-4*(a*c)))/(2*a);
    r2=-b/(2*a)-(Math.sqrt(b*b-4*(a*c)))/(2*a);
    System.out.println(r1+" , "+r2);
    }
    }

    It is showing me 'possible loss of precision' in the line:
    r1=-b/(2*a)+(Math.sqrt(b*b-4*(a*c)))/(2*a);
    f2prateek, Jan 17, 2007
    #11
  12. f2prateek wrote:
    > Sorry.Some typing mistake again.


    You are doing a number of mistakes at the
    moment in terms of getting people to *want*
    to help you.

    1st of all - do not waste our time with typing
    mistakes.

    Always copy/paste both code and compiler
    or runtime errors. *Always*.

    Next - you seem to have ignored me when I
    asked you to review this thread carefully.

    You are still making mistakes that others
    have already mentioned, and it is obvious
    from your flurry of replies that you did not
    even read my post carefully.

    As such - I will make no further posts on
    this thread.

    See you over on c.l.j.help, if you should
    choose to post there.

    Andrew T.
    Andrew Thompson, Jan 17, 2007
    #12
  13. f2prateek

    Guest

    f2prateek wrote:
    > Sorry.Some typing mistake again.
    > My program is:
    > //program to find the roots of a quadratic eqaution
    >
    > import java.io.*;
    > public class quadratic_roots
    > {
    > float r1,r2;
    > public void accept(int a,int b,int c)
    > {
    > r1=-b/(2*a)+(Math.sqrt(b*b-4*(a*c)))/(2*a);
    > r2=-b/(2*a)-(Math.sqrt(b*b-4*(a*c)))/(2*a);
    > System.out.println(r1+" , "+r2);
    > }
    > }
    >
    > It is showing me 'possible loss of precision' in the line:
    > r1=-b/(2*a)+(Math.sqrt(b*b-4*(a*c)))/(2*a);


    You've gotten far enough along here that you're starting to deal with
    errors that are a bit more specific to Java that to programming in
    general,
    but you really need to get some background in basic elements of the
    language.
    You've got two immediate things to worry about. The first term on the
    right of the
    equality involves integer division. This likely doesn't do what you
    want in Java.
    E.g., what do you think
    int x = 3
    double y = x/2;
    System.out.println(y)
    prints out? It's not 1.5.

    The second issue-- which is causing your current error message --
    involves
    what happens in a computer program when the arguments to an operator
    involves different data types. In Java Math.sqrt always returns a
    'double'.
    You are adding this to an 'int' value. Java's rule is that the integer
    argument
    is converted to a double and you get a double result. In the end you
    try
    to save this as a float (in r1 or r2). Since floats have less
    precision than
    doubles, Java does not allow you to do this without an explicit cast.

    There are some other issues you may want to think about -- what happens
    when the discriminant is negative? But before getting into such
    subtleties
    you need to spend some time learning basic Java to understand how
    it does arithmetic and coversions amongst the arithmetic types.

    Regards,
    Tom McGlynn
    , Jan 17, 2007
    #13
  14. f2prateek

    Paul Tomblin Guest

    In a previous article, "f2prateek" <> said:

    >What do you mean?
    >


    Why don't you ask your teacher, since that's the person who is getting
    paid to answer your questions?

    --
    Paul Tomblin <> http://blog.xcski.com/
    Alright. Talk. Don't make me reach over there and pull your still-pumping
    heart out from the gaping hole you used to call a chest whilst breaking
    your sternum and playing air guitar with your ribcage. -- Tai
    Paul Tomblin, Jan 17, 2007
    #14
  15. f2prateek

    f2prateek Guest

    The program I have given you is much more advanced than what the ICSE
    board want the students to learn till the ninth..it will be of a higher
    level in tenth...I am just making this program to make finding the
    roots in my maths faster and easier.And since 20 mks are in my teachers
    hand.who probably will not understand what I am trying to do or what to
    do....I don't want to offend her.
    f2prateek, Jan 17, 2007
    #15
  16. f2prateek wrote:
    > //program to find the roots of a quadratic eqaution


    Note that the original poster took my
    recommendation to post over at c.l.j.help,
    but ended up solving the 'loss of precision'
    problem themselves.

    There are a lot of things the OP might yet
    benefit to learn about posting/asking questions,
    and Java, but I think the same can be said,
    of most of us.

    Andrew T.
    Andrew Thompson, Jan 17, 2007
    #16
  17. f2prateek

    Alex Hunsley Guest

    Andrew Thompson wrote:
    > f2prateek wrote:
    >> Sorry i did not post the error my program.The error it is showing is it
    >> needs a ';' and higlights the line:
    >>
    >> r1=-b/2a+(math.sqrt(b*b-4ac))/2a;

    >
    > You do not seem to be listenning carefully to what
    > you are being told. I recall Daniel mentioned that
    > 'math.sqrt' should be 'Math.sqrt'.
    >
    > Why did you not check that method in the JavaDocs?
    > (Have you heard of the JavaDocs?)
    >
    > The line should be more like this, in any case..
    >
    > r1=-b/(2*a)+(Math.sqrt(b*b-4*(a*c)))/(2*a);


    Yup, it's correct, but one inefficiency that can be taken out is the
    dividing by 2a twice, since:


    -b sqrt(b^2 - 4ac)
    -- +/- ----------------
    2a 2a

    is equal to:

    -b +/- sqrt(b^2 - 4ac)
    ---------------------
    2a

    lex
    Alex Hunsley, Jan 18, 2007
    #17
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. pipi
    Replies:
    1
    Views:
    613
    Joseph Millar
    Jul 21, 2003
  2. Rey
    Replies:
    4
    Views:
    10,914
    Roedy Green
    Dec 12, 2003
  3. ola
    Replies:
    3
    Views:
    702
    Marco Schmidt
    Feb 16, 2004
  4. Parvsandhu
    Replies:
    2
    Views:
    406
    Thomas Weidenfeller
    Jul 11, 2005
  5. sylsau
    Replies:
    1
    Views:
    347
    Steve Sobol
    Apr 29, 2006
Loading...

Share This Page