Stuck at polynomial program

 DevX Home Today's Headlines   Articles Archive   Tip Bank   Forums

# Thread: Stuck at polynomial program

1. Registered User
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. ## 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

3. Registered User
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. Senior Member
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. Registered User
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. 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++) {
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

7. Registered User
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. Registered User
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. ## 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

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();
}
}
}```

10. Registered User
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. The dump method prints any 2dim double matrix, it's for debug data dump..

#### Posting Permissions

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

 FAQ Latest Articles Java .NET XML Database Enterprise