Exercise 1: Intro to Java & Eclipse Discussion of exercise solution We do not put the exercise solution s source code online! But we show & publish the most important parts on slides There are sometimes several possible solutions Tips for the next exercise Remarks about the week s topic Tips about programming tools, libraries, etc.... 1
Your assistant for this week Velko Vechev PhD student at Advanced Interactive Technologies (AIT) Department of Computer Science (D-INFK) Technical Human-Computer Interaction (HCI) E-mail: velko.vechev@inf.ethz.ch Office: CNB H 100.9 Please submit exercise 1 solutions to me Complex.java 2
No testat, but exercises are exam material Exercises: Exercises 1-3: Recommended to do alone Exercises 4-11: Teams of two are recommended (it is a lot of work) Hand-in (by Email) Velko Vechev, velko.vechev@inf.ethz.ch Stefan Stevsic, stefan.stevsic@inf.ethz.ch Deadline: Two weeks after hand-in (there are exceptions) Assistants may not be able to provide feedback for late submissions Ask specific questions 3
Today Installation of Java + Eclipse First steps with eclipse (Demo) Hello World in Java Vektor-Class 4
Installation of Java install Java Development Kit (JDK) Version 9 aka. 1.9, (versions 1.4 lack features used in this course) Windows / OSX: Download from http://www.oracle.com/technetwork/java/javase/downloads/index.html 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. 5
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 6
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) 7
Start java from the command line The Java VM (java.exe) as well as the compiler (javac.exe) can be started from the console: 8
Extend the searchpath (Windows) The bin directory of the JDK must be in the PATH environment variable: Under : My Computerà Properties extend Path (append) ;C:\<INSTALLATION-DIRECTORY>\Java\jdk1.8.0_121\bin; 9
Eclipse IDE Download: http://www.eclipse.org/downloads recomended Eclipse is an open-source framework, primarily 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) 10
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 11
Eclipse 12
Eclipse Execute Application Icon or: Run As => Java Application in context menu 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 13
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: 14
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 Be able to read your own code 6 months from now J 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 15
Some Code Conventions Package names are lower-case (e.g. ch.ethz.inf.sd.math) Classes start with an upper-case letter (e.g. 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. Separation of words by an upper-case letter (camelcase): get_first_element() getfirstelement() Brackets: void foo() void foo() { { if (x>y) { if (x>y) else { { else { Indent code blocks 16
Console output of objects Output of primitive 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) 17
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 18
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); 19
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()); 20