Objektorientierte Programmierung in - Eine praxisnahe Einführung mit Bluej Programmentwicklung BlueJ 1.0
Ein BlueJ-Projekt Ein BlueJ-Projekt ist der Inhalt eines Verzeichnisses. das Projektname heißt wie das Verzeichnis Das Verzeichnis enthält: Quellcode (.java-dateien), kompilierter Code (.class-dateien), Bluej spezifische Dateien mit Konfigurations- und Steuerinformationen. BlueJ 2
Die BlueJ-Verzeichnisstruktur Calculator project: calculator UserInterface CalcEngine c:\bluej\calculator\ bluej.pkg bluej.pkh Calculator.java Calculator.class Calculator.ctxt UserInterface.java UserInterface.class UserInterface.ctxt CalcEngine.java CalcEngine.class CalcEngine.ctxt BlueJ 3
Die BlueJ-Dateien bluej.pkg - the package file. - Enthält Information über die Klassen, die ein Package (Teilbibliothek) bilden. Eine Datei für jedes Package. bluej.pkh - backup of the package file. *.java - standard source file (text). One per class. *.class - standard code file. One per class *.ctxt - BlueJ context file. Contains extra information for a class. One per class. BlueJ 4
Standard files source files: *.java source files contain the source code in readable form, as typed in by the programmer. class files: *.class class files contain byte code (a machine readable version of the class). They are generated by the compiler from the source file. BlueJ 5
Der Entwicklungszyklus eines -Programms editor source file compiler (javac) class file 011010 110101 010001 011010 110101 1001 10 1 1 1 0111 0110110 virtual machine (java) BlueJ 6
Bearbeitung des Quelltextes Der Quelltext kann mit jedem Texteditor bearbeitet werden: Notepad, emacs, PFE,... Vorsicht bei Textverarbeitungsprogrammen, wie z.b. Word: Datei wird normalerweise nicht im Textformat gespeichert! Erst speichern, dann kompilieren! BlueJ 7
Programmaufruf auf der Kommandozeile Der Kompiler und die JVM des JDK werden mittels einer Kommandozeile aufgerufen. Bei Microsoft: DOS shell. Bei Unix: Unix shell. Path-Variable muss gesetzt sein. BlueJ 8
Kompilieren Name des JDK-Kompiler: javac Aufruf: javac <source name> kompiliert <source name> und alle davon abhängigen Klassen! Example: cd C:\bluej\zuul javac Game.java BlueJ 9
Fehlermeldungen C:\bluej\zuul> javac Game.java Game.java:22: ';' expected. 1 error private Parser parser C:\bluej\zuul> The programmer has to open the file in the editor, find the line number, fix the error and recompile. BlueJ 10 ^
Programmausführung C:\bluej\zuul> java Game java starts the virtual machine. The named class is loaded and execution is started. Other classes are loaded as needed. Only possible if class has been compiled. BlueJ 11
Problem: Welche Methode in der Klasse soll denn ausgeführt werden? If we try: C:\bluej\zuul> java Game.java Exception in thread "main" java.lang.nosuchmethoderror: main The problem: how does the system know which method to execute? BlueJ 12
Die main-methode The answer: The java system always executes a method called main with a certain signature: public static void main(string[] args) {... } For this to work, such a method must exist! BlueJ 13
Die main-methode (2) main must exist main must be public main must be static (class method) main must have a String array parameter Only main can be invoked BlueJ 14
Beispiel für eine main- Methode public static void main(string[] args) { Game game = new Game(); game.play(); } Die main-method sollte ein Objekt erzeugen die erste Methode aufrufen BlueJ 15
Testen to test, test drivers must be written all test method calls must be written into a test method all relevant parameter combination must be tested if tests depend on the results of earlier tests, the test driver must be edited and recompiled the test driver must create the objects BlueJ 16