Hallo erstmal. Ich stell mich kurz vor: Ich bin Student der Naturwissenschaften und hab das Pflichtmodul Computerorientierte Mathematik. Und bei der dritten Hausaufgabe hab ich mein ersten Problem.
Wir sollen ein Apllet schreiben, dass eine Exponentialfunktion beschreibt.
folgendes hab ich schon:
Mein Problem besteht nun darin, dass ich bei dem Exponenten unter 0 festhänge. Das Applet startet zwar aber bei der Eingabe -1 als x(Exponent) hängt es sich auf. Warum?
Wir sollen ein Apllet schreiben, dass eine Exponentialfunktion beschreibt.
folgendes hab ich schon:
Java:
/**
* Exponential.java
* input: a real x and one of (accurancy | sum limit)
* output: an approximation of exp(x)
*
* method: two methods are implemented. the first sums the approximation sum up
* to the given number, the other tries to reach a given accurancy.
*
* Created: Oct 20, 2009,5:12:10 PM
*/
import java.applet.Applet;
import java.awt.Font;
import java.awt.Label;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
/**
* Calculates the exponential function exp(x) using an approximation by the
* formula sum_{i=1}^n x^i/i! for n to infinity. It can handle both, fixed n
* and approximation to a given accurancy.
*/
public class ExponentialSkeleton extends Applet {
/** Labels describing Text in the user interface. */
Label lblDescription;
Label lblSumLimit;
Label lblAccurancy;
Label lblxValue;
Label lblOutput;
Label lblOutputIterations;
/** Textfields used for in and output. */
TextField output;
TextField outputIterations;
TextField inputSumLimit;
TextField inputAccurency;
TextField inputxValue;
/** The number of iterations used by approximation method. */
int iterations;
double base;
/**
* Set up the graphical user interface components, initialize labels
* and textfields with text.
*/
public void init() {
// define font and applet size
setFont( new Font( "Times", Font.PLAIN, 24 ) );
setSize( 980, 180 );
// define labels and textfields
lblDescription = new Label( "Geben sie die Summen-Laufweite und einen Exponenten bzw. x-Wert an:" );
lblSumLimit = new Label( "Summe bis: " );
lblxValue = new Label( "x-Wert: " );
lblOutput = new Label( "Ergebnis: " );
output = new TextField( 10 );
inputSumLimit = new TextField( 10 );
inputSumLimit.addActionListener( new ActionListener() {
public void actionPerformed( ActionEvent e ) {
action();
}
} );
inputxValue = new TextField( 10 );
inputxValue.addActionListener( new ActionListener() {
public void actionPerformed( ActionEvent e ) {
action();
}
} );
output.setEnabled( false );
// add labels and textfields to the applet
add( lblDescription );
add( lblSumLimit );
add( inputSumLimit );
add( lblxValue );
add( inputxValue );
add( lblOutput );
add( output );
}
/**
* Performs a calculateion of exp(x) with fixed sum limits. The values are
* read from the text fields and after the computation written to the output
* fields.
*/
private void action() {
double result = power( Double.parseDouble(inputSumLimit.getText()) , Integer.parseInt(inputxValue.getText()) );
output.setText( Double.toString(result));
}
/**
* Performs a calculation of exp(x) with given accurancy. The values are read
* from the text fields and after the computation written to the output
* fields.
*/
private void actionApproximate() {
output.setText( "Ergebnis." );
}
public double power(double base, int exp){
double basestart = base; // Basestart speichert die eingegebene Zahl
if(exp == 0) // wenn Exponent NULL, dann halt Rückgabe 1
return 1;
else if(exp == 1) //wenn Exponent 1, dann halt Rückgabe der BASE
return base;
else if (exp > 1) { // wenn Exponent größer 1 dann führ er die Schleife aus
while (exp > 1) { // Schleife wird solange Ausgeführt wie der Exponent größer 1 ist
base = base * basestart; // BASE wird um BASE * BASESTART erhöht. BASE ist bei 2 dann z.B. 4. Beim nächsten Durchlauf halt 4(BASE) * 2(BASESTART) usw.
exp--; // Exponent wird um 1 verkleinert
}
return base; // Rückgabe der BASE wenn Exponent <= 1
}
else while (exp < 0) {
base = base / powerrun(basestart, exp);
}
return base;
}
private double powerrun(double base3, int exp) {
double base2 = base3;
while (exp < 0) {
base3 = base3 * base2;
exp++;
}
return base3;
}
}
Mein Problem besteht nun darin, dass ich bei dem Exponenten unter 0 festhänge. Das Applet startet zwar aber bei der Eingabe -1 als x(Exponent) hängt es sich auf. Warum?