1   /**
2    * 183.592 Programmierpraxis TU Wien WS2014/15 H.Moritsch
3    * Plot einer Funktion y = f(x) mittels '*'-Zeichen 
4    * im Intervall von a bis b mit Schrittweite dx
5    * (mit lokalen Variablen)
6    */
7   public class FunktionPlotV2 {
8       public static void main(String[] args) {
9   
10          double a  = -2.0;
11          double b  = 3.0;
12          double dx = 0.25;
13  
14          double x;
15  
16          x = a;
17          // *solange* x innerhalb des Intervalls liegt:    
18          while (x <= b) {
19  
20              /*****/
21              double y = f(x);            // lokale Variable (while)
22              /*****/
23  
24              /*****/
25              double pos = 0.0;           // lokale Variable (while)
26              /*****/
27  
28              // *solange* pos "links" des Funktionswertes liegt:
29              while ( pos < y ) {
30  
31                  System.out.print(' ');
32  
33                  pos = pos + dx/2.0;     // nächster Positionsschritt
34                  }
35  
36              System.out.println('*');    // entspricht dem Funktionswert
37  
38              x = x + dx;                 // nächster x-Wert
39          }
40  
41      }
42  
43      /**
44      * Berechnung der Funktion -0.01*x^5 + 0.08*x^4 - 0.26*x^3 - 0.31*x^2 + 1.4*x + 2.36
45      */ 
46      static double f(double x) {
47          return -0.01*x*x*x*x*x + 0.08*x*x*x*x - 0.26*x*x*x - 0.31*x*x + 1.4*x + 2.36;
48      }
49  
50  }
51