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 / November 2007

Tip: Looking for answers? Try searching our database.

evaluate function

Thread view: 
(-Peter-) - 12 Nov 2007 17:16 GMT
Hi.

What is easiest to do if I want to make a program to evaluate the
value f(x) of some function at several points.

Is it possible to specify the function first, for example f(x) = x^3 -
x - 4 and then afterwards type for example f(10) and get the result
986 =(1000 - 10 - 4)?

/Peter
Eric Sosman - 12 Nov 2007 17:47 GMT
(-Peter-) wrote On 11/12/07 12:16,:
> Hi.
>
[quoted text clipped - 4 lines]
> x - 4 and then afterwards type for example f(10) and get the result
> 986 =(1000 - 10 - 4)?

    static double f(double x) {
       return x * x * x - x - 4;
    }
    ...
    System.out.println(f(10));

... but I have a feeling this isn't the answer to the
question you really meant to ask.  Tell us more.

Signature

Eric.Sosman@sun.com

Lew - 12 Nov 2007 18:08 GMT
> (-Peter-) wrote On 11/12/07 12:16,:
>> Hi.
[quoted text clipped - 14 lines]
> .... but I have a feeling this isn't the answer to the
> question you really meant to ask.  Tell us more.

During development, nothing is impossible to a programmer.  During production,
the bug actually observed elicits from the programmer, "That's impossible!"

Signature

Lew

(-Peter-) - 12 Nov 2007 18:18 GMT
> > (-Peter-) wrote On 11/12/07 12:16,:
> >> Hi.
[quoted text clipped - 20 lines]
> --
> Lew

Oh.. I forgot one question:

Isn't correct to make a program like this (to make the method outside
the main program, but in the same file):

public class Program
{
    static double f(double x)
    {
           return 3*x*x*x + 2*x*x + 0.2;
       }

    public static void main (String[] args)
    {
           System.out.println(f(0) + "\t" + f(1));
       }
}

/Peter
Lew - 12 Nov 2007 18:33 GMT
> Isn't correct to make a program like this (to make the method outside
> the main program, but in the same file):

Assuming that's a question, yes, it is correct.  It worked for you, didn't it?

> public class Program
> {
>    static double f(double x)

Do not embed TAB characters in Usenet source listings.  It makes life hard on
those using newsreaders (everyone).

A reasonable indentation is two to four spaces per level.

>   {
>     return 3*x*x*x + 2*x*x + 0.2;
[quoted text clipped - 5 lines]
>   }
> }

A small point - instead of thinking of your source as a "file", think of it as
a "class".  The fact that the class is specified in a file is secondary to its
purpose.

Note that the method 'f()' has package-private (a.k.a., "default") access as
declared.  Package-private is a very useful access level, maybe not what you
need right here this time but useful.  It does no harm here.

Likewise note that the arguments to f() as provided are ints, implicitly
widened to double by the mechanics of method invocation.

Signature

Lew

(-Peter-) - 12 Nov 2007 18:46 GMT
> > Isn't correct to make a program like this (to make the method outside
> > the main program, but in the same file):
[quoted text clipped - 33 lines]
> --
> Lew

thank you for the comments, and yes - it did work :-)

/Peter
(-Peter-) - 12 Nov 2007 18:14 GMT
>         static double f(double x) {
>             return x * x * x - x - 4;
[quoted text clipped - 7 lines]
> --
> Eric.Sos...@sun.com

Hi and thank you for the answer - it was what I meant, I just think
that I'm not experienced enough with java to figure this out..

/peter
Roedy Green - 12 Nov 2007 19:16 GMT
On Mon, 12 Nov 2007 17:16:15 -0000, "(-Peter-)"
<garfieldpbj@gmail.com> wrote, quoted or indirectly quoted someone who
said :

>What is easiest to do if I want to make a program to evaluate the
>value f(x) of some function at several points.

see http://mindprod.com/jgloss/eval.html
http://mindprod.com/jgloss/delegate.html
http://mindprod.com/jgloss/callback.html
Signature

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com



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.