Results 1 to 20 of 28
Thread: Help need on math java program
- 06-29-2010, 11:38 PM #1
Member
- Join Date
- Jun 2010
- Posts
- 8
- Rep Power
- 0
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();
}
}
}
-
- 06-30-2010, 12:00 AM #3attempt I made below does not work
- 06-30-2010, 12:54 AM #4
Member
- Join Date
- Jun 2010
- Posts
- 8
- Rep Power
- 0
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"
- 06-30-2010, 02:10 AM #5it does not compile.
- 06-30-2010, 02:45 AM #6
Senior Member
- Join Date
- Feb 2010
- Location
- Waterford, Ireland
- Posts
- 748
- Rep Power
- 12
- 06-30-2010, 10:26 AM #7
Member
- Join Date
- Jun 2010
- Posts
- 8
- Rep Power
- 0
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
Last edited by zidangus; 06-30-2010 at 10:29 AM.
- 06-30-2010, 12:29 PM #8for (x=0; x<=100; x++)
- 06-30-2010, 01:42 PM #9
Member
- Join Date
- Jun 2010
- Posts
- 8
- Rep Power
- 0
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 ?
- 06-30-2010, 02:15 PM #10
Member
- Join Date
- Jun 2010
- Location
- Bangalore,India
- Posts
- 70
- Rep Power
- 0
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
- 06-30-2010, 02:38 PM #11
Please use code tags when posting code.
- 06-30-2010, 03:04 PM #12
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 14,422
- Blog Entries
- 7
- Rep Power
- 29
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);
Java Code:for (int x= 0; x < 100; x++) { // changing values of x y=f(x); // re-evaluate y because it depends on x ... }
Jos
- 06-30-2010, 03:22 PM #13
Member
- Join Date
- Jun 2010
- Posts
- 8
- Rep Power
- 0
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 } }
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 errorsLast edited by zidangus; 06-30-2010 at 03:25 PM.
- 06-30-2010, 03:24 PM #14
f(x) was pseudo code, not java code.
- 06-30-2010, 03:41 PM #15
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 14,422
- Blog Entries
- 7
- Rep Power
- 29
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; }
Java Code:y= (a*(x*8)) + (b*Math.pow((x*8),-0.5))+c;
kind regards,
Jos
-
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....
- 06-30-2010, 03:53 PM #17
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 14,422
- Blog Entries
- 7
- Rep Power
- 29
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
- 06-30-2010, 04:02 PM #18
Member
- Join Date
- Jun 2010
- Posts
- 8
- Rep Power
- 0
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:
- 06-30-2010, 04:06 PM #19they do not compile
I am getting a headache
- 06-30-2010, 04:07 PM #20
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 14,422
- Blog Entries
- 7
- Rep Power
- 29
Similar Threads
-
Math Program
By stlboi in forum New To JavaReplies: 8Last Post: 04-02-2009, 09:34 PM -
need help with math for a new program
By gotenks05 in forum New To JavaReplies: 13Last Post: 09-26-2008, 07:32 AM -
Java Math
By levent in forum Java TutorialReplies: 1Last Post: 05-12-2008, 09:03 AM -
java.lang.Math
By eva in forum New To JavaReplies: 2Last Post: 01-31-2008, 04:17 PM -
Help with math in java
By fernando in forum New To JavaReplies: 1Last Post: 08-06-2007, 06:05 AM
Bookmarks