![Page 1: Introduction to Programming with Java Presented by RA Waraich (adapted from Slides of BJ Vitins) IVT ETH Zürich September 2014](https://reader033.vdocuments.us/reader033/viewer/2022051515/55204d8449795902118d8e37/html5/thumbnails/1.jpg)
Introduction to Programming with Java
Presented by RA Waraich (adapted from Slides of BJ Vitins)
IVTETHZürich
September 2014
![Page 2: Introduction to Programming with Java Presented by RA Waraich (adapted from Slides of BJ Vitins) IVT ETH Zürich September 2014](https://reader033.vdocuments.us/reader033/viewer/2022051515/55204d8449795902118d8e37/html5/thumbnails/2.jpg)
2
Übersicht
• Klassen, Methoden, Objekte
• Referenzen
• Datentypen und Konventionen
• Pakete und Zugriffe
• Codebeispiele
![Page 3: Introduction to Programming with Java Presented by RA Waraich (adapted from Slides of BJ Vitins) IVT ETH Zürich September 2014](https://reader033.vdocuments.us/reader033/viewer/2022051515/55204d8449795902118d8e37/html5/thumbnails/3.jpg)
3
Objekte
• Sind autonome gekapselte Einheiten eines bestimmten Types
• Haben einen eigenen Zustand (= lokale Variablen)
• Besitzen ein Verhalten (wenn sie aktiviert werden)
• Bieten anderen Objekten eine Dienstleistung an• Durchführung von Berechnungen• Änderungen des lokalen Zustandes• Zurückliefern von Variablenwerten oder
Berechnungsergebnissen• Allgemein: “Reaktion” auf Aufruf einer Methode
Que
lle: M
atte
rn (
2010
)
![Page 4: Introduction to Programming with Java Presented by RA Waraich (adapted from Slides of BJ Vitins) IVT ETH Zürich September 2014](https://reader033.vdocuments.us/reader033/viewer/2022051515/55204d8449795902118d8e37/html5/thumbnails/4.jpg)
4
Dynamische Klassen und Referenzen
• Erzeugen von dynamischen Klassen geschieht mit
new x();
• Es wird eine Instanz bzw. Objekt der Klasse x erzeugt und eine Referenz (“Zeiger”) darauf zurückgeliefert
• Am besten die Referenz gleich abspeichern: p1 = new x();
• Referenzen sind implementierungstechnisch Adressen• null gehört zum Wertebereich aller Referenztypen
Que
lle: M
atte
rn (
2010
)
![Page 5: Introduction to Programming with Java Presented by RA Waraich (adapted from Slides of BJ Vitins) IVT ETH Zürich September 2014](https://reader033.vdocuments.us/reader033/viewer/2022051515/55204d8449795902118d8e37/html5/thumbnails/5.jpg)
5
Erzeugen von Objekten
public class Bicycle {
// the Bicycle class has three fields
public int gear;
public int speed;
public String marke;
}
class Herstellung {
public static void main (String [] args) {
Bicycle f;
f = new Bicycle ();
f.marke = “Scott”;
System.out.println(f.marke)
}
}
Que
lle: n
ach
Ora
cle
(201
4)
![Page 6: Introduction to Programming with Java Presented by RA Waraich (adapted from Slides of BJ Vitins) IVT ETH Zürich September 2014](https://reader033.vdocuments.us/reader033/viewer/2022051515/55204d8449795902118d8e37/html5/thumbnails/6.jpg)
6
Programmstruktur (1)
• Mit “import” werden anderweitig vorhandene Pakete von Klassen verfügbar gemacht.
• Der Klassenkörper enthält • Instanzen und Klassenvariablen• Benannte Konstanten• Klassenbezogene “static” Methoden
• Methoden übernehmen die Rollen von Funktionen• Konstruktoren sind spezielle Methoden
(beim Erzeugen aufgerufen)
import
class A {
}
class B {…}
Konstruktur {}
Method_1{}
Method_1{}
Klassenkörper
Que
lle: M
atte
rn (
2010
)
![Page 7: Introduction to Programming with Java Presented by RA Waraich (adapted from Slides of BJ Vitins) IVT ETH Zürich September 2014](https://reader033.vdocuments.us/reader033/viewer/2022051515/55204d8449795902118d8e37/html5/thumbnails/7.jpg)
7
Programmstruktur (2)
• Bei eigenständigen Programmen muss es eine “main” – Methode geben, die so aussieht:
public static void main (String[] args) {…
• Jede Klasse kann eine (einzige) solche “main” – Methode enthalten. Sie wird ausgeführt, wenn der entsprechende Klassenname beim “java” Kommando Zeile genannt wird.
import
class A {
}
class B {…}
Konstruktur {}
Method_1{}
Method_1{}
Klassenkörper
Que
lle: M
atte
rn (
2010
)
![Page 8: Introduction to Programming with Java Presented by RA Waraich (adapted from Slides of BJ Vitins) IVT ETH Zürich September 2014](https://reader033.vdocuments.us/reader033/viewer/2022051515/55204d8449795902118d8e37/html5/thumbnails/8.jpg)
8
Beispiel
class Bicycle { private int cadence = 0; private int speed = 0; private int gear = 1; public String marke;
public Bicycle() {System.out.println("Eine Fahrradinstanz wurde generiert");
}void changeCadence(int newValue) {
cadence = newValue; } void changeGear(int newValue) {
gear = newValue; } void speedUp(int increment) {speed = speed + increment; } void applyBrakes(int decrement) { speed = speed - decrement; } void printStates() { System.out.println("cadence:" + cadence + " speed:" + speed + " gear:" + gear); }
}Que
lle: O
racl
e (2
014)
Attribute von aussen nicht mehr sichtbar
Konstruktor besitzt den Klassennamen und hat kein Rückgabetyp
Für die Veränderung der Variablen braucht es eine Methode
![Page 9: Introduction to Programming with Java Presented by RA Waraich (adapted from Slides of BJ Vitins) IVT ETH Zürich September 2014](https://reader033.vdocuments.us/reader033/viewer/2022051515/55204d8449795902118d8e37/html5/thumbnails/9.jpg)
9
Anwendung des Beispiels
class BicycleDemo {
public static void main(String[] args) {
// Create two different bicycle objects
Bicycle bike1 = new Bicycle();
// Invoke methods on those objects bike1.changeCadence(50);
bike1.speedUp(10);
bike1.changeGear(2);
bike1.printStates();
}
}
Que
lle: O
racl
e (2
014)
Konstruktor wird aufgerufen
bike1 ist eine Referenz die auf Bicycle Objekte zeigen kann
Bike1 hat auch fünf Methoden, die aufgerufen werden können
![Page 10: Introduction to Programming with Java Presented by RA Waraich (adapted from Slides of BJ Vitins) IVT ETH Zürich September 2014](https://reader033.vdocuments.us/reader033/viewer/2022051515/55204d8449795902118d8e37/html5/thumbnails/10.jpg)
10
Ausgabe von Daten in der Kommandozeile
int i = 461012;
System.out.println("The value of i is:”, i);
Zusätzlich wird danach noch ein Zeilenumbruch erzeugt.
![Page 11: Introduction to Programming with Java Presented by RA Waraich (adapted from Slides of BJ Vitins) IVT ETH Zürich September 2014](https://reader033.vdocuments.us/reader033/viewer/2022051515/55204d8449795902118d8e37/html5/thumbnails/11.jpg)
11
Konventionen
• kleingeschrieben:• methoden• variablen
• Grossgeschrieben• Klassen• Konstruktor
• Oft beginnen Methoden mit einem Verb
![Page 12: Introduction to Programming with Java Presented by RA Waraich (adapted from Slides of BJ Vitins) IVT ETH Zürich September 2014](https://reader033.vdocuments.us/reader033/viewer/2022051515/55204d8449795902118d8e37/html5/thumbnails/12.jpg)
12
Methoden
public double calculateEnergy (double wingSpan, int numberOfEngines, double length, double grossTons) {
double energy;
//do the calculation here
return energy;
}
ModifierReturn Typ, oder void
Methodenname Notwendige Parameter
Que
lle: O
racl
e (2
014)
![Page 13: Introduction to Programming with Java Presented by RA Waraich (adapted from Slides of BJ Vitins) IVT ETH Zürich September 2014](https://reader033.vdocuments.us/reader033/viewer/2022051515/55204d8449795902118d8e37/html5/thumbnails/13.jpg)
13
Methoden
public class DataArtist { ... public void draw(String s) {
... } public void draw(int i) {
... } public void draw(double f) {
... } public void draw(int i, double f) {
... }
}Que
lle: O
racl
e (2
014)
![Page 14: Introduction to Programming with Java Presented by RA Waraich (adapted from Slides of BJ Vitins) IVT ETH Zürich September 2014](https://reader033.vdocuments.us/reader033/viewer/2022051515/55204d8449795902118d8e37/html5/thumbnails/14.jpg)
14
Information hiding
• Mit bewusst verfügbaren und unsichtbaren Informationen kann ein Missbrauch der Daten verhindert werden.
• Korrektheit kann von den Klassen selber überprüft werden.
• Dadurch wird eine sichere Codeumgebung gewährleistet.• Es können Schnittstellen definiert werden.• Es können Änderungen im Code erfolgen.
Que
lle: M
atte
rn (
2010
)
![Page 15: Introduction to Programming with Java Presented by RA Waraich (adapted from Slides of BJ Vitins) IVT ETH Zürich September 2014](https://reader033.vdocuments.us/reader033/viewer/2022051515/55204d8449795902118d8e37/html5/thumbnails/15.jpg)
15
Inheritance
Que
lle: O
racl
e (2
014)
![Page 16: Introduction to Programming with Java Presented by RA Waraich (adapted from Slides of BJ Vitins) IVT ETH Zürich September 2014](https://reader033.vdocuments.us/reader033/viewer/2022051515/55204d8449795902118d8e37/html5/thumbnails/16.jpg)
16
Abstrakte Klassen und Methoden
Que
lle: O
racl
e (2
014)
![Page 17: Introduction to Programming with Java Presented by RA Waraich (adapted from Slides of BJ Vitins) IVT ETH Zürich September 2014](https://reader033.vdocuments.us/reader033/viewer/2022051515/55204d8449795902118d8e37/html5/thumbnails/17.jpg)
17
Referenzen
![Page 18: Introduction to Programming with Java Presented by RA Waraich (adapted from Slides of BJ Vitins) IVT ETH Zürich September 2014](https://reader033.vdocuments.us/reader033/viewer/2022051515/55204d8449795902118d8e37/html5/thumbnails/18.jpg)
18
Gleichheit
78 23 12a1
a2
78 23 12a3
![Page 19: Introduction to Programming with Java Presented by RA Waraich (adapted from Slides of BJ Vitins) IVT ETH Zürich September 2014](https://reader033.vdocuments.us/reader033/viewer/2022051515/55204d8449795902118d8e37/html5/thumbnails/19.jpg)
19
Referenzen und Pointer
Bicycle b,c;
b = new Bicycle ();
c Ø
b
Ø
Que
lle: n
ach
Mat
tern
(20
10)
Marke:Cadence:Händler:
b = null;
c Ø
b Ø
Hätte man vorher c = b ausgeführt:
c
b
Ø
Marke:Cadence:Händler:
Dieses Objekt ist nicht mehr zugreifbar und ab sofort endgültig verloren
Marke:Cadence:Händler:
![Page 20: Introduction to Programming with Java Presented by RA Waraich (adapted from Slides of BJ Vitins) IVT ETH Zürich September 2014](https://reader033.vdocuments.us/reader033/viewer/2022051515/55204d8449795902118d8e37/html5/thumbnails/20.jpg)
20
Zuweisen von Referenzen
b = new Bicycle();
c = new Bicycle();
b.marke = “Scott”;
c.marke = “Cannondale”;
b
c
Marke: ScottCadence:Händler:
Marke: CannondaleCadence:Händler:
b = c
b
c
System.out.println(b.marke);
System.out.println(c.marke);
ist beides “Cannondale”.
Marke: ScottCadence:Händler:
Marke: CannondaleCadence:Händler:
![Page 21: Introduction to Programming with Java Presented by RA Waraich (adapted from Slides of BJ Vitins) IVT ETH Zürich September 2014](https://reader033.vdocuments.us/reader033/viewer/2022051515/55204d8449795902118d8e37/html5/thumbnails/21.jpg)
21
Gleichheit von Objekten
• Referenzvariablen können auf Gleichheit / Ungleichheit mit “==“ bzw. “!=“ verglichen werden
• p == q ist true genau dann wenn die beiden Referenzvariablen auf dasselbe Objekt zeigen (z.B. nach p = q) oder wenn beide null sind.
• p != q wenn zwei verschiedene Objekte auf die p bzw. Q verweisen, die gleichen Werte haben.
Que
lle: M
atte
rn (
2010
)
![Page 22: Introduction to Programming with Java Presented by RA Waraich (adapted from Slides of BJ Vitins) IVT ETH Zürich September 2014](https://reader033.vdocuments.us/reader033/viewer/2022051515/55204d8449795902118d8e37/html5/thumbnails/22.jpg)
22
Ungleichheit von Objekten mit gleichen Variablenwerten
b = new Bicycle();
c = new Bicycle();
b.marke = “Scott”;
c.marke = “Scott”;
b
c
b != c
Marke: ScottCadence: 2Händler:
Marke: ScottCadence: 2Händler:
![Page 23: Introduction to Programming with Java Presented by RA Waraich (adapted from Slides of BJ Vitins) IVT ETH Zürich September 2014](https://reader033.vdocuments.us/reader033/viewer/2022051515/55204d8449795902118d8e37/html5/thumbnails/23.jpg)
23
Primitive Datentypen
Datentyp Grösse Wertebereich Beschreibung
boolean JVM-Spezifisch true / false Boolescher
Wahrheitswert
char 16 bit 0 ... 65.535 (z. B. 'A') Unicode-Zeichen (UTF-16)
byte 8 bit -128 ... 127 Ganze Zahl
short 16 bit -32.768 ... 32.767 Ganze Zahl
int 32 bit -2.147.483.648 ... 2.147.483.647 Ganze Zahl
long 64 bit -9.223.372.036.854.775.808 ...
9.223.372.036.854.775.807
Ganze Zahl
float 32 bit +/-1,4E-45 ... +/-3,4E+38 Gleitkommazahl
double 64 bit +/-4,9E-324 ... +/-1,7E+308 Gleitkommazahl
![Page 24: Introduction to Programming with Java Presented by RA Waraich (adapted from Slides of BJ Vitins) IVT ETH Zürich September 2014](https://reader033.vdocuments.us/reader033/viewer/2022051515/55204d8449795902118d8e37/html5/thumbnails/24.jpg)
24
Arrays
class ArrayDemo {
public static void main(String[] args) {
// declares an array of integers
int[] anArray;
// allocates memory for 10 integers
anArray = new int[10];
// initialize first element
anArray[0] = 100;
// initialize second element
anArray[1] = 200;
// and so forth
anArray[2] = 300;
}
}
Que
lle: n
ach
Mat
tern
(20
10)
![Page 25: Introduction to Programming with Java Presented by RA Waraich (adapted from Slides of BJ Vitins) IVT ETH Zürich September 2014](https://reader033.vdocuments.us/reader033/viewer/2022051515/55204d8449795902118d8e37/html5/thumbnails/25.jpg)
25
Matrizen
• double [][] matrix = new double [4][4];
• matrix[3][0] = 18.309823;
• int[][] matrix2 = {{1,2,3}, {4,5,6}};
• Die Grösse der Matrix / des Arrays wird festgelegt.• Einmal festgelegt kann sie sich aber nicht mehr verändern
Zeilenanzahl Spaltenanzahl
![Page 26: Introduction to Programming with Java Presented by RA Waraich (adapted from Slides of BJ Vitins) IVT ETH Zürich September 2014](https://reader033.vdocuments.us/reader033/viewer/2022051515/55204d8449795902118d8e37/html5/thumbnails/26.jpg)
26
if - else
void applyBrakes() {
if (isMoving) {
currentSpeed--;
}
else {
System.err.println("The bicycle has already stopped!");
}
}
![Page 27: Introduction to Programming with Java Presented by RA Waraich (adapted from Slides of BJ Vitins) IVT ETH Zürich September 2014](https://reader033.vdocuments.us/reader033/viewer/2022051515/55204d8449795902118d8e37/html5/thumbnails/27.jpg)
27
while
class WhileDemo {
public static void main(String[] args){
int count = 1;
while (count < 11) {
System.out.println("Count is: " + count);
count++;
}
}
}
![Page 28: Introduction to Programming with Java Presented by RA Waraich (adapted from Slides of BJ Vitins) IVT ETH Zürich September 2014](https://reader033.vdocuments.us/reader033/viewer/2022051515/55204d8449795902118d8e37/html5/thumbnails/28.jpg)
28
for
class ForDemo {
public static void main(String[] args){
for(int i=1; i < 11; i++){
System.out.println("Count is: " + i);
}
}
}
![Page 29: Introduction to Programming with Java Presented by RA Waraich (adapted from Slides of BJ Vitins) IVT ETH Zürich September 2014](https://reader033.vdocuments.us/reader033/viewer/2022051515/55204d8449795902118d8e37/html5/thumbnails/29.jpg)
29
Pakete in Java
![Page 30: Introduction to Programming with Java Presented by RA Waraich (adapted from Slides of BJ Vitins) IVT ETH Zürich September 2014](https://reader033.vdocuments.us/reader033/viewer/2022051515/55204d8449795902118d8e37/html5/thumbnails/30.jpg)
30
Pakete in Java
• Paket: Zusammengehörte Menge von Klassen
• Hierarchischer Aufbau: Paket “xyz” im Paket “java” → “java.xyz”
• Klassen und Methoden (sowie Attribute von Klassen) sind ohne weitere Angaben nur im eigenen Paket sichtbar und zugreifbar.
• Konvention: kleingeschriebene Namen• Paketdeklaration immer am Anfang einer Datei, z.B. package
xyz;
Que
lle: n
ach
Mat
tern
(20
10)
![Page 31: Introduction to Programming with Java Presented by RA Waraich (adapted from Slides of BJ Vitins) IVT ETH Zürich September 2014](https://reader033.vdocuments.us/reader033/viewer/2022051515/55204d8449795902118d8e37/html5/thumbnails/31.jpg)
31
Zugriff auf Klassen und Methoden
package vehicle;
public class Bicycle {
…void changeCadence(int newValue) {
cadence = newValue; }
}• Bicycle kann auch ausserhalb des Paketes verwendet werden. • changeCadence() kann nur innerhalb des package «vehicle»
verwendet werden.• Ein «private» Modifier erlaubt nur Zugriff innerhalb der Klasse
Bicylce.
Que
lle: n
ach
Mat
tern
(20
10)
![Page 32: Introduction to Programming with Java Presented by RA Waraich (adapted from Slides of BJ Vitins) IVT ETH Zürich September 2014](https://reader033.vdocuments.us/reader033/viewer/2022051515/55204d8449795902118d8e37/html5/thumbnails/32.jpg)
32
Literatur
Mattern, F. (2010) Informtik II, Departement Informatik, ETH Zürich, Zürich.
Oracle (2014) The java Tutorials, webpage, URL http://docs.oracle.com/javase/tutorial/.