Page 1 of 2 12 LastLast
Results 1 to 20 of 28
  1. #1
    zidangus is offline Member
    Join Date
    Jun 2010
    Posts
    8
    Rep Power
    0

    Default Help need on math java program

    Hi I am trying to write a java program that lets me evaluate the value of x over the range x=0 to100, for the function y=a*(x*8))+b*((x*8)^-0.5))+c and then print the values of y into a file. However the attempt I made below does not work can someone help me out and show me where I am going wrong and what to do to make the code work.

    public class javatest {

    public static void main(String[] args) {

    // Stream to write file
    FileOutputStream fout;

    try
    {
    // Open an output stream
    fout = new FileOutputStream ("output.txt");

    double a = 5;
    double b = 10;
    double c = 15;
    double y;
    double x;
    y=(a*(x*8))((b)*Math.pow((x*8)),-0.5)c;


    // for x returns y for 100 channels.
    for (x=0; x < 100; x+) {
    System.out.println(
    "The value of y is " y;

    // Print a line of text
    new PrintStream(fout).println (" The value of y is" y;);

    // Close our output stream
    fout.close();

    }

    }

    }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default

    attempt I made below does not work
    Please explain.

  4. #4
    zidangus is offline Member
    Join Date
    Jun 2010
    Posts
    8
    Rep Power
    0

    Default

    Ok I am knew to programming so forgive me if I dont know the "rules".
    The other forums I asked this question were
    Java Programming - Help needed using math in java
    and
    Help need on math java program - Java Programming Forums
    When I say it does not work I mean it does not compile.
    I have went through all the errors and I am left with the compiler complaining about using "to" and "catch"

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default

    it does not compile.
    Please copy and paste the full text of the error messages here.

  6. #6
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default

    Quote Originally Posted by zidangus View Post
    Ok I am knew to programming so forgive me if I dont know the "rules".
    The other forums I asked this question were
    Java Programming - Help needed using math in java
    and
    Help need on math java program - Java Programming Forums
    When I say it does not work I mean it does not compile.
    I have went through all the errors and I am left with the compiler complaining about using "to" and "catch"
    you probably mean try and catch???

    You have a try clause, it needs a to be completed with a catch clause.

    e.g.

    Java Code:
    try{
    int sumVariable=10
    }
    catch(Exception e)
    {
    e.printstacktrace();
    }
    catch comes after the closing brace of the try closing brace.

  7. #7
    zidangus is offline Member
    Join Date
    Jun 2010
    Posts
    8
    Rep Power
    0

    Default

    I have tried to write the code again. But I still cannot get it to print out every value of y for all values of x between 0 and 100. The new code is below. By the way it compiles but only prints out the value of y for x =101 ?.
    Java Code:
    //Evaluate function
    import java.lang.*;
    import javax.swing.*;
    import java.text.*;
    import java.io.*;
    
    public class jurofunction
    {                                                                      //begin class scope
    //get user input for the variables
    public static void main(String arg[])
    {                                                                     // begin main method scope
    
    JOptionPane.showMessageDialog(null, " The program calculates the value of y for a function of the form y = ax + bx ^-0.5 + c");
    // get input from the user for the variables a b  and offset
    
     double a =
          validateDouble("Please enter the value of a", "Input a");                      // ask the user for values of a b and offset and store them in method which validates the input see below
    
        double b =
          validateDouble("Please enter the value of b", "Input b");
          
         double c =
          validateDouble("Please enter the value of offset", "Input c");
          int x;
          double y=0;
    
    for (x=0; x<=100; x++)
    
    y = (a*(x*8)) + (b*Math.pow((x*8),-0.5))+c
    
    //  These next three lines allow the answer y to be rounded to 3 decimal places
    NumberFormat nf = NumberFormat.getNumberInstance();     
    nf.setMinimumFractionDigits(5);
    nf.setMaximumFractionDigits(5);
    // Displays value of y to 5 decimalplaces
    JOptionPane.showMessageDialog(null, " The value of y is " + nf.format(y)); 
    //Display text showing thanks for using the program 
    JOptionPane.showMessageDialog(null, " Thank you for using the program"); 
    ");
    
    //Create stream to write file to
    FileOutputStream fiout;
    try
    {                                                                         //begining of try block
    //open an output stream
    fiout = new FileOutputStream ("The-value-of-y.txt");
    // print to ouput file
    new PrintStream(fiout).println("Thanks for using the program.");
    new PrintStream(fiout).println("With a =" + nf.format(a));
    new PrintStream(fiout).println("With b =" + nf.format(b));
    new PrintStream(fiout).println("With offset =" + nf.format(c));
    new PrintStream(fiout).println("With x =" + nf.format(x));
    new PrintStream(fiout).println("The value of y is " + nf.format(y));
    // close the output stream
    fiout.close();
    }                                                                        // end of try block
    //Catch any errors
    catch (IOException e)                                   
    {                                                                       //begining of catch block
    System.err.println ("Unable to write o file ");
    System.exit(-1);
    }                                                                      // end of catch block
    }                                                                      //end of main method scope
    
                                                                        
          
    public static double validateDouble(String prompt, String titleBar)                            // begining of method to stop user invalid input 
      {
        boolean inputIsValid = false;
        String inputString = null;
        double input = 0.0;
    
        do {
          inputString =
            JOptionPane.showInputDialog(null, prompt, titleBar, JOptionPane.QUESTION_MESSAGE);
    
          // Input is valid if the user typed something
          inputIsValid = (inputString != null);
    
          // If the input is valid so far, attempt to convert it
          if (inputIsValid) {
            // Convert inputString to type double
            try {
              input = Double.parseDouble(inputString);                                      // here it ensures only double values can be accepted.
            }
            catch (NumberFormatException ex) {
              JOptionPane.showMessageDialog(null,"Invalid input value.","Error: Invalid Input",JOptionPane.ERROR_MESSAGE);
    
              // Set flag to false
              inputIsValid = false;
            }
            
              }
        }
        while (!inputIsValid);    // i.e. inputIsValid == false
    
        return input;
      }
      }	                                                          // end of class scope
    Any ideas as to where i am going wrong here ?
    Last edited by zidangus; 06-30-2010 at 10:29 AM.

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default

    for (x=0; x<=100; x++)
    Should this for have { and } following it enclosing more than 1 statement?

  9. #9
    zidangus is offline Member
    Join Date
    Jun 2010
    Posts
    8
    Rep Power
    0

    Default

    Yes sorry it should have { }. The code compiles however it only prints out one value of y and thats at x=101. I want it to print out all values of y for x between 0 and 100.
    Any suggestions on how I can get it to do this ?

  10. #10
    arun9683 is offline Member
    Join Date
    Jun 2010
    Location
    Bangalore,India
    Posts
    70
    Rep Power
    0

    Default

    Try with the below modification

    for (x=0; x<=100; x++)
    {

    y = (a*(x*8)) + (b*Math.pow((x*8),-0.5))+c

    // These next three lines allow the answer y to be rounded to 3 decimal places
    NumberFormat nf = NumberFormat.getNumberInstance();
    nf.setMinimumFractionDigits(5);
    nf.setMaximumFractionDigits(5);
    // Displays value of y to 5 decimalplaces
    JOptionPane.showMessageDialog(null, " The value of y is " + nf.format(y));
    //Display text showing thanks for using the program
    JOptionPane.showMessageDialog(null, " Thank you for using the program");
    ");

    //Create stream to write file to
    FileOutputStream fiout;
    try
    { //begining of try block
    //open an output stream
    fiout = new FileOutputStream ("The-value-of-y.txt");
    // print to ouput file
    new PrintStream(fiout).println("Thanks for using the program.");
    new PrintStream(fiout).println("With a =" + nf.format(a));
    new PrintStream(fiout).println("With b =" + nf.format(b));
    new PrintStream(fiout).println("With offset =" + nf.format(c));
    new PrintStream(fiout).println("With x =" + nf.format(x));
    new PrintStream(fiout).println("The value of y is " + nf.format(y));
    // close the output stream
    fiout.close();
    } // end of try block
    //Catch any errors
    catch (IOException e)
    { //begining of catch block
    System.err.println ("Unable to write o file ");
    System.exit(-1);
    }

    }// for closed

  11. #11
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default

    Please use code tags when posting code.

  12. #12
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,352
    Blog Entries
    7
    Rep Power
    20

    Default

    I think that the OP has the fundamental misunderstanding that if a variable y functionally depends on a variable x:

    Java Code:
    y= f(x);
    and if x changes that 'automagically' y changes accordingly. In Java (and a whole lot of other programming languages) it doesn't; i.e. you have to re-evaluate y again for new values of x; so that would be:

    Java Code:
    for (int x= 0; x < 100; x++) { // changing values of x
       y=f(x); // re-evaluate y because it depends on x
       ...
    }
    kind regards,

    Jos

  13. #13
    zidangus is offline Member
    Join Date
    Jun 2010
    Posts
    8
    Rep Power
    0

    Default

    Thanks JosAH, I tried your advice and it does not compile, am I putting your code in the right place ?
    Java Code:
    public class jurofunction3
    {                                                                      //begin class scope
    //get user input for the variables
    public static void main(String arg[])
    {                                                                     // begin main method scope
    
    JOptionPane.showMessageDialog(null, " The program calculates the value of y for 3rd order polynominals of the form y = ax + bx ^-0.5 + c");
    // get input from the user for the variables x z and h
    
     double a =
          validateDouble("Please enter the value of a", "Input a");                      // ask the user for values of a b and offset and store them in method which validates the input see below
    
        double b =
          validateDouble("Please enter the value of b", "Input b");
          
         double c =
          validateDouble("Please enter the value of offset", "Input c");
         int x;
         double y;
        double f(x);
        
     
        y= (a*(x*8)) + (b*Math.pow((x*8),-0.5))+c;
    
    for (x= 0; x < 100; x++) { // changing values of x
        y=f(x); // re-evaluate y because it depends on x
       
    }
    
    }
    compile errors
    jurofunction3.java:26: cannot find symbol
    symbol : method f(int)
    location: class jurofunction3
    double y=f(x)=0;
    ^
    jurofunction3.java:30: cannot find symbol
    symbol : method f(int)
    location: class jurofunction3
    y=f(x); // re-evaluate y because it depends on x
    ^
    jurofunction3.java:31: cannot find symbol
    symbol : method f(int)
    location: class jurofunction3
    f(x) = (a*(x*8)) + (b*Math.pow((x*8),-0.5))+c;
    ^
    3 errors
    Last edited by zidangus; 06-30-2010 at 03:25 PM.

  14. #14
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default

    f(x) was pseudo code, not java code.

  15. #15
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,352
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by zidangus View Post
    Thanks JosAH, I tried your advice and it does not compile, am I putting your code in the right place ?
    Java Code:
    public class jurofunction3
    {                                                                      //begin class scope
    //get user input for the variables
    public static void main(String arg[])
    {                                                                     // begin main method scope
    
    JOptionPane.showMessageDialog(null, " The program calculates the value of y for 3rd order polynominals of the form y = ax + bx ^-0.5 + c");
    // get input from the user for the variables x z and h
    
     double a =
          validateDouble("Please enter the value of a", "Input a");                      // ask the user for values of a b and offset and store them in method which validates the input see below
    
        double b =
          validateDouble("Please enter the value of b", "Input b");
          
         double c =
          validateDouble("Please enter the value of offset", "Input c");
         int x;
         double y;
        double f(x);
        
     
        y= (a*(x*8)) + (b*Math.pow((x*8),-0.5))+c;
    
    for (x= 0; x < 100; x++) { // changing values of x
        y=f(x); // re-evaluate y because it depends on x
       
    }
    
    }
    compile errors
    jurofunction3.java:26: cannot find symbol
    symbol : method f(int)
    location: class jurofunction3
    double y=f(x)=0;
    ^
    jurofunction3.java:30: cannot find symbol
    symbol : method f(int)
    location: class jurofunction3
    y=f(x); // re-evaluate y because it depends on x
    ^
    jurofunction3.java:31: cannot find symbol
    symbol : method f(int)
    location: class jurofunction3
    f(x) = (a*(x*8)) + (b*Math.pow((x*8),-0.5))+c;
    ^
    3 errors
    Look what you did: you introduced an 'f' without telling Java what that 'f' means. I just used that 'f' as any function so that y would be functionally dependent of x, so: y= f(x). Java, and all other programming languages, is way too stupid to understand your intentions (although I doubt you had any intentions at all except getting rid of this bloody program ;-) If you want 'f' to be understood by Java you have to define 'f' as a function, like so:

    Java Code:
    private static double f(double x) { 
        return (a*(x*8)) + (b*Math.pow((x*8),-0.5))+c;
    }
    and a, b and c have to be defined somewhere where 'f' can find them. For now forget this construct and forget about my 'f'; it was just a matter of speech. Instead of writing y= f(x); write this:

    Java Code:
    y= (a*(x*8)) + (b*Math.pow((x*8),-0.5))+c;
    in the body of your loop.

    kind regards,

    Jos

  16. #16
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    I already mentioned his creating a method in his cross-post yesterday, but he never responded to me or to many other responses in that thread... which is one reason I hate cross-posters....

  17. #17
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,352
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Fubarable View Post
    I already mentioned his creating a method in his cross-post yesterday, but he never responded to me or to many other responses in that thread... which is one reason I hate cross-posters....
    Ditto here; it made me think not to post code-formatted code blocks anymore because that's what most posters just copy and paste without reading the rest of the reply. See above: blindly copying pseudo code and complaining when it doesn't work. I am amazed that still some progress is being made in the world; it won't take long before it all comes to a standstill.

    kind regards,

    Jos

  18. #18
    zidangus is offline Member
    Join Date
    Jun 2010
    Posts
    8
    Rep Power
    0

    Default

    I would just like to know how can I get it to print in the output file all values of Y for each value of X in the loop. i have tried your suggestions and they do not compile. I am getting a headache :confused:

  19. #19
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default

    they do not compile
    You'll have to post the errors for anyone to help.

    I am getting a headache
    You're not alone.

  20. #20
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,352
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by zidangus View Post
    I would just like to know how can I get it to print in the output file all values of Y for each value of X in the loop. i have tried your suggestions and they do not compile. I am getting a headache :confused:
    Sigh, have a look at this:

    Java Code:
    int x;
    double y;
    double a, b, c;
    
    // first set values for a, b and c here ...
    
    for (x= 0; x < 100; x++) {
       y= (a*(x*8)) + (b*Math.pow((x*8),-0.5))+c;
       System.out.println(x+" "+y);
    }

Page 1 of 2 12 LastLast

Similar Threads

  1. Math Program
    By stlboi in forum New To Java
    Replies: 8
    Last Post: 04-02-2009, 09:34 PM
  2. need help with math for a new program
    By gotenks05 in forum New To Java
    Replies: 13
    Last Post: 09-26-2008, 07:32 AM
  3. Java Math
    By levent in forum Java Tutorial
    Replies: 1
    Last Post: 05-12-2008, 09:03 AM
  4. java.lang.Math
    By eva in forum New To Java
    Replies: 2
    Last Post: 01-31-2008, 04:17 PM
  5. Help with math in java
    By fernando in forum New To Java
    Replies: 1
    Last Post: 08-06-2007, 06:05 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •