P
Problemkind713
Gast
Ich habe folgendes Problem:
Muss ein Programm mit Polynomen schreiben.
dass folgende Methoden beinhalten soll:
Also, momentan kompiliert mein Programm, genauso wie mein Testprogramm
Doch sobald ich starte bekomm ich bei der degree sofort ne NullPointerException
Ich kopier euch mal schnell meinen Konstruktor und die degree rein .... bitte um schnelle hilfe und danke im voraus ^^
Muss ein Programm mit Polynomen schreiben.
dass folgende Methoden beinhalten soll:
Java:
public Polynom(double[] coeffs) //Konstruktor
public double eval(double x) //Wert an der Stelle x
public int degree() //hoechster Exponent (mit Koeffizient != 0)
public void display() //Ausgabe in ueblicher mathem. Form (s.o.)
public Polynom diff() //1.Ableitung (symbolisch)
public Polynom mul(Polynom p) //liefert this * p; requires p!=null
Also, momentan kompiliert mein Programm, genauso wie mein Testprogramm
Doch sobald ich starte bekomm ich bei der degree sofort ne NullPointerException
Ich kopier euch mal schnell meinen Konstruktor und die degree rein .... bitte um schnelle hilfe und danke im voraus ^^
Java:
class Polynom {
private double coeffs[];
private int x;
private double helper;
//----------------------------------------
public Polynom(double[] coeffs) { // Konstruktor
System.out.println ("Folgendes Polynom wurde erstellt:");
x = coeffs.length -1;
while (x>1){
if (coeffs [x] > 0) {
System.out.print ("+" + coeffs [x] + "x^" + x);
x--;
}else if (coeffs [x] < 0) {
System.out.print ("-" + coeffs [x] + "x^" + x);
x--;
}else {
x--;
}
}
if (x == 1) {
if (coeffs [x] > 0) {
System.out.print ("+" + coeffs [x] + "x");
x--;
}else if (coeffs [x] < 0) {
System.out.print ("-" + coeffs [x] + "x");
x--;
}else {
x--;
}
}
if (x == 0) {
if (coeffs [x] > 0) {
System.out.print ("+" + coeffs [x]);
}else if (coeffs [x] < 0) {
System.out.print ("-" + coeffs [x]);
}else {
}
System.out.println ();
System.out.println ();
}
}
public double eval (int x) {
System.out.println ( coeffs [x-1]);
return coeffs [x-1] ;
}
public int degree (){
x = 1;
while(coeffs [coeffs.length - x] ==0){
x++;
}
return coeffs.length - x;
}
}
Zuletzt bearbeitet von einem Moderator: