Du verwendest einen veralteten Browser. Es ist möglich, dass diese oder andere Websites nicht korrekt angezeigt werden. Du solltest ein Upgrade durchführen oder ein alternativer Browser verwenden.
Hallo Jungs ich hab da nen kleines Problem mit meinen abstrakten Klassen und meinen Arrays!
Allgemeine Klasse:
Code:
abstract class Funktion {
double a;
double b;
double c;
double x;
public Funktion() {
}
public abstract double f();
}
Spezifizierte Klasse:
Code:
class Verkettung extends Funktion {
double a;
double b;
double c;
double x;
public Verkettung(double a, double b, double c, double x) {
this.a = a;
this.b = b;
this.c = c;
this.x = x;
}
public double f() {
return a /(a*x*x + b*x + c);
}
}
Klasse zum Ausführen:
Code:
import Prog1Tools.IOTools;
public class Eingabe {
static int ANZ_W = 2000;
static double wert = 0.0;
static Funktion[] fkt = new Funktion[1];
static double s = 0.1;
static Funktion[] tabelle = new Funktion[ANZ_W];
public static void main(String[] args) {
System.out.println("Geben Sie einen rationalen Wertebereich fuer die Funktion h(x): 1 / (1x^2 - 2x + 4)");
double xmin = IOTools.readInteger("Geben einen unteren x- Wert ein: ");
double xmax = IOTools.readInteger("Geben einen oberen x- Wert ein: ");
double x = xmin;
fkt[0] = new Verkettung(1.0,- 2.0, 4.0, x);
for (x = xmin; x < xmax ; xmin += s) {
for (int i = 0; i<= tabelle.length; ++i) {
tabelle[i] = fkt[0].f();
}
}
System.out.println("Der Funktionswert bei x=" + x + " betraegt:" + tabelle[] );
}
}
Mein Problem ist jetzt, dass der Compiler sagt: incompatible types, found double, requiered Funktion in Zeile:24!!
Allerdings greife ich doch mit der Punktnotation auf die abstrakte Methode in der Klasse Funktion zu oder nicht??
Das 2. Problem ist, dass er dann nicht die ausgerechneten Funktionswerte im Array tabelle[] speichert!!
Hoffe mir kann einer helfen, wär suuuuper net so auf nem Sonntag!!
Eigentlich würde ich jetzt sagen: Doch, das klappt, dann würdest du sagen: Nein, es klappt nicht. Dann würde ich mir den Code rauskopieren, das i<=tabelle.length ändern in i<tablelle.length, es compilieren und starten, und es würde funktionieren. (Vermutlich).
Füg' mal in deine Schleife eine kleine Ausgabe ein
Code:
for (int i = 0; i<tabelle.length; ++i) {
System.out.println("Zugriff auf "+i+" in Array mit Länge "+tabelle.length);
tabelle[i] = fkt[0].f();
}
Und wenn er dann immernoch eine unerklärliche ArrayIndexOutOfBoundsException wirft, oder ein anderer fehler auftritt, sag nochmal bescheid.
Abgesehen davon, dass die Schleifenkonstruktion an sich ziemlich sinnlos ist, müßte es in der äußeren Schleife nicht
xmin += s
sondern
x += s
heißen ... damit es funktioniert. (Schön ist das nicht, und Sinn macht es nicht, aber es funktioniert).
Und wenn er bei dem Code, der dann rauskommt (egal, ob mit dem xmin oder ohne) eine Exception wirft, ist dein Computer kaputt :roll:
Code:
abstract class Funktion {
double a;
double b;
double c;
double x;
public Funktion() {
}
public abstract double f();
}
class Verkettung extends Funktion {
double a;
double b;
double c;
double x;
public Verkettung(double a, double b, double c, double x) {
this.a = a;
this.b = b;
this.c = c;
this.x = x;
}
public double f() {
return a /(a*x*x + b*x + c);
}
}
public class Eingabe {
static int ANZ_W = 2000;
static double wert = 0.0;
static Funktion[] fkt = new Funktion[1];
static double s = 1.1;
static double[] tabelle = new double[ANZ_W];
public static void main(String[] args) {
double xmin = 0.1;
double xmax = 0.3;
double x = xmin;
fkt[0] = new Verkettung(1.0,- 2.0, 4.0, x);
for (x = xmin; x < xmax ; x += s) {
for (int i = 0; i< tabelle.length; ++i) {
System.out.println("Zugriff auf "+i+" in Array mit Länge "+tabelle.length);
tabelle[i] = fkt[0].f();
}
}
System.out.println("Der Funktionswert bei x=" + x + " betraegt:" + tabelle[0] );
}
}