Slides_Eclipse

Transcription

Slides_Eclipse
Java Tools
• JDK
– http://www.oracle.com/technetwork/java/javase/
– Downloads
 Java SE 8
 Java SE 8 Documentation
• IDEs
– Eclipse
 http://www.eclipse.org
– IntelliJ
 http://www.jetbrains.com/idea/
– NetBeans
 https://netbeans.org/
26 February 2016
(C) Hochschule für Technik
Fachhochschule Nordwestschweiz
1
Eclipse
26 February 2016
(C) Hochschule für Technik
Fachhochschule Nordwestschweiz
2
Eclipse: Create New Project
• File -> New -> Java Project
– Project name: HelloEclipse
– Finish
26 February 2016
(C) Hochschule für Technik
Fachhochschule Nordwestschweiz
3
Eclipse: Create New Class
• New -> Class
– Name: Hello
– [x] on public static void
main(String[] args)
– Finish
26 February 2016
(C) Hochschule für Technik
Fachhochschule Nordwestschweiz
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
26 February 2016
(C) Hochschule für Technik
Fachhochschule Nordwestschweiz
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
26 February 2016
(C) Hochschule für Technik
Fachhochschule Nordwestschweiz
6
Eclipse: Using Eclipse
• Documentation
– Hover over an element and wait
– Ctrl-Click on an element to jump into its sources
26 February 2016
(C) Hochschule für Technik
Fachhochschule Nordwestschweiz
7
Java Syntax
• 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 = accessible from everywhere
 static = defined on the class level (not bound to instances),
accessible over the class
 void
= does not return a result (=> procedure)
 args = method argument, array of command-line arguments
26 February 2016
(C) Hochschule für Technik
Fachhochschule Nordwestschweiz
8
Java Syntax
• 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
26 February 2016
(C) Hochschule für Technik
Fachhochschule Nordwestschweiz
9
Java Syntax
• Comments with Eclipse (operates on selection)
–
–
–
–
Source->Toggle Comment
Source->Add Block Comment
Source->Remove Block Comment
Source->Generate Element Comment
[CTRL-/ or CTRL-7]
[CTRL-SHIFT-/]
[CTRL-SHIFT-\]
[ALT-SHIFT-J]
• Formatting with Eclipse
–
–
–
–
Source->Shift Left
Source->Shift Right
Source->Correct Indentation
Source->Format
[CTRL-I]
[CTRL-SHIFT-F]
 Works on selection or, without selection, on while source
26 February 2016
(C) Hochschule für Technik
Fachhochschule Nordwestschweiz
10
Java Syntax
• System.out.println, what is this
– System
 A class containing several useful fields and methods
 Class System is defined in package java.lang (automatically available)
– out
 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
26 February 2016
(C) Hochschule für Technik
Fachhochschule Nordwestschweiz
11
Java Syntax
26 February 2016
(C) Hochschule für Technik
Fachhochschule Nordwestschweiz
12
Java Syntax
• 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
26 February 2016
(C) Hochschule für Technik
Fachhochschule Nordwestschweiz
13
Java Syntax
• Declaration
public class
staticMarks
double
{ mark(double p1, double p2, double msp) {
double static
public
en = Math.round(10*(p1+p2)/2)/10.0;//
double mark(double p1, double p2,
Erfahrungsnote
double mn = Math.round(10*msp)/10.0;
// Modulnote
double
msp){
return
double
Math.round(10*(en
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 static
public
mark(p1,
boolean
p2, msp)
passed(double
>= 3.8;
p1, double p2,
}
double msp) {
return mark(p1, p2, msp) >= 3.8;
}
}
26 February 2016
(C) Hochschule für Technik
Fachhochschule Nordwestschweiz
14
Java Syntax
• 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!");
}
}
}
26 February 2016
(C) Hochschule für Technik
Fachhochschule Nordwestschweiz
15