Java programmieren: Polynom Klasse

kanywayne

Mitglied
Hallo Leute,
ich hab eine Polynom Klasse erstellt und soll zudem noch die add, sub , Ableitung ect... erstellen.


Java:
public class Polynomial {

private int grad;
private double[] coeffizients;

// Konstruktor : a*x^n
public Polynomial(double a, int n) {
this.grad = n;
this.coeffizients = new double[grad + 1];
this.coeffizients[n] = a; // p_n = a, p_n =0, ..., P_1 = 0, P_0 =0

}

public Polynomial(double... coeffizients) {
if (coeffizients == null) {
this.grad = 0;
this.coeffizients = new double[1];
} else {
this.grad = coeffizients.length - 1;
this.coeffizients = coeffizients;
}
}

@Override
public String toString() {

StringBuilder sb = new StringBuilder();
for (int i = grad; i > 0; i--) {
String coeffizient = null;
if (i == grad)
coeffizient = String.format("%.4f", coeffizients);
else
coeffizient = String.format("%.4f", Math.abs(coeffizients));
if (i == 1)
sb.append(coeffizient).append(" x ");
else
sb.append(coeffizient).append(" x^").append(i);
sb.append(getSign(coeffizients[i - 1]));
}
String coeffizient = String.format("%.4f", Math.abs(coeffizients[0]));
sb.append(coeffizient);
return sb.toString();
}

private String getSign(double x) {
if (x >= 0)
return " + ";
else
return " - ";

}

public double evaluation(double x) {
double value = coeffizients[grad]; // b_i= a_i + b_1*x
for (int i = grad; i > 0; i--) {
value = coeffizients[i - 1] + x * value;
}
return value;
}

public Polynomial composition(Polynomial q) {
return null;
}

private Polynomial plus(double v) {
double[] coeffizientSum = new double[grad + 1];
for (int i = 0; i <= grad; i++)
coeffizientSum = coeffizients;
coeffizientSum[0] += v;
return new Polynomial(coeffizientSum);
}

private Polynomial plus(Polynomial q) { //this + q(x)
int m = Math.max(this.grad, q.grad);
double[] coeffizientSum = new double[m + 1];
for (int i = 0; i <= m; i++) {
if (i > q.grad && i <= this.grad)
coeffizientSum = this.coeffizients;
else if (i > this.grad && i <= q.grad)
coeffizientSum = q.coeffizients;
else
coeffizientSum = this.coeffizients + q.coeffizients;
}
return new Polynomial(coeffizientSum);
}

public Polynomial add(double v) {
return this.plus(v);
}

public Polynomial addRev(double v) {
return this.plus(v);
}

public Polynomial add(Polynomial v) {
return this.plus(v);
}

public Polynomial addRev(Polynomial v) {
return this.plus(v);
}

public static void main(String[] args) {

double[] coeffizients = { -1, -2, -3, 4 };
Polynomial p1 = new Polynomial(coeffizients); //läuft
System.out.println(p1);

Polynomial p2 = new Polynomial(-1, -1, 0, 1); //läuft
System.out.println("p2 =" + p2);

Polynomial p3 = new Polynomial(1, 2, 3, 4); //läuft
System.out.println("p3 =" + p3);

Polynomial p4 = p2 + p3;          //[[[[[ Exception in thread "main" java.lang.Error: Unresolved compilation problem:
The operator + is undefined for the argument type(s) Polynomial, Polynomial]]]]]
System.out.println("p4 =" + p4);
}
}

Wäre dankbar für jede hilfe !!
 
Zuletzt bearbeitet:
Bitte verwende die Code-Tags: Das erste Symbol im Editor </>.

Erstens wird es so besser lesbar und zweitens werden sonst manche Zeichen falsch interpretiert und nicht angezeigt.

Du kannst deinen Beitrag auch editieren.
 

Zurück
Oben