Stuck at polynomial program


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 11 of 11

Thread: Stuck at polynomial program

  1. #1
    Join Date
    Nov 2005
    Posts
    6

    Stuck at polynomial program

    Hi, I am stuck at making a rational polynomial program. I have made a class to handle Rational numbers but at the second problem it asks to make a program for RationalPolynomials and I don't know how to start this. I don't understand the arrays much.

    Here I have my rational class: http://files.webhop.biz/Rational.txt

    And here the problem 2, its long so I make it an image: http://files.webhop.biz/RP_P2.jpg

    Can someone help me how to make this? I have to turn it in soon. Thanks for any help.

  2. #2
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560

    I have started it for you..

    This code has only implemented the rationalPolyAdd method, but the
    procedures for the other methods are quite similar.
    The package directives on top of each source file is just for ny own
    housekeeping, just delete them.

    Fair warning: I haven't debugged it
    Attached Files Attached Files
    eschew obfuscation

  3. #3
    Join Date
    Nov 2005
    Posts
    6
    Hi, thanks for the help. I have made the other functions Subtract, Multiply, Divide correct, I think. But now I don't know how should I make the toString method

    Can you help with this?

    My TA says it can be like this:

    public String toString(){
    String str = null;
    int i = coef.length; // but here, how to call the coefficient?
    while(i > 0){
    s += coef[i];
    s += " x^ ";
    s += i;
    s += " + ";
    i--;
    }
    return str;
    }

  4. #4
    Join Date
    Dec 2004
    Location
    San Bernardino County, California
    Posts
    1,468
    Its going to be troublesome to build a string that way.

    Try using StringBuilder class to add your elements, then return the result of the toString() method of that StringBuilder instance.

  5. #5
    Join Date
    Nov 2005
    Posts
    6
    We didn't learn StringBuilder. The exercise says to use toString so we have to use a toString.

  6. #6
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560
    You should make a toString for every class like:

    Code:
    // RationalPoly toString method
    public String toString () {
        String result="";
        for (int i=0; i<polynom.length; i++) {
          // start with minus if first term is neg. else blank
          if (i==0 && polynom[i].getCoefficient().getDoubleValue() < 0.0) {
            result += "-";
            result += polynom[i].toString();
          } else { // for rest of polynom include Term's Rational prefix
            result += polynom[i].getCoefficient().getPrefix();
            result += polynom[i].toString();
          }
        }
        return result;
      }
      // the Term toString method
      public String toString() {
        if (degree >= 0) {
          return coefficient.toString() + "X**" + Integer.toString(degree);
        } else { // parenthesis round neg. degree for clarity ?
          return coefficient.toString() + "X**(" + Integer.toString(degree)+")";
        }
      }
      // the Rational toString method
      public String toString() {
        if (degree >= 0) {
          return coefficient.toString() + "X**" + Integer.toString(degree);
        } else { // parenthesis round neg. degree for clarity ?
          return coefficient.toString() + "X**(" + Integer.toString(degree)+")";
        }
      }
      // the Rational prefix method
      public String getPrefix () {
        if (getDoubleValue () >= 0.0) {
          return "+";
        } else {
          return "-";
        }
      }
    Last edited by sjalle; 11-02-2005 at 05:45 PM. Reason: blunder
    eschew obfuscation

  7. #7
    Join Date
    Nov 2005
    Posts
    6
    Ok, I have a question on another exercise. It says

    Write a method with the signature public double reverseRows( double [][] array ) that takes as a parameter a two-dimensional double array, and returns a new two-dimensional double array constructed by “reversing” the entires in the rows of the array. For example, if the array referenced by the parameter is

    [0][1][2]
    [1][2][3]
    [2][3][4]
    [3][4][5]

    then the new array returned would look like this:
    [2][1][0]
    [3][2][1]
    [4][3][2]
    [5][4][3]
    So I have made the following code to test before making it just a method. But the problem is it doesn't sort properly and I don't know how to fix this.

    Code:
    public class reverseRows2 {
    	public static void main(String[] args) {
    	int[][] arr = new int[3][3];
    	arr[0][0] = 1;
    	arr[0][1] = 2;
    	arr[0][2] = 3;
    
    	arr[1][0] = 4;
    	arr[1][1] = 5;
    	arr[1][2] = 6;
    
    	arr[2][0] = 7;
    	arr[2][1] = 8;
    	arr[2][2] = 9;
    
    	int j, k;
    	for (j = 0; j <= arr.length-1; j++) {
    		for (k = 0; k <= arr[j].length-1; k++) {
    			arr[j][k] = arr[j].length-1;
    			System.out.println("arr[" + j + "][" + k + "]: " + arr[j][k]);
    		}
    	}
        }
    }

  8. #8
    Join Date
    Nov 2005
    Posts
    6
    Thanks for the reply, but I think its not working right.


    Here I make a class file and put the method there
    Code:
    public class reverse {
        public static double[][] reverseRows(double[][] input) {
            double[][] array = input;
            for (int j = 0; j < array.length; j++) {
                int shift = 0;
                for (int k = array[j].length-1; k >= 0; k--) {
                    array[j][k] = array[j][shift++];
                }
            }
            return array;
        }
    }
    And here is the driver to test the method
    Code:
    public class reverseDriver {
        public static void main(String[] args) {
    
            double[][] arr = {
                {1.0,2.0,3.0},
                {4.0,5.0,6.0},
                {7.0,8.0,9.0}
            };
    
    	reverse.reverseRows(arr);
    	int i, j;
    	for (i = 0; i <= arr.length-1; i++) {
    		for (j = 0; j < arr[i].length; j++) {
            			System.out.println("arr[" + i + "][" + j + "] :" + arr[i][j]);
    		}
    	}
        }
    }
    And then it outputs like this, but its not right
    Code:
    input[0][0] :1.0 // Should be 3.0
    input[0][1] :2.0
    input[0][2] :1.0
    input[1][0] :4.0 // Should be 6.0
    input[1][1] :5.0
    input[1][2] :4.0
    input[2][0] :7.0 // Should be 9.0
    input[2][1] :8.0
    input[2][2] :7.0
    [[D@18a992f
    I don't think my return statement is right also. How can I change it so to make it return the right output?
    Last edited by yeetso; 11-03-2005 at 12:24 PM.

  9. #9
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560

    I goofed

    The code i posted further up the thread has been deleted, here is
    the version that I have tested, I should have done that with the first one
    too, bad mistake.

    Code:
    public class ReverseRows {
      public double[][] reverseRows(double[][] input) {
        for (int j = 0; j < input.length; j++) {
          int shift = 0;
          for (int k = input[j].length - 1; k >= 0; k--) {
            if (k <= shift) break;
            double tmp=input[j][k];
            input[j][k] = input[j][shift];
            input[j][shift]=tmp;
            shift++;
          }
        }
        return input;
      }
      public static void main(String[] args) {
    
        double[][] arr = {
            {
            1.0, 2.0, 3.0}
            , {
            4.0, 5.0, 6.0, 66}
            , {
            7.0, 8.0, 9.0, 99}
        };
        ReverseRows rr=new ReverseRows();
        rr.dump(arr);
        rr.reverseRows(arr);
        rr.dump(arr);
    
    
      }
      private void dump(double[][] input) {
        for (int i=0; i<input.length; i++) {
          for (int j=0; j<input[i].length; j++) {
            System.out.print(input[i][j]+" ");
          }
           System.out.println();
        }
      }
    }
    eschew obfuscation

  10. #10
    Join Date
    Nov 2005
    Posts
    6
    Ah, thanks all the help! I made some small change to it so it works the way it should. But what is the reason of using dump?

  11. #11
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560
    The dump method prints any 2dim double matrix, it's for debug data dump..
    eschew obfuscation

Similar Threads

  1. problem in program in c++
    By mheasen in forum Architecture and Design
    Replies: 0
    Last Post: 03-20-2002, 09:24 AM
  2. Please help with running executable vb program.
    By Gordon Reichhardt in forum VB Classic
    Replies: 2
    Last Post: 01-08-2002, 10:06 AM
  3. How can I build copy protection into my program
    By W.Pierce in forum VB Classic
    Replies: 1
    Last Post: 12-11-2001, 08:28 AM
  4. The invisible program
    By James Abbott in forum VB Classic
    Replies: 0
    Last Post: 11-24-2001, 01:42 AM
  5. Replies: 0
    Last Post: 12-15-2000, 10:07 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
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center