Problems with arrays
Hello,
I have got problems with giving out an array.
I declared two arrays and puted them out, but with the
resultarray it doesn?t work. Cansomebody help me?
Here is a runtime output:
Grad eingeben!
1
Koeffizienten eingeben!
1
2
1x^0 + 2x^1 +
1
Grad eingeben!
3
Koeffizienten eingeben!
2
3
4
5
2x^0 + 3x^1 + 4x^2 + 5x^3 +
3
p:1
this:3
And here is the source code
[Code]
// Die Klasse Polynom dient dem Umgang mit dem Polynom
//
//Sie enth齦lt die Methoden:
//> public void readCoeffizients()
//> public void show()
//> public int getGrad()
//> public Polynom add(Polynom p)
//Und den Konstruktor:
//> public Polynom(int n)
public class Polynom
{
private int grad;
private int [] coeffizient;
private Polynom q1;
public Polynom(int n) //Konstruktor mit Stanard-Initialisierung
{
this.grad = -1;
this.coeffizient = new int [n+1];
for(int i=1; i<(n+1); i++)
{
this.coeffizient = 0;
}
}
public void readCoeffizients() // Methode zum Einlesen der Koeffizienten
{
this.grad = (coeffizient.length)-1;
System.out.println("Koeffizienten eingeben!");
for(int i=0; i<(grad+1);i++)
{
this.coeffizient = Eingabe.intValue();
}
}
public void show() // Methode zum Ausgeben des Polynoms
{
for (int i=0; i<(this.getGrad()+1);i++)
{
System.out.print(coeffizient + "x^" + i + " + ");
}
System.out.println();
}
public int getGrad()
{
return this.grad;
}
public Polynom add(Polynom p) // Methode zum Addieren zweier Polynome
{
System.out.println("\np:" + p.getGrad());
System.out.println("this:" + this.getGrad());
if(p.getGrad() >= this.getGrad()) // Vorgehen falls p.getGrad() grer als this.getGrad()
{
q1 = new Polynom(p.getGrad());
grad = p.getGrad();
for(int i=0; i<(this.getGrad()+1);i++)
{
q1.coeffizient = this.coeffizient + p.coeffizient;
}
for(int i=(this.getGrad()+1);i<(p.getGrad()+1);i++)
{
q1.coeffizient = p.coeffizient;
}
}
else // Vorgehen falls this.getGrad() grer als p.getGrad()
{
q1 = new Polynom(this.getGrad());
grad = this.getGrad();
for(int i=0; i<(p.getGrad()+1);i++)
{
q1.coeffizient = this.coeffizient + p.coeffizient;
}
for(int i=(p.getGrad()+1);i<(this.getGrad()+1);i++)
{
q1.coeffizient = this.coeffizient;
}
}
return q1; // R齝kgabe des Ergebnisspolynoms
}
}
// Diese Klasse testet die Klasse Polynom
public class Test
{
public static void main(String[] args)
{
System.out.println("Grad eingeben!");
Polynom p1 = new Polynom(Eingabe.intValue());
p1.readCoeffizients();
p1.show();
System.out.println(p1.getGrad());
System.out.println("Grad eingeben!");
Polynom p2 = new Polynom(Eingabe.intValue());
p2.readCoeffizients();
p2.show();
System.out.println(p2.getGrad());
//Polynom p3 = (p2.add(p1));
//p3.show();
(p2.add(p1)).show();
}
}
[/Code]
Thanks for your help!

