Page 1 of 2 12 LastLast
Results 1 to 20 of 25
  1. #1
    matt_well's Avatar
    matt_well is offline Member
    Join Date
    Jul 2008
    Posts
    59
    Rep Power
    0

    Question 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:
    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"
    Attached Files Attached Files
    Last edited by matt_well; 07-27-2008 at 09:42 AM. Reason: Attached textfile

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default

    Question about the application/program.
    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.
    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
    read number
    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.
    Last edited by Norm; 07-26-2008 at 08:55 PM.

  3. #3
    matt_well's Avatar
    matt_well is offline Member
    Join Date
    Jul 2008
    Posts
    59
    Rep Power
    0

    Default

    Quote Originally Posted by Norm View Post
    Question about the application/program.
    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,

    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.

  4. #4
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    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.

  5. #5
    matt_well's Avatar
    matt_well is offline Member
    Join Date
    Jul 2008
    Posts
    59
    Rep Power
    0

    Default

    Quote Originally Posted by masijade View Post
    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.

  6. #6
    matt_well's Avatar
    matt_well is offline Member
    Join Date
    Jul 2008
    Posts
    59
    Rep Power
    0

    Default

    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.

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default

    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.
    Java 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.
    Last edited by Norm; 07-26-2008 at 11:49 PM.

  8. #8
    matt_well's Avatar
    matt_well is offline Member
    Join Date
    Jul 2008
    Posts
    59
    Rep Power
    0

    Default

    Thanks Norm for some guides. Here's the current doing, but cannot run yet.

    Java 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..
    Java 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.

  9. #9
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default

    Please reread my last about a waste of time.

  10. #10
    matt_well's Avatar
    matt_well is offline Member
    Join Date
    Jul 2008
    Posts
    59
    Rep Power
    0

    Default

    Still interest in doing, can you give me some guide please how this work?
    Java Code:
        if first line
            y = value;
        else
            y = func(y, x, oldy)

  11. #11
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    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.

  12. #12
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default

    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.

  13. #13
    matt_well's Avatar
    matt_well is offline Member
    Join Date
    Jul 2008
    Posts
    59
    Rep Power
    0

    Default

    Hi, here's some doing ideas.

    Java 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.
    Last edited by matt_well; 07-27-2008 at 08:46 PM.

  14. #14
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default

    to assign the 'double value' as x[i]?
    Do you mean: x[i] = value; // save value in current x

  15. #15
    matt_well's Avatar
    matt_well is offline Member
    Join Date
    Jul 2008
    Posts
    59
    Rep Power
    0

    Default

    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]?

    Java Code:
            for(int i = 2; i < maxSize; i++)
            {
                String data = getNextValue(fileData, i);
                double value = parseValue(data);
                x[i] = value;
    ......

  16. #16
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default

    Are you asking how to create an array?

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

  17. #17
    matt_well's Avatar
    matt_well is offline Member
    Join Date
    Jul 2008
    Posts
    59
    Rep Power
    0

    Default

    Quote Originally Posted by Norm View Post
    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"?

  18. #18
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    Use an ArrayList. See the API docs.

  19. #19
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default

    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.

  20. #20
    matt_well's Avatar
    matt_well is offline Member
    Join Date
    Jul 2008
    Posts
    59
    Rep Power
    0

    Default

    Quote Originally Posted by masijade View Post
    Use an ArrayList. See the API docs.
    Yes, I know that. How to use it here the double value?

Page 1 of 2 12 LastLast

Similar Threads

  1. Replies: 0
    Last Post: 07-01-2008, 10:06 PM
  2. Replies: 9
    Last Post: 06-25-2008, 05:48 AM
  3. Is it possible to make a Phone call program using java?
    By fireball2008 in forum New To Java
    Replies: 2
    Last Post: 05-08-2008, 07:20 PM
  4. Java Question
    By Jay-1.1 in forum New To Java
    Replies: 11
    Last Post: 05-01-2008, 05:04 PM
  5. Make sound play in a java application
    By lenny in forum AWT / Swing
    Replies: 2
    Last Post: 08-13-2007, 12:45 PM

Posting Permissions

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