Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

8 hours on 1 problem; can anyone help?

Status
Not open for further replies.

confission

Programmer
Sep 28, 2002
10
US
i'm trying to extend a class and i keep getting this error:

c:\codes\DiagonalToTridiagonal.java:7: cannot resolve symbol
symbol : constructor DiagonalMatrix ()
location: class dataStructures.DiagonalMatrix
public class DiagonalToTridiagonal extends DiagonalMatrix

the DiagonalMatrix class file is in the dataStructures folder, and i don't understand why there would be an error, or what the error is. when i extend from other classes, such as ArrayLinearList, i don't get the error. i seem to only get the error when i extend from a class that isn't implemented from an interface. so how do i extend from a class which isn't implemented from an interface? how can i extend DiagonalMatrix with DiagonalToTridiagonal without getting an error? i would greatly appreciate it if anyone can help; this problem is driving me crazy.

the following is my code:

package dataStructures;
import java.util.*;
import utilities.*;



public class DiagonalToTridiagonal extends DiagonalMatrix
{
public TridiagonalMatrix transformToTridiagonal()
{
TridiagonalMatrix MyTridiagonalMatrix=new TridiagonalMatrix(this.rows, this.element[rows-1]);
for(int i=0; i<this.rows; i=i++)
{
MyTridiagonalMatrix.element[rows-1+i]=this.element;
MyTridiagonalMatrix.element[2*rows-1+i]=this.element;
Computable x=(Computable) this.element;
MyTridiagonalMatrix.element=x.add(this.element[i+1]);
}
MyTridiagonalMatrix.element[2*rows-2]=this.element[rows-1];
return MyTridiagonalMatrix;
}
}

the following is the diagonal matrix code:

/** diagonal matrix mapped into a one-dimensional array */

package dataStructures;

import java.util.*;
import utilities.*;
import wrappers.*;

public class DiagonalMatrix
{
// data members
int rows; // matrix dimension
Object zero; // zero element
Object [] element; // element array

// constructor
/** @throws IllegalArgumentException when theRows < 1 */
public DiagonalMatrix(int theRows, Object theZero)
{
// validate theRows
if (theRows < 1)
throw new IllegalArgumentException(&quot;number of rows must be > 0&quot;);

// create and initialize the matrix
rows = theRows;
zero = theZero;
element = new Object [rows];
for (int i = 0; i < rows; i++)
element = zero;
}

// methods
/** @throws IndexOutOfBoundsException when i < 1
* or j < 1 or i > rows or j > rows */
void checkIndex(int i, int j)
{
if (i < 1 || j < 1 || i > rows || j > rows)
throw new IndexOutOfBoundsException
(&quot;i = &quot; + i + &quot; j = &quot; + j +
&quot; rows = &quot; + rows + &quot; cols = &quot; + rows);
}

/** @return the element this(i,j)
* @throws IndexOutOfBoundsException when i < 1
* or j < 1 or i > rows or j > rows */
public Object get(int i, int j)
{
checkIndex(i, j);

// determine element to return
if (i == j)
return element[i-1]; // diagonal element
else
return zero; // nondiagonal element
}

/** set this(i,j) = newValue
* @throws IndexOutOfBoundsException when i < 1
* or j < 1 or i > rows or j > rows
* @throws IllegalArgumentException when you try
* to set a nondiagonal element to nonzero */
public void set(int i, int j, Object newValue)
{
checkIndex(i, j);

if (i == j)
// save the diagonal element
element[i - 1] = newValue;
else
// nondiagonal element, newValue must be zero
if (!((Zero) newValue).equalsZero())
throw new IllegalArgumentException
(&quot;nondiagonal elements must be zero&quot;);
}


/** test program */
public static void main(String [] args)
{
// create the matrix
DiagonalMatrix x = new DiagonalMatrix(20, new MyInteger(0));

// set a few elements
x.set(1, 1, new MyInteger(22));
x.set(5, 5, new MyInteger(44));
x.set(8, 5, new MyInteger(0));

// retrieve a few elements
System.out.println(x.get(5,5));
System.out.println(x.get(1,1));
System.out.println(x.get(10,1));
}
}


 
You're trying to extend the DiagonalMatrix class with the DiagonalToTridiagonal class. So far, so good. But the DiagonalMatrix class has a constructor
Code:
DiagonalMatrix(int theRows, Object theZero).
Your class, however, has no constructor at all. When an extending class has no constructor,
Code:
super()
is called. This means that the constructor of the super class is called without arguments. Since DiagonalMatrix only has a constructor which takes two arguments, you should add a constructor to your extending class DiagonalToTridiagonal like this:

Code:
public class DiagonalToTridiagonal extends DiagonalMatrix
{
    public DiagonalToTridiagonal(int theRows, Object theZero)
    {
        super(theRows, theZero);
    }
}

This way, the DiagonalMatrix constructor will be called with the right amount of arguments.

So basically, Java tries to call a no-arg constructor in the DiagonalMatrix class, which doesn't exist.

I hope this'll help you out!

regards,
Blaxo
 
thank you so much! that totally makes sense. thanks for explaining it so well
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top