Click here to load reader
Upload
hadieu
View
238
Download
11
Embed Size (px)
Citation preview
FB Technische BetriebswirtschaftCampus Hagen
Grundlagen der Informatik
Vorlesungsskript fur Wirtschaftsingenieuredes ersten Semesters
Stefan Bocker, Andreas de Vries und Volker Wei
Version: 24. September 2010
Dieses Skript unterliegt der Creative Commons License 3.0(http://creativecommons.org/licenses/by-nc/3.0/deed.de)
http://www.fh-swf.dehttp://creativecommons.org/licenses/by-nc/3.0/deed.dehttp://creativecommons.org/licenses/by-nc/3.0/deed.de
2
Inhaltsverzeichnis
1. Funktionsweise eines Computers 91.1. Information und ihre Darstellung . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
1.1.1. Information . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91.1.2. Wie wird Information dargestellt? . . . . . . . . . . . . . . . . . . . . . . . . . 101.1.3. Darstellung von Zahlen . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121.1.4. Umrechnung vom Dezimal- ins Binarsystem . . . . . . . . . . . . . . . . . . . 141.1.5. Binarbruche . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 151.1.6. Informationsdarstellung im Computer . . . . . . . . . . . . . . . . . . . . . . . 161.1.7. Darstellung reeller Zahlen: Fliekommazahlen nach IEEE 754 . . . . . . . . . . 16
1.2. Der von-Neumann-Computer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 181.3. Betriebssystem und Prozesse . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
2. Grundlegende Sprachelemente von Java 232.1. Einfuhrung . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
2.1.1. Wie funktioniert Java? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 242.1.2. Installation der Java SDK-Umgebung . . . . . . . . . . . . . . . . . . . . . . . 252.1.3. Erstellung von Java-Programmen . . . . . . . . . . . . . . . . . . . . . . . . . 252.1.4. Die 4 Programmarten von Java . . . . . . . . . . . . . . . . . . . . . . . . . . . 252.1.5. Dokumentation der API . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
2.2. Elemente eines Java-Programms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 272.2.1. Anweisungen und Blocke . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 272.2.2. Reservierte Worter und Literale . . . . . . . . . . . . . . . . . . . . . . . . . . 282.2.3. Kommentare . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 282.2.4. import-Anweisung . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
2.3. Applikationen, Strings und Ausgaben . . . . . . . . . . . . . . . . . . . . . . . . . . . 292.3.1. Ausgabe eines Texts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 292.3.2. Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 332.3.3. Moglichkeiten der Ausgabe . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
2.4. Variablen, Datentypen und Operatoren . . . . . . . . . . . . . . . . . . . . . . . . . . . 352.4.1. Variablen, Datentypen und Deklarationen . . . . . . . . . . . . . . . . . . . . . 362.4.2. Der Zuweisungsoperator = . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 372.4.3. Datenkonvertierung mit dem Cast-Operator . . . . . . . . . . . . . . . . . . . . 382.4.4. Operatoren, Operanden, Prazedenz . . . . . . . . . . . . . . . . . . . . . . . . . 392.4.5. Spezielle arithmetische Operatoren . . . . . . . . . . . . . . . . . . . . . . . . 40
2.5. Eingaben . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 412.5.1. Strings addieren . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 412.5.2. Eingabedialoge . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 422.5.3. Zahlen addieren . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 442.5.4. Konvertierung von Strings in Zahlen . . . . . . . . . . . . . . . . . . . . . . . . 442.5.5. Einlesen von Kommazahlen . . . . . . . . . . . . . . . . . . . . . . . . . . . . 452.5.6. Weitere Eingabemoglichkeiten . . . . . . . . . . . . . . . . . . . . . . . . . . . 46
2.6. Zusammenfassung . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47
4
3. Prozedurale Programmierung 503.1. Logik und Verzweigung . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50
3.1.1. Die Verzweigung . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 503.1.2. Logische Bedingungen durch Vergleiche . . . . . . . . . . . . . . . . . . . . . 513.1.3. Logische Operatoren . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 533.1.4. Die if-else-if-Leiter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 563.1.5. Gleichheit von double-Werten . . . . . . . . . . . . . . . . . . . . . . . . . . . 563.1.6. Abfragen auf Zahlbereiche . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57
3.2. Wiederholung durch Schleifen . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 583.2.1. Die while-Schleife . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 583.2.2. Die Applikation Guthaben . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 583.2.3. Umrechnung Dezimal- in Binardarstellung . . . . . . . . . . . . . . . . . . . . 593.2.4. Die for-Schleife . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 603.2.5. Die do/while-Schleife . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 623.2.6. Wann welche Schleife? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63
3.3. Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 633.3.1. Was ist ein Array? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 633.3.2. Lagerbestande . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65
3.4. Die for-each-Schleife . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 663.5. Mehrdimensionale Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 673.6. Statische Methoden . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68
3.6.1. Mathematische Funktionen . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 683.6.2. Was ist eine Methode? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 693.6.3. Eine erste Applikation mit statischen Methoden . . . . . . . . . . . . . . . . . . 723.6.4. Statische Methoden: Die Applikation Pythagoras . . . . . . . . . . . . . . . . . 73
3.7. Zusammenfassung . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76
4. Objektorientierte Programmierung 794.1. Die Grundidee der Objektorientierung . . . . . . . . . . . . . . . . . . . . . . . . . . . 79
4.1.1. Was charakterisiert Objekte? . . . . . . . . . . . . . . . . . . . . . . . . . . . . 794.1.2. Objekte als Instanzen ihrer Klasse . . . . . . . . . . . . . . . . . . . . . . . . . 804.1.3. Kapselung und Zugriff auf Attributwerte . . . . . . . . . . . . . . . . . . . . . 804.1.4. Abfragen und Verandern von Attributwerten: get und set . . . . . . . . . . . . . 81
4.2. Klassendiagramme der UML . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 814.3. Programmierung von Objekten . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82
4.3.1. Der Konstruktor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 824.3.2. Die Referenz this . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 824.3.3. Typische Implementierung einer Klasse fur Objekte . . . . . . . . . . . . . . . . 834.3.4. Objektelemente sind nicht statisch . . . . . . . . . . . . . . . . . . . . . . . . . 844.3.5. Lokale Variablen und Attribute . . . . . . . . . . . . . . . . . . . . . . . . . . . 844.3.6. Speicherverwaltung der Virtuellen Maschine . . . . . . . . . . . . . . . . . . . 85
4.4. Erzeugen von Objekten . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 854.4.1. Deklaration von Objekten . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 864.4.2. Erzeugung von Objekten: der new-Operator . . . . . . . . . . . . . . . . . . . . 864.4.3. Aufruf von Methoden eines Objekts . . . . . . . . . . . . . . . . . . . . . . . . 874.4.4. Besondere Objekterzeugung von Strings und Arrays . . . . . . . . . . . . . . . 87
4.5. Fallbeispiel DJTools . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 874.5.1. Version 1.0 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 874.5.2. Version 2.0: Formatierte Zeitangaben . . . . . . . . . . . . . . . . . . . . . . . 90
4.6. Zeit- und Datumfunktionen in Java . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 944.6.1. Die Systemzeit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 954.6.2. GregorianCalendar . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95
5
4.6.3. Datumsformatierungen . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 974.7. Objekte in Interaktion: Fallbeispiel Konten . . . . . . . . . . . . . . . . . . . . . . . . . 994.8. Zusammenfassung . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102
A. Spezielle Themen 104A.1. javadoc, der Dokumentationsgenerator . . . . . . . . . . . . . . . . . . . . . . . . . . . 104
A.1.1. Dokumentationskommentare . . . . . . . . . . . . . . . . . . . . . . . . . . . . 104A.2. jar-Archive: Ausfuhrbare Dateien und Bibliotheken . . . . . . . . . . . . . . . . . . . . 105
A.2.1. Manifest-Datei . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 106A.3. Formatierte Ausgabe mit HTML . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 106
A.3.1. HTML-Ausgabe von Wahrheitstabellen . . . .