Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / January 2007

Tip: Looking for answers? Try searching our database.

What is the problem in this program?

Thread view: 
f2prateek - 17 Jan 2007 12:34 GMT
//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);
}
}
Daniel Dyer - 17 Jan 2007 12:40 GMT
> //program to find the roots of a quadratic eqaution
>
[quoted text clipped - 10 lines]
> }
> }

It doesn't compile and it has redundant imports.

Dan.

Signature

Daniel Dyer
http://www.uncommons.org

f2prateek - 17 Jan 2007 12:51 GMT
What do you mean?
Daniel Dyer - 17 Jan 2007 13:01 GMT
> 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.

Signature

Daniel Dyer
http://www.uncommons.org

Hendrik Maryns - 17 Jan 2007 13:01 GMT
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
f2prateek - 17 Jan 2007 13:16 GMT
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;
Ingo R. Homann - 17 Jan 2007 13:27 GMT
Hi,

> 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
Andrew Thompson - 17 Jan 2007 13:36 GMT
> 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.
f2prateek - 17 Jan 2007 13:41 GMT
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 - 17 Jan 2007 13:42 GMT
//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 - 17 Jan 2007 13:45 GMT
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);
Andrew Thompson - 17 Jan 2007 13:55 GMT
> 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.
tam@lheapop.gsfc.nasa.gov - 17 Jan 2007 14:34 GMT
> Sorry.Some typing mistake again.
> My program is:
[quoted text clipped - 14 lines]
> 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
Alex Hunsley - 18 Jan 2007 16:32 GMT
>> Sorry i did not post the error my program.The error it is showing is it
>> needs a ';' and higlights the line:
[quoted text clipped - 11 lines]
>
>   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
Paul Tomblin - 17 Jan 2007 15:43 GMT
In a previous article, "f2prateek" <f2prateek@gmail.com> 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?

Signature

Paul Tomblin <ptomblin@xcski.com> 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

f2prateek - 17 Jan 2007 17:01 GMT
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.
Andrew Thompson - 17 Jan 2007 17:59 GMT
> //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.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.