Software Design Exercise session 1 : Introduction to Java & Eclipse Stefan Müller stefan.mueller@inf.ethz.ch 1
About us Stefan Müller stefan.mueller@inf.ethz.ch What I m doing PhD Student (Systems Group) Darko Makreshanski darkoma@inf.ethz.ch What I m doing PhD Student (Systems Group) Office CAB E 78 Office CAB F 72 2
Exercise Sessions Discussion of exercise solution We do not put the exercise solution s source code online! There are sometimes several possible solutions Tips for the next exercise Remarks about the week s topic Tips about programming tools, libraries, etc.... 3
Exercise Sessions Slides online after Exercise Session: http://web.fhnw.ch/plattformen/sd/assignments 4
No testat, but exercises are exam material Exercises: Exercises 1-3: Recomended to do alone Exercises 4-11: Teams of two are recomended (it is a lot of work) Hand-in A-J: stefan.mueller@inf.ethz.ch K-Z: darkoma@inf.ethz.ch Exercises 1-3: Sourcecode by e-mail Subject: SD:netzname_ex# Bsp: SD:smuelle_ex01 Exercises 4-11: (see slides exercise session 5) 5
No testat, but exercises are exam material Hand-in Deadline: Two weeks after hand-in (there are exceptions) We may not be able to provide feedback for late submissions Ask specific questions 6
Today Installation of Java + Eclipse First steps with eclipse (Demo) Hello World in Java Vektor-Class 7
Installation of Java install Java Development Kit (JDK) Version 8 aka. 1.8, (versions 1.4 lack features used in this course) Windows / OSX: Download from http://www.oracle.com/technetwork/java/javase/downloads/index.html (Version JDK without JavaFX, J2EE or NetBeans) Linux: Installation via Package-Manager (apt-get, Synaptic etc.) or download from Oracle Attention: Java Runtime Environment (JRE) is not sufficient: The compiler is missing! JRE is part of the JDK. 8
Hello World Application in Java Enter source in text-editor and save as HelloWorld.java Filename must be equal to the class name. public class HelloWorld { public static void main(string args[]) { System.out.println( Hello World! ); HelloWorld.java 9
Hello World Programm in Java Name of the class: HelloWorld Application entrance point: main( ) The Java Virtual Machine (VM) searches the static method static void main(string[]) within the class that was given to the java command. Array args contains the command line arguments. public class HelloWorld { public visibility (all) public static void main(string args[]) { System.out.println( Hello World! ); class object method System static field (class variable) 10
Start java from the command line The Java VM (java.exe) as well as the compiler (javac.exe) can be started from the console: 11
Extend the searchpath (Windows) The bin directory of the JDK must be in the PATH environment variable: Unter : My Computer Properties extend Path (append) ;C:\<INSTALLATION-DIRECTORY>\Java\jdk1.6.0_18\bin; 12
Eclipse IDE Download: http://www.eclipse.org/downloads recomended Eclipse is an open-source framework, primarlily used as a Integrated Development Environment (IDE) Eclipse is modular and can be extended by plugins Eclipse is available in several packages (different set of pre-installed plugins) 13
Eclipse Workspaces Workspace: Contains the projects Where is my workspace? Shown at startup It is possible to have several workspaces Hides this dialog in the future. Can be changed later in: Window Preferences General Startup and Shutdown Workspaces Prompt for workspace on startup 14
Eclipse 15
Eclipse Execute Application Icon or: Run As => Java Application in contextmenu of Hello.java Helpers while coding: Code formatting: CTRL+SHIFT+F Code Completion: CTRL+SPACE Refactoring: Renaming of methods, classes, fields, incl. changing of all references. a lot more... Stefan Müller Software Design Übungsstunde Frühlingssemester 2012 16
Documentation Complete Java Documentation: http://docs.oracle.com/javase/8/docs/ Documentation of all Classes that come with java http://docs.oracle.com/javase/8/docs/api/ Tooltips in Eclipse: 17
Java Coding Style Guideline for Java-Programming style Increases readability of the code 80% of software cost is from maintenance Code usually is not maintained by the original author Makes it easier for me to read Code Conventions for the Java Programming Language http://www.oracle.com/technetwork/java/codeconv-138413.html Used by almost all professional java developers Automatic in Eclipse : Source Format 18
Some Code Conventions Package names are lower-case (e.g. ch.ethz.inf.sd.math) Classes start with an upper-case letter (z.b. Complex) Java Files are named after the public class they contain. UNIX-like operating systems are case sensitive! Static constants are written all upper-case. Fields and methods start with a lower-case letter. Seperation of words by an upper-case letter: get_first_element() getfirstelement() Brackets: void foo() void foo() { { if (x>y) { if (x>y) else { { else { Indent code blocks 19
Console output of objects Output of primive types (int, float, double, usw.) int a, b;... System.out.println(a+b*3.0); Result appears in console Output of objects by converting them to strings: Foo foo = new Foo(); System.out.println(foo); Output: Foo@82ba41 (Classe+hash value) 20
A class for vector operations Encapsulation: Class Vector capsulates vector operations in R 3 Possible operations (not complete): Constructors: Construct vector from the three components R 3 x R 3 R 3 : addition, subtraction R 3 x R R 3 : scaling R 3 R : norm getter and setter methods for the individual components. normalizing Output as string Source code available online after the exercise 21
public class Vector { private double x, y, z; Basic structure public Vector(double x, double y, double z) { this.x = x; this.y = y; this.z = z; //... public double getx() { return x; public void setx(double x) { this.x = x; public void add(vector v) { x+=v.x; y+=v.y; z+=v.z; //... also possible: public Vector add(vector v) { return new Vector(x+v.x, y+v.y, z+v.z); 22
How to use the vector class: The class VectorTest uses Vector public class VectorTest { public static void main(string args[]) { Vector a = new Vector(3.0, 1.0, 5.0); Vector b = new Vector(1.0, 0.0, 0.0); a.add(b); System.out.println(a.getX()); 23