Java Tools JDK http://www.oracle.com/technetwork/java/javase/ Downloads IDEs Java SE 8 Java SE 8 Documentation Eclipse http://www.eclipse.org IntelliJ http://www.jetbrains.com/idea/ NetBeans https://netbeans.org/ 24 February 2017 1
Eclipse 24 February 2017 2
Eclipse: Create New Project File -> New -> Java Project Project name: HelloEclipse Finish 24 February 2017 3
Eclipse: Create New Class New -> Class Name: Hello [x] on public static void main(string[] args) Finish 24 February 2017 4
Eclipse: Edit and Execute Program Edit your program (and save it) Execute the program Press or Exeucte Run As => Java Application in Context menu of Hello.java 24 February 2017 5
Eclipse: Using Eclipse Syntax Errors are marked with a red x Hover over the x to get explanation Warnings are marked with a yellow! Hover over the "!" to get explanation 24 February 2017 6
Eclipse: Using Eclipse Documentation Hover over an element and wait Ctrl-Click on an element to jump into its sources 24 February 2017 7
Java Classes A class begins with public class Name {... } Class is stored in file Name.java Class names are capitalized Java Programs A "program" is a class which contains a main method public static void main(string[] args) {... } public static void args = accessible from everywhere = defined on the class level (not bound to instances), accessible over the class = does not return a result (=> procedure) = method argument, array of command-line arguments 24 February 2017 8
Comments Provide information for people For every class: What is the purpose of this class? For every method: What does it? Which parameters are required? What value does it return? In your code: explain difficult statements only, code should be readable! Comment Types // single line comment /* multiline comment, starts with /*, ends with first occurrence of */ /** JavaDoc comment, may be multi-line */ No nesting of comments possible 24 February 2017 9
Comments with Eclipse (operates on selection) Source->Toggle Comment [CTRL-/ or CTRL-7] Source->Add Block Comment [CTRL-SHIFT-/] Source->Remove Block Comment [CTRL-SHIFT-\] Source->Generate Element Comment [ALT-SHIFT-J] Formatting with Eclipse Source->Shift Left Source->Shift Right Source->Correct Indentation [CTRL-I] Source->Format [CTRL-SHIFT-F] Works on selection or, without selection, on while source 24 February 2017 10
System.out.println, what is this System out A class containing several useful fields and methods Class System is defined in package java.lang (automatically available) Public field offered by the System class The standard output stream, gives access to the console println A method to print a line of text 24 February 2017 11
24 February 2017 12
Methods Methods allow to define your own statements (or functions) Functions are methods which return a value (to be used in an expression) Methods combine a set of statements Method Declaration Begins with a line declaring its name and parameters Method body contains the statements public static ResultType Name ( Parameter, Parameter,..) { Body } Result type void for procedures 24 February 2017 13
Declaration public static class Marks double { mark(double p1, double p2, double msp) { double public en static = Math.round(10*(p1+p2)/2)/10.0;// double mark(double p1, double p2, Erfahrungsnote double mn = Math.round(10*msp)/10.0; // double Modulnote msp){ return double Math.round(10*(en = Math.round(10*(p1+p2)/2)/10.0; + mn)/2)/10.0; } double mn = Math.round(10*msp)/10.0; return Math.round(10*(en + mn)/2)/10.0; public } static boolean passed(double p1, double p2, double msp) { return public mark(p1, static boolean p2, msp) passed(double >= 3.8; p1, double p2, } double msp) { return mark(p1, p2, msp) >= 3.8; } } 24 February 2017 14
Invocation Called using class name: x = Math.sqrt(9) public class Test public static void main(string[] args) { if(marks.passed(4, 3.9, 3.4)) { System.out.println("Gratulation!"); } } } 24 February 2017 15