| FunktionMethode.java |
1 /**
2 * 183.592 Programmierpraxis TU Wien WS2014/15 H.Moritsch
3 * Ausgabe der Werte einer Funktion y = f(x) im Intervall von a bis b
4 * mit einer bestimmten Schrittweite dx
5 * Berechnung des Funktionswertes in einer Methode
6 */
7 public class FunktionMethode {
8 public static void main(String[] args) {
9
10 double a = -5.0;
11 double b = 5.0;
12 double dx = 1.0;
13
14 double x; // Argument der Funktion
15 double y; // Funktionswert
16
17 // Vorbereitung der Schleife
18
19 x = a; // Beginn des Intervalls
20
21 // *solange* x noch innerhalb des Intervalls liegt:
22
23 while (x <= b) {
24
25 y = f(x); // Berechnung des Funktionswertes
26
27 System.out.println(y);
28
29 x = x + dx; // x wird um die Schrittweite erhöht
30 }
31
32 }
33
34 /**
35 * Berechnung des Werte der Funktion y(x) = x*x-0.5*x+2
36 */
37 private static double f(double wert) {
38 double ergebnis;
39
40 ergebnis = wert * wert - 0.5 * wert + 2.0;
41
42 return ergebnis;
43 }
44
45 }
46