DVG1 - Modulare Programmierung 1 Modulare Programmierung.

43
DVG1 - Modulare Programm ierung 1 Modulare Programmierung

Transcript of DVG1 - Modulare Programmierung 1 Modulare Programmierung.

Page 1: DVG1 - Modulare Programmierung 1 Modulare Programmierung.

DVG1 - Modulare Programmierung

1

Modulare Programmierung

Page 2: DVG1 - Modulare Programmierung 1 Modulare Programmierung.

DVG1 - Modulare Programmierung

2

BeispielSchreiben Sie ein Programm, das eine Tabelle erzeugt, die für i=1,7,49,...<1000000000 die Werte i, sqrt(i) und (double)i*i enthält.

i | sqrt(i) | i*i-------------+---------------------+-------------------- 1 | 1.000000000000E+00 | 1.000000000000E+00 7 | 2.645751311064E+00 | 4.900000000000E+01 49 | 7.000000000000E+00 | 2.401000000000E+03 343 | 1.852025917745E+01 | 1.176490000000E+05 2401 | 4.900000000000E+01 | 5.764801000000E+06 16807 | 1.296418142421E+02 | 2.824752490000E+08 117649 | 3.430000000000E+02 | 1.384128720100E+10 823543 | 9.074926996951E+02 | 6.782230728490E+11 5764801 | 2.401000000000E+03 | 3.323293056960E+13 40353607 | 6.352448897866E+03 | 1.628413597910E+15 282475249 | 1.680700000000E+04 | 7.979226629761E+16

Page 3: DVG1 - Modulare Programmierung 1 Modulare Programmierung.

DVG1 - Modulare Programmierung

3

0.te Näherung

public class t1

{

public static void main(String [] args)

{

System.out.println(

"\n i | sqrt(i) | i*i");

System.out.println(

"-------------+---------------------+--------------------");

for (long i=1;i<1000000000;i*=7)

System.out.println(i+" | "+Math.sqrt(i)+" | "+(double)i*i);

}

}

Page 4: DVG1 - Modulare Programmierung 1 Modulare Programmierung.

DVG1 - Modulare Programmierung

4

Ergebnis der 0.ten Näherung

i | sqrt(i) | i*i-------------+---------------------+--------------------1 | 1.0 | 1.07 | 2.6457513110645907 | 49.049 | 7.0 | 2401.0343 | 18.520259177452136 | 117649.02401 | 49.0 | 5764801.016807 | 129.64181424216494 | 2.82475249E8117649 | 343.0 | 1.3841287201E10823543 | 907.4926996951546 | 6.78223072849E115764801 | 2401.0 | 3.3232930569601E1340353607 | 6352.448897866082 | 1.628413597910449E15282475249 | 16807.0 | 7.9792266297612E16

Page 5: DVG1 - Modulare Programmierung 1 Modulare Programmierung.

DVG1 - Modulare Programmierung

5

1.te Näherung

public class t1

{

public static void main(String [] args)

{

System.out.println(

"\n i | sqrt(i) | i*i");

System.out.println(

"-------------+---------------------+--------------------");

for (long i=1;i<1000000000;i*=7)

System.out.println(i+"\t| "+Math.sqrt(i)+"\t| "+(double)i*i);

}

}

Page 6: DVG1 - Modulare Programmierung 1 Modulare Programmierung.

DVG1 - Modulare Programmierung

6

Ergebnis der 1.ten Näherung

i | sqrt(i) | i*i-------------+---------------------+--------------------1 | 1.0 | 1.07 | 2.6457513110645907 | 49.049 | 7.0 | 2401.0343 | 18.520259177452136 | 117649.02401 | 49.0 | 5764801.016807 | 129.64181424216494 | 2.82475249E8117649 | 343.0 | 1.3841287201E10823543 | 907.4926996951546 | 6.78223072849E115764801 | 2401.0 | 3.3232930569601E1340353607 | 6352.448897866082 | 1.628413597910449E15282475249 | 16807.0 | 7.9792266297612E16

Page 7: DVG1 - Modulare Programmierung 1 Modulare Programmierung.

DVG1 - Modulare Programmierung

7

Probleme Die Wirkung von Tabulatoren ist nicht vollständig durch das

Programm beeinflußbar. Zahlen sollten rechtsbündig ausgegeben werden. Das Format von Gleitkommazahlen hängt sehr von dem Wert ab und

ist somit nicht vorhersehbar.– Anzahl der Stellen– Exponentialdarstellung– Vorzeichen

Fazit: formatgesteuerte Ausgabe fehlt in JAVA!– Z.B. Pascal: write(i:8,x:8:2);

Page 8: DVG1 - Modulare Programmierung 1 Modulare Programmierung.

DVG1 - Modulare Programmierung

8

Formatierung von Festkommazahlen

Eingabe: Festkommazahl (byte, short, int, long)– Alle Festkommatypen werden automatisch nach long konvertiert.

==> Wir wählen long als Eingabetyp: long z Ausgabe: Zeichenkette, die den Wert der Eingabezahl repräsentiert. Format der Ausgabe: <bl>...< bl><vz><zn>...<z0>

– <bl>: Leerzeichen– <vz>: Vorzeichen = ' ' oder '-'– <zi>: Ziffern

zusätzliche Eingabe:– Länge der Ausgabe: int l

Page 9: DVG1 - Modulare Programmierung 1 Modulare Programmierung.

DVG1 - Modulare Programmierung

9

Probleme:– Was tun, wenn falsche Parameter eingegeben werden?

==> einzige Möglichkeit für Fehler ist l<=0 ==> null ausgeben

– Was tun, wenn sich die Zahl nicht mit l Stellen darstellen läßt? z.B.: l=3 und z=-12345 ==> Zeichenkette "**...*" mit l Sternen ausgeben

Page 10: DVG1 - Modulare Programmierung 1 Modulare Programmierung.

DVG1 - Modulare Programmierung

10

Vorhandene Lösungen untersuchen In JAVA existiert die Methode Long.toString

Aus der JAVA-Doku:

public static String toString(long i)– Returns a new String object representing the specified integer. The

argument is converted to signed decimal representation and returned as a string, exactly as if the argument and the radix 10 were given as arguments to the toString method that takes two arguments.

Parameters: – i - a long to be converted.

Returns: – a string representation of the argument in base 10.

Page 11: DVG1 - Modulare Programmierung 1 Modulare Programmierung.

DVG1 - Modulare Programmierung

11

public static String toString(long i, int radix)

Creates a string representation of the first argument in the radix specified by the second argument. ... If the first argument is negative, the first element of the result is the ASCII minus sign '-' ('\u002d'. If the first argument is not negative, no sign character appears in the result. The remaining characters of the result represent the magnitude of the first argument. If the magnitude is zero, it is represented by a single zero character '0' ('\u0030'); otherwise, the first character of the representation of the magnitude will not be the zero character....

Parameters: i - a long. radix - the radix.

Returns: a string representation of the argument in the specified radix.

Page 12: DVG1 - Modulare Programmierung 1 Modulare Programmierung.

DVG1 - Modulare Programmierung

12

Darstellung ist o.k. Wenn die Zahl zu klein ist, werden zu wenig Stellen ausgegeben. Wenn Zahl zu groß ist, werden zu viel Stellen ausgegeben.

==>

Long.toString kann verwendet werden. Das Resultat muß aber modifiziert werden.

Page 13: DVG1 - Modulare Programmierung 1 Modulare Programmierung.

DVG1 - Modulare Programmierung

13

return s;

String s;Berechnung der

Ausgabe

false

return null;true

l <= 0

Blockdiagramm - 1.Versionlong z, int l

Page 14: DVG1 - Modulare Programmierung 1 Modulare Programmierung.

DVG1 - Modulare Programmierung

14

Methode - 1.Version

public static String toString (long z, int l)

{

if ( l <= 0 ) return null;

String s;

// Berechnung der Ausgabezeichenkette

return s;

}

Page 15: DVG1 - Modulare Programmierung 1 Modulare Programmierung.

DVG1 - Modulare Programmierung

15

false

return s;

Fehlerbehandlung

>l

links auffüllen

<l

return null;true

l <= 0

Blockdiagramm - 2.Version

String s = Long.toString(z);

s.length()

=l

long z, int l

Page 16: DVG1 - Modulare Programmierung 1 Modulare Programmierung.

DVG1 - Modulare Programmierung

16

Methode - 2.Versionpublic static String toString (long z, int l)

{

if ( l <= 0 ) return null;

String s = Long.toString(z);

if ( s.length() == l ) return s;

if ( s.length() < l)

{

// links auffuellen

return s;

}

// Fehlerbehandlung

return s;

}

Page 17: DVG1 - Modulare Programmierung 1 Modulare Programmierung.

DVG1 - Modulare Programmierung

17return s;

String s = Long.toString(z);

s.length()

=l

>l<l

return null;true

false

l <= 0

long z, int l

Blockdiagramm - 3.Version

trues.length()<l

false s='*'+s;

s.length()<l

true

falses=' '+s;

s="*";

Page 18: DVG1 - Modulare Programmierung 1 Modulare Programmierung.

DVG1 - Modulare Programmierung

18

Methode - 3.Versionpublic static String toString (long z, int l){

if ( l <= 0 ) return null;String s = Long.toString(z);if ( s.length() == l ) return s;if ( s.length() < l){

do{

s = ' '+s;} while ( s.length()<l);return s;

}s = "*";while ( s.length() < l){

s = '*' + s;}return s;

}

Page 19: DVG1 - Modulare Programmierung 1 Modulare Programmierung.

DVG1 - Modulare Programmierung

19

Methode - 1.optimierte Versionpublic static String toString (long z, int l){

if ( l <= 0 ) return null;String s = Long.toString(z);while ( s.length() < l){

s = ' '+s;}if ( s.length() == l ) return s;s = "*";while ( s.length() < l){

s = '*' + s;}return s;

}

Page 20: DVG1 - Modulare Programmierung 1 Modulare Programmierung.

DVG1 - Modulare Programmierung

20

Methode - 2.optimierte Versionpublic static String toString (long z, int l){

if ( l <= 0 ) return null;String s = Long.toString(z);char fz = ' ';if ( s.length() > l){

fz= '*' ;s = "*";

}while ( s.length() < l){

s = fz+s;}return s;

}

Page 21: DVG1 - Modulare Programmierung 1 Modulare Programmierung.

DVG1 - Modulare Programmierung

21

Methode - endgültige Version/*** <EM>toString</EM> konvertiert eine <CODE>long</CODE>-Variable* in eine Zeichenkette und f&uuml;llt mit Leerzeichen bis zur * L&auml;nge <CODE>l</CODE> auf. Die Gesamtl&auml;nge enth&auml;lt* das Vorzeichen "-" f&uuml;r negative Zahlen. <BR>* Falls die Zahl nicht in dem Format darstellbar ist, * wird die Zeichenkette "**...*" ausgegeben. <BR>* Wird <CODE>l&lt;=0</CODE> angegeben, so wird <CODE>null</CODE>* zur&uuml;ckgegeben.* @author Gerhard Telschow* @version 1.0* @param z auszugebende Variable* @param l L&auml;nge der Ausgabe* @return Zeichenkette*/

Page 22: DVG1 - Modulare Programmierung 1 Modulare Programmierung.

DVG1 - Modulare Programmierung

22

public static String toString(long z, int l){// Falls l<= null zurueckgeben ! if (l<=0) return null;// Rohkonvertierung vom System uebernehmen String s = Long.toString(z);// Standardfuellzeichen ist das Leerzeichen char fz = ' ';// Wenn Laenge zu gross ist, Fuellzeichen wird *,// Zeichenkette wird geloescht if ( s.length() > l ) { fz = '*'; s = "*"; }// Wenn noetig mit Fuellzeichen auffuellen while ( s.length() < l) { s = fz + s; } return s;}

Page 23: DVG1 - Modulare Programmierung 1 Modulare Programmierung.

DVG1 - Modulare Programmierung

23

2.te Näherung

public class t1

{

public static void main(String [] args)

{

System.out.println("\n i | sqrt(i) | i*i");

System.out.println(

"-------------+---------------------+--------------------");

for (long i=1;i<1000000000;i*=7)

System.out.println(InOut.toString(i,12)+" | "+Math.sqrt(i)+"\t| "+(double)i*i);

}

}

Page 24: DVG1 - Modulare Programmierung 1 Modulare Programmierung.

DVG1 - Modulare Programmierung

24

Ergebnis der 2.ten Näherung

i | sqrt(i) | i*i-------------+---------------------+-------------------- 1 | 1.0 | 1.0 7 | 2.6457513110645907 | 49.0 49 | 7.0 | 2401.0 343 | 18.520259177452136 | 117649.0 2401 | 49.0 | 5764801.0 16807 | 129.64181424216494 | 2.82475249E8 117649 | 343.0 | 1.3841287201E10 823543 | 907.4926996951546 | 6.78223072849E11 5764801 | 2401.0 | 3.3232930569601E13 40353607 | 6352.448897866082 | 1.628413597910449E15 282475249 | 16807.0 | 7.9792266297612E16

Page 25: DVG1 - Modulare Programmierung 1 Modulare Programmierung.

DVG1 - Modulare Programmierung

25

Nutzung der EntwicklungUm die entwickelte Methode weiter zu nutzen und deren Anwendung zu erleichtern, können wir zusätzliche Schnittstellen entwickeln.Z.B.: Konvertierung einer Zahl und anschließende Ausgabe:/*** <EM>print</EM> gibt eine <CODE>long</CODE>-Variable aus und* f&uuml;llt mit f&uuml;hrenden Leerzeichen bis zur L&auml;nge* <CODE>l</CODE> auf. Die Gesamtl&auml;nge enth&auml;lt das* Vorzeichen "-" f&uuml;r negative Zahlen.<BR>* Falls die Zahl nicht in dem Format darstellbar ist, wird die * Zeichenkette "**...*" ausgegeben.<BR>* Wird <CODE>l&lt;=0</CODE> angegeben, so erfolgt keine Ausgabe.* @author Gerhard Telschow* @version 1.0* @param z auszugebende Variable* @param l L&auml;nge der Ausgabe incl. Vorzeichen* @return Nothing*/public static void print(long z, int l){ String s = toString(z, l); if ( s != null) System.out.print(s);}

Page 26: DVG1 - Modulare Programmierung 1 Modulare Programmierung.

DVG1 - Modulare Programmierung

26

Z.B.: Konvertierung einer Zahl und anschließende Ausgabe mit Zeilenschaltung:

/*** <EM>println</EM> gibt eine <CODE>long</CODE>-Variable aus und* f&uuml;llt mit f&uuml;hrenden Leerzeichen bis zur L&auml;nge* <CODE>l</CODE> auf. Die Gesamtl&auml;nge enth&auml;lt das* Vorzeichen "-" f&uuml;r negative Zahlen. Anschlie&szlig;end wird* auf eine neue Zeile geschaltet.<BR>* Falls die Zahl nicht in dem Format darstellbar ist, wird die * Zeichenkette "**...*" ausgegeben.<BR>* Wird <CODE>l&lt;=0</CODE> angegeben, so erfolgt keine Ausgabe.* @author Gerhard Telschow* @version 1.0* @param z auszugebende Variable* @param l L&auml;nge der Ausgabe incl. Vorzeichen* @return Nothing*/public static void println(long z, int l){ print(z,l); System.out.println();}

Page 27: DVG1 - Modulare Programmierung 1 Modulare Programmierung.

DVG1 - Modulare Programmierung

27

Weg von der Aufgabe zur Lösung

Aufgabe

Grobkonzept

Verfeinerung

Lösung

Optimierung

Ausbau

Dokumentation

Endgültige Lösung

Page 28: DVG1 - Modulare Programmierung 1 Modulare Programmierung.

DVG1 - Modulare Programmierung

28

Formatierung von Gleitkommazahlen Eingabe : Gleitkommazahl (float, double)

– float wird automatisch nach double konvertiert ==> wählen double : double z

Ausgabe: Zeichenkette, die den Wert der Eingabezahl repräsentiert. Format der Ausgabe: <vm><m0><.><m1>...<mn>E<ve><ek>...<e0>

– <vm>: Vorzeichen = ' ' oder '-'– <mi>: Ziffern der Mantisse mit folgenden Nullen

– <ve>: Vorzeichen = '+' oder '-'– <ei>: Ziffern des Exponenten mit führenden Nullen

zusätzliche Eingabe:– Länge der Mantisse: int mant (Stellen nach dem Dezimalpunkt)– Länge des Exponenten: int expo– Gesamtlänge der Ausgabe: int l , l = mant+expo+5

Page 29: DVG1 - Modulare Programmierung 1 Modulare Programmierung.

DVG1 - Modulare Programmierung

29

Probleme:– Welche Parameter wählt man. Nur zwei der drei Parameter l, mant und expo können genutzt werden. Wählen l und mant.

– Was tun, wenn falsche Parameter eingegeben werden? • Fehler ist l < mant+6• Fehler ist mant < 1• null ausgeben

– Sonderfälle:• z==+0.0 ==> " 0.0...0E+0..0"• z==-0.0 ==> "-0.0...0E+0..0"• z==+Infinity ==> " Inf "• z==-Infinity ==> "-Inf "• z==NaN ==> "NaN "

– Was tun, wenn sich die Zahl nicht mit l Stellen darstellen läßt? ==> Zeichenkette "**...*" mit l Sternen ausgeben

Page 30: DVG1 - Modulare Programmierung 1 Modulare Programmierung.

DVG1 - Modulare Programmierung

30

Tests für die Sonderfälle z==+0.0: if ( z == 0.0 & 1.0/z > 0.0 ) ... z==-0.0: if ( z == 0.0 & 1.0/z < 0.0 ) ... z==+Infinity: if ( (1.0/z) == 0.0 & z > 0.0 ) ... z==-Infinity: if ( (1.0/z) == 0.0 & z < 0.0 ) ... z==NaN: if ( z != z) ...

Page 31: DVG1 - Modulare Programmierung 1 Modulare Programmierung.

DVG1 - Modulare Programmierung

31

return s;

Berechnung der Ausgabe

Sonderfälle

false

String s;

return null;true

l<mant+6 | mant<1

Blockdiagramm - 1.Versiondouble z, int l, int mant

Page 32: DVG1 - Modulare Programmierung 1 Modulare Programmierung.

DVG1 - Modulare Programmierung

32

Methode - 1.Version

public static String toString (double z, int l, int mant)

{

if ( l < mant+6 | mant < 1 ) return null;

String s;

// Sonderfaelle

// Berechnung der Ausgabezeichenkette

return s;

}

Page 33: DVG1 - Modulare Programmierung 1 Modulare Programmierung.

DVG1 - Modulare Programmierung

33return s;

rechts auffüllen

true

false

false

s.length()>0 Exponent s=s+"E+0"

s="-Inf"

falses=" Inf"

truetruez>0.0

s="NaN"true

Mantisse

false

s="-0.0"

trues=" 0.0"

true1.0/z>0.0

false

z==0.0

1.0/z==0.0

false

z != z

Page 34: DVG1 - Modulare Programmierung 1 Modulare Programmierung.

DVG1 - Modulare Programmierung

34

Methode - Sonderfälleif ( z != z ) s="NaN" ;else{

if (1.0/z == 0.0){

if ( z>0.0 ) s=" Inf";else s="-Inf";

}else{

if ( z == 0.0 ){

if ( 1.0/z > 0.0 ) s=" 0.0";else s="-0.0";while (s.length()<mant+3) s=s+"0";s=s+"E+";while (s.length()<l) s=s+"0";

}}

}

Page 35: DVG1 - Modulare Programmierung 1 Modulare Programmierung.

DVG1 - Modulare Programmierung

35

if ( s.length() > 0 ){

while (s.length()<l) s=s+" ";return s;

}

Page 36: DVG1 - Modulare Programmierung 1 Modulare Programmierung.

DVG1 - Modulare Programmierung

36Ergebnis berechnen

Fehler behandeln

Exponent umwandeln

Vorzeichen des Exponent bestimmen

Mantisse umwandeln

Mantisse und Exponent berechnen

Vorzeichen bestimmen

Page 37: DVG1 - Modulare Programmierung 1 Modulare Programmierung.

DVG1 - Modulare Programmierung

37s=s.substring(0,mant+2);false

s=s+'0';true

s.length()<mant+2

false

s=Double.toString(z);

e--;z*=10.0;true

false

z<1.0

e++;z*=0.1;true

z>=10.0

false

int e=0;

z=-z;true

vz='-'; z<0.0

char vz=' ';

Page 38: DVG1 - Modulare Programmierung 1 Modulare Programmierung.

DVG1 - Modulare Programmierung

38

false

return vz+s+'E'+vze+se;

return s;

s=s+'*';

s.length()<ltrue

s="";

false

se.length()>l-mant-5

se='0'+se;true

se.length()<l-mant-5

String se=Integer.toString(e);

falsee=-e;

truevze='-'; e<0

char vze='+';

true

false

Page 39: DVG1 - Modulare Programmierung 1 Modulare Programmierung.

DVG1 - Modulare Programmierung

39

/**********************************************************************Das Vorzeichen wird bestimmt, in vz gemerkt und die Zahl inpositiv verwandelt.***********************************************************************/ char vz = ' '; if (z<0.0) { vz='-'; z=-z; }/**********************************************************************Der Exponent wird berechnet; z*10^e bleibt der eingegebene Wert***********************************************************************/ int e = 0; while (z >= 10.0) { z*=0.1; e++; } while (z<1.0) { z*=10.0; e--; }/**********************************************************************Mantisse umwandeln**********************************************************************/ s=Double.toString(z); while (s.length()<mant+2) s=s+'0'; s=s.substring(0,mant+2);

Page 40: DVG1 - Modulare Programmierung 1 Modulare Programmierung.

DVG1 - Modulare Programmierung

40

/**********************************************************************Vorzeichen des Exponenten bestimmen**********************************************************************/ String vze = "+"; if (e<0) { vze="-"; e=-e; }/**********************************************************************Exponenten umwandeln**********************************************************************/ String se = Integer.toString(e); while (se.length()<l-mant-5) se="0"+se; if ( se.length()>l-mant-5) { s=""; while (s.length()<l) s=s+'*'; return s; }/**********************************************************************Ergebnis zusammenfuegen**********************************************************************/ return vz+s+'E'+vze+se;

Page 41: DVG1 - Modulare Programmierung 1 Modulare Programmierung.

DVG1 - Modulare Programmierung

41

3.te Näherung

public class t1

{

public static void main(String [] args)

{

System.out.println("\n i | sqrt(i) | i*i");

System.out.println(

"-------------+---------------------+--------------------");

for (long i=1;i<1000000000;i*=7)

System.out.println(InOut.toString(i,12)+" | "+

InOut.toString(Math.sqrt(i),19,12)+" | "+

InOut.toString((double)i*i),19,12));

}

}

Page 42: DVG1 - Modulare Programmierung 1 Modulare Programmierung.

DVG1 - Modulare Programmierung

42

Ergebnis der 3.ten Näherung

i | sqrt(i) | i*i-------------+---------------------+-------------------- 1 | 1.000000000000E+00 | 1.000000000000E+00 7 | 2.645751311064E+00 | 4.900000000000E+01 49 | 7.000000000000E+00 | 2.401000000000E+03 343 | 1.852025917745E+01 | 1.176490000000E+05 2401 | 4.900000000000E+01 | 5.764801000000E+06 16807 | 1.296418142421E+02 | 2.824752490000E+08 117649 | 3.430000000000E+02 | 1.384128720100E+10 823543 | 9.074926996951E+02 | 6.782230728490E+11 5764801 | 2.401000000000E+03 | 3.323293056960E+13 40353607 | 6.352448897866E+03 | 1.628413597910E+15 282475249 | 1.680700000000E+04 | 7.979226629761E+16

Page 43: DVG1 - Modulare Programmierung 1 Modulare Programmierung.

DVG1 - Modulare Programmierung

43

Nutzung der EntwicklungUm die entwickelte Methode weiter zu nutzen und deren Anwendung zu erleichtern, können wir zusätzliche Schnittstellen entwickeln.Z.B.: Konvertierung einer Zahl und anschließende Ausgabe:public static void print(double z, int l, int mant){ String s = toString(z, l, mant); if ( s != null) System.out.print(s);}public static void println(double z, int l, int mant){ print(z, l, mant); System.out.println();}