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.
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.
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;
}
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]);
}
}
}
}
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();
}
}
}
Bookmarks