# Is it possible to make this in Java? Challenging question.

Show 40 post(s) from this thread on one page
Page 1 of 2 12 Last
• 07-26-2008, 06:43 PM
matt_well
Equation problems in Java
y0 = x0
y1 = 2*y0 + x1

y2 = 2*y1 - y0 + x2
y3 = 2*y2 - y1 + x3
y4 = 2*y3 - y2 + x4
y5 = 2*y4 - y3 + x5
y6 = 2*y5 - y4 + x6
y7 = 2*y6 - y5 + x7
y8 = 2*y7 - y6 + x8
y9 = 2*y8 - y7 + x9
y10 = 2*y9 - y8 + x10

and continues until the maximum number in a text file.
"x0" is the first number in the text file and so on.
Here my text file name is "Xdata.txt" that contains:
Quote:

1.2
-1.7
0.8
4.6
0.9
1.0
0.0
5.4
3.9
9.2
1.5
4.92
-0.5
3.7
2.5
8.0
9.8
-0.8
1.0
2.0

The program read from the "Xdata.txt" and get all the outputs into "Ydata.txt"
• 07-26-2008, 07:53 PM
Norm
It reads a number (x0) from a file and does some computations on it until when? What stops the computation sequence? Your example shows it stopping at y10.
Quote:

continues until the maximum number in a text file
The maximum number(by that I assume you mean the largest) that is in the file is 9.8. It's hard to do something 9.8 times. Do you mean read to end of file?

By doing algebraic substitution on the 10 equations you show, you can reduce it to a single equation. So why do you show 10 equations? Can there be more? How many?
It seems like the program only needs this simple form:
loop until eof
compute y10 = <the reduced equation>
write y10
end of loop

What is the program supposed to do?

It is not possible to write a program without there being much better specifications.
• 07-26-2008, 08:45 PM
matt_well
Quote:

Originally Posted by Norm
It reads a number (x0) from a file and does some computations on it until when? What stops the computation sequence? Your example shows it stopping at y10.
The maximum number(by that I assume you mean the largest) that is in the file is 9.8. It's hard to do something 9.8 times. Do you mean read to end of file?

No no, the idea is like this.
Maximum number I mean here is the numbers that the textfile have from beginning until the last one.
What I mean is like this, x0 is the first number in the text file until the last value in the text file.
My text file name is "Xdata.txt" contains X0 until the last X19, so there is maximum of 20 numbers.

x0 = 1.2
x1 = -1.7
x2 = 0.8
x3 = 4.6
x4 = 0.9
x5 = 1.0
until
X19 = 2.0

Here's my details explaination,

Quote:

y0 = x0
y1 = 2*y0 + x1

y2 = 2*y1 - y0 + x2
y3 = 2*y2 - y1 + x3
y4 = 2*y3 - y2 + x4
y5 = 2*y4 - y3 + x5
y6 = 2*y5 - y4 + x6
y7 = 2*y6 - y5 + x7
y8 = 2*y7 - y6 + x8
y9 = 2*y8 - y7 + x9
y10 = 2*y9 - y8 + x10

and continues until the last number in a text file.
• 07-26-2008, 09:05 PM
Yes, of course it's possible, but, since I believe this is probably a homework assignment (and even if it's not) I won't do it for you. Try it yourself, and if you get stuck, post your code and all error/compiler messages and we will help you correct it, but we will not do it for you.
• 07-26-2008, 09:16 PM
matt_well
Quote:

Yes, of course it's possible, but, since I believe this is probably a homework assignment (and even if it's not) I won't do it for you. Try it yourself, and if you get stuck, post your code and all error/compiler messages and we will help you correct it, but we will not do it for you.

Hehe, it's not homework assignment, just learning Java for fun.
I hope to have assignment too.:D
I am a electrical graduated researcher now, just want to get to learn more on Java that I learn about some basic before during school.
• 07-26-2008, 09:39 PM
matt_well
This question is from my stupid idea mind to play with this textfile previously.
Ok, reading input from txt file is not the problem, but now the problem is that how should I start a for loop for these equation in this case using the continuous input x0 until x19 read from the textfile?

From the text file,
y0 = 1.2
y1 = 2*1.2 + (-1.7)
blah blah...

where...

y0 = x0
y1 = 2*y0 + x1

y2 = 2*y1 - y0 + x2
y3 = 2*y2 - y1 + x3
y4 = 2*y3 - y2 + x4
y5 = 2*y4 - y3 + x5
y6 = 2*y5 - y4 + x6
y7 = 2*y6 - y5 + x7
y8 = 2*y7 - y6 + x8
y9 = 2*y8 - y7 + x9
y10 = 2*y9 - y8 + x10

As you can see, the previously output are use for the next input, so I am thinking how's the for loop can be create.
• 07-26-2008, 10:43 PM
Norm
Ok, I missed the x variable at the end of each equation.
So your problem is to read a line with a number, convert it to a number and if its the first line save the value in y and go read another line.
If its not the first line, compute a new value for y using the old value of y and the new value read from the line x.
Continue until end of file.
Code:

```  loop until eof:     read line & convert to x     if first line         y = value;     else         y = func(y, x, oldy)         save oldy   end loop```
You know this has NOTHING to do with java programming and you are wasting time trying to write this kind of program. Its purely an exercise in creating an algorithm for an obscure problem.
If you want to learn java, I'd suggest finding some more productive problems to solve. There are several sites you can Google that will have sample student exercises you can attempt.

Good luck.
• 07-27-2008, 12:14 AM
matt_well
Thanks Norm for some guides. Here's the current doing, but cannot run yet.

Code:

```import java.io.*; import java.util.*; public class FindOutputs {     public static void main(String[] args) throws java.io.IOException     {         String inputFileName = "Xdata.txt";         List<String> fileData = getData(inputFileName);         System.out.println('\n'+ "fileData size = " + fileData.size());         String path = "C:\\Ydata.txt";         PrintWriter pw = new PrintWriter(                         new FileOutputStream(path));         int maxSize = fileData.size();         for(int i = 0; i < maxSize; i++)         {             String data = getNextValue(fileData, i);             double x = parseValue(data);             if(firstline = ){                                 y = value;                         }                                                 else{y = func(y, x, oldy);                                     pw.printf("%1.2f%n",oldy);                         }         }         pw.close();                 System.out.print( "Output file at C drive now" + '\n');     }     private static List<String> getData(String filePath) throws IOException     {         List<String> list = new ArrayList<String>();         FileReader fr = new FileReader(filePath);         BufferedReader br = new BufferedReader(fr);         String buffer;         while( (buffer = br.readLine()) != null)         {             list.add(buffer);         }         br.close();         return list;     }     private static String getNextValue(List<String> list, int index)     {         if(index > list.size()-1) // gone past end of list             return "";         return list.get(index);     }     private static double parseValue(String s)     {         if(s.equals("")) // missing data values appear as empty string             return 0;         return Double.parseDouble(s);     } }```

I am not so sure about the meaning..
Code:

```    if first line         y = value;     else         y = func(y, x, oldy)```
How to get the first line and how's the working inside the function?
I am on stuck in the middle part. Correct me.
• 07-27-2008, 03:03 AM
Norm
• 07-27-2008, 07:25 AM
matt_well
Still interest in doing, can you give me some guide please how this work?
Code:

```    if first line         y = value;     else         y = func(y, x, oldy)```
• 07-27-2008, 09:13 AM
Well, set a boolean true before you start reading, and after reading the first line set it false. Use that boolean to determine whether or not you've already read the first line.

That's one easy way to do it, though not necessarily the most sophisticated.
• 07-27-2008, 04:20 PM
Norm
The 3rd and following equations you show can be replaced by a single one:

y[i] = 2*y[i-1] - y[i-2] + x[i]

You need to handle the cases where i is less than 2 manually, then this equation will work forever after that.
• 07-27-2008, 07:39 PM
matt_well
Hi, here's some doing ideas.

Code:

```        for(int i = 2; i < maxSize; i++)         {             String data = getNextValue(fileData, i);             double value = parseValue(data);             // What's the way to assign the 'double value' as x[i]?             if(x[0]){                                 y[0] = x[0] ; //how to read the first value 'x[0]' only?                         }                         else if(x[1]){                                 y[1] = 2*y[0] + x[1]; //how to read the value 'x[1]' only?                         }                         else{                                 y[i] = 2*y[i-1] - y[i-2] + x[i];                                 }         pw.close();                 System.out.print( "Output file at C drive now" + '\n');                 }```
Guide and Correct me, see I may do some mistakes. Thanks.
• 07-27-2008, 08:01 PM
Norm
Quote:

to assign the 'double value' as x[i]?
Do you mean: x[i] = value; // save value in current x
• 07-27-2008, 08:14 PM
matt_well
As I know the 'value' is already converted to a 'double value'.
Is it like this? Then, how to declare the x[i] and y[i]?

Code:

```        for(int i = 2; i < maxSize; i++)         {             String data = getNextValue(fileData, i);             double value = parseValue(data);             x[i] = value; ......```
• 07-28-2008, 02:42 AM
Norm
Are you asking how to create an array?

double[] x = new double[100]; // assume < 100 nbrs in file
double[] y = new double[100];
• 07-28-2008, 04:45 AM
matt_well
Quote:

Originally Posted by Norm
Are you asking how to create an array?

double[] x = new double[100]; // assume < 100 nbrs in file
double[] y = new double[100];

How to change the 100 to something like "buffer" as in future if the number in the text file is "unknown" or "to be greater than that"?
• 07-28-2008, 09:25 AM
Use an ArrayList. See the API docs.
• 07-28-2008, 03:32 PM
Norm
Is there a reason you need to save all the values of x and y?
What is supposed to be the output from the program?
You only need to save the last two values of y to compute the new y[i] value.
• 07-29-2008, 04:05 AM
matt_well
Quote: