Root Program cannot print

M

michalik.ireneusz

This is my program to find zero however it will not print and i cannot
find the mistake i mad. it might have to do with the out.close but i
am not sure can anyone fix it for me
/**
* Given an equation and a high and low , This program will find the
zero using recursion
*
* @author (Dan Michalik)
* @version (10/25/08)
*/
import java.io.*;
import java.lang.Math;
public class Main
{


File outfile;
PrintWriter out;

public void main()
{


// This prints the answer to a specific file
outfile = new File ("H:/results.txt");
try
{
out = new PrintWriter(outfile);

out.println("The zero of the given function is as follows...");
out.println();
Loop();
out.println();
out.println("This program was created by Dan Michalik");

System.out.close();
} catch(FileNotFoundException e) {};
}

double fofx (double x)
//coverts equation into a variable
{
double y = ((x*x*x*x)+6.01*(x*x*x)-51.728*(x*x)-110.55424*(x)
+76.9776);
return y;
system.out.print (fofx)
}
double signum (double x)
//determines if the variable is positive, negative, or zero
{
if (x<0){return 1;}
if (x==0){return 0;}
return -1;
}

double abs (double x)
//gives the absolute value of the equation
{
if (x<0){x = x * -1;}
return x;
}


double zero (double lo, double hi)
// recursion method that finds zero
{
double xmid = (hi + lo) / 2;
if ((abs(fofx(xmid))) <= .000001) {out.println(xmid); return
xmid;}
if((signum (fofx(xmid)) == signum (fofx(hi)))) {hi=xmid;}
if((signum (fofx(xmid)) == signum (fofx(lo)))) {lo=xmid;}
return zero (lo,hi);
}

void Loop()
{// this is the loop that perform the zero function
double x = -100.12347;
while (x <= 100.12347)
{ if (signum(fofx(x)) != signum(fofx(x+.01))) {zero(x+.01,x);}
x = (x+.01);


}
}
}
 
R

RedGrittyBrick

This is my program to find zero however it will not print and i cannot
find the mistake i mad. it might have to do with the out.close but i
am not sure can anyone fix it for me
/**
* Given an equation and a high and low , This program will find the
zero using recursion
*
* @author (Dan Michalik)
* @version (10/25/08)
*/
import java.io.*;
import java.lang.Math;
public class Main
{


File outfile;
PrintWriter out;

public void main()
{


// This prints the answer to a specific file
outfile = new File ("H:/results.txt");
try
{
out = new PrintWriter(outfile);

out.println("The zero of the given function is as follows...");
out.println();
Loop();
out.println();
out.println("This program was created by Dan Michalik");

System.out.close();


Why are you closing System.out? Did you mean to close the local 'out'
instead?
} catch(FileNotFoundException e) {};

Maybe the program is throwing this exception. You shouldn't ignore
exceptions. I'd at least put in:
e.printStackTrace();
System.exit(1);

}

double fofx (double x)
//coverts equation into a variable
{
double y = ((x*x*x*x)+6.01*(x*x*x)-51.728*(x*x)-110.55424*(x)
+76.9776);
return y;
system.out.print (fofx)

This statement can never be reached. Your compiler should tell you that.
Also, fofx isn't a variable and you are missing a final semicolon. Is
this real code?
}
double signum (double x)
//determines if the variable is positive, negative, or zero
{
if (x<0){return 1;}
if (x==0){return 0;}
return -1;
}

The return values look more like integers than doubles. Does this compile?
double abs (double x)
//gives the absolute value of the equation
{
if (x<0){x = x * -1;}
return x;

I'd write that as
if (x<0) return -x;
else return x;
}


double zero (double lo, double hi)
// recursion method that finds zero
{
double xmid = (hi + lo) / 2;
if ((abs(fofx(xmid))) <= .000001) {out.println(xmid); return
xmid;}

Unless I'm mistaken, fofx is an instance method of Main and you don't
have an instance of Main. Perhaps I've missed something. Maybe you meant
to declare fofx() as static?

Your method zero() refers to the variable 'out' but wasn't the scope of
that limited to main()?

if((signum (fofx(xmid)) == signum (fofx(hi)))) {hi=xmid;}
if((signum (fofx(xmid)) == signum (fofx(lo)))) {lo=xmid;}
return zero (lo,hi);
}

void Loop()

Method names should start with a lowercase letter otherwise you'll
confuse everyone who follows Sun naming conventions.
{// this is the loop that perform the zero function
double x = -100.12347;
while (x <= 100.12347)
{ if (signum(fofx(x)) != signum(fofx(x+.01))) {zero(x+.01,x);}
x = (x+.01);

You are incrementing x outside the while loop, that seems odd to me. If
you ever got it to compile I'd expect it to loop forever. The brackets
look misplaced too.

The erratic indentation makes it hard to read.

I find it hard to believe the above is even close to compiling. But you
don't mention any compiler errors.
 
R

Roedy Green

/**
* Given an equation and a high and low , This program will find the
zero using recursion
*
* @author (Dan Michalik)
* @version (10/25/08)
*/
import java.io.*;
import java.lang.Math;
public class Main
{


File outfile;
PrintWriter out;

public void main()
{


// This prints the answer to a specific file
outfile = new File ("H:/results.txt");
try
{
out = new PrintWriter(outfile);

out.println("The zero of the given function is as follows...");
out.println();
Loop();
out.println();
out.println("This program was created by Dan Michalik");

System.out.close();
} catch(FileNotFoundException e) {};
}

double fofx (double x)
//coverts equation into a variable
{
double y = ((x*x*x*x)+6.01*(x*x*x)-51.728*(x*x)-110.55424*(x)
+76.9776);
return y;
system.out.print (fofx)
}
double signum (double x)
//determines if the variable is positive, negative, or zero
{
if (x<0){return 1;}
if (x==0){return 0;}
return -1;
}

double abs (double x)
//gives the absolute value of the equation
{
if (x<0){x = x * -1;}
return x;
}


double zero (double lo, double hi)
// recursion method that finds zero
{
double xmid = (hi + lo) / 2;
if ((abs(fofx(xmid))) <= .000001) {out.println(xmid); return
xmid;}
if((signum (fofx(xmid)) == signum (fofx(hi)))) {hi=xmid;}
if((signum (fofx(xmid)) == signum (fofx(lo)))) {lo=xmid;}
return zero (lo,hi);
}

void Loop()
{// this is the loop that perform the zero function
double x = -100.12347;
while (x <= 100.12347)
{ if (signum(fofx(x)) != signum(fofx(x+.01))) {zero(x+.01,x);}
x = (x+.01);


}
}
}

Your code does not even compile. It is badly indented confusing the
meaning


/**
* Given an equation and a high and low , This program will find the
zero using recursion
*
* @author (Dan Michalik)
* @version (10/25/08)
*/
import java.io.*;
import java.lang.Math;
public class FindRoot
{
File outfile;
PrintWriter out;

/** ?? main always has args */
public void main()
{


// This prints the answer to a specific file
outfile = new File ("H:/results.txt");
try
{
out = new PrintWriter(outfile);

out.println("The zero of the given function is as
follows...");
out.println();
loop(); // ?? methods start with lower case letter.
out.println();
out.println("This program was created by Dan Michalik");

System.out.close();
}
catch ( FileNotFoundException e )
{
};
}

/** ?? missing javadoc */
double fofx (double x)
//coverts equation into a variable
{
double y = ((x*x*x*x)+6.01*(x*x*x)-51.728*(x*x)-110.55424*(x)
+76.9776);
// ?? perhaps you meant System.out.print( y );

return y;
// System.out.print (fofx); // ?? missing ; System needs
capital S, no such variable fofox, unreachable.
}

/** ?? missing javadoc */
// ?? you importhed Math. Why note use Math.signum?
double signum (double x)
//determines if the variable is positive, negative, or zero
{
if ( x<0 )
{
return 1;
}
if ( x==0 )
{
return 0;
}
return -1;
}

/** ?? missing javadoc */
double abs (double x)
//gives the absolute value of the equation
{
if ( x<0 )
{
x = x * -1;
}
return x;
}

/** ?? missing javadoc */
double zero (double lo, double hi)
// recursion method that finds zero
{
double xmid = (hi + lo) / 2;
if ( (abs(fofx(xmid))) <= .000001 )
{
out.println(xmid); return
xmid;
}
if ( (signum (fofx(xmid)) == signum (fofx(hi))) )
{
hi=xmid;
}
if ( (signum (fofx(xmid)) == signum (fofx(lo))) )
{
lo=xmid;
}
return zero (lo,hi);
}

/** ?? missing javadoc. Methods start with lower case. */
void loop()
{// this is the loop that perform the zero function
double x = -100.12347;
while ( x <= 100.12347 )
{
if ( signum(fofx(x)) != signum(fofx(x+.01)) )
{
zero(x+.01,x);
}
x = (x+.01);
}
}
}


--
Roedy Green Canadian Mind Products
http://mindprod.com
Your old road is
Rapidly agin'.
Please get out of the new one
If you can't lend your hand
For the times they are a-changin'.
 
L

Lew

This is my program to find zero however it will not print ...
/**
 * Given an equation and a high and low , This program will find the
zero using recursion
 *
 * @author (Dan Michalik)
 * @version (10/25/08)
 */
import java.io.*;
import java.lang.Math;
public class Main
{

        File outfile;
        PrintWriter out;

        public void main()

You don't show us how you attempt to compile or run the code, nor what
error messages you receive. You should do that. But one can tell up
front that you cannot run this program directly because it lacks a
'public static void main( String [] args )' method.

The main method must be static and must have a single 'String []'
parameter for the java runtime to execute it.

Start here:
<http://java.sun.com/docs/books/tutorial/index.html>
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,077
Latest member
SangMoor21

Latest Threads

Top