Prelimary Discussion Exercise 1

Werbung
Exercise 1: Intro to Java & Eclipse
• 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.
• ...
24 February 2017
(C) Hochschule für Technik
Fachhochschule Nordwestschweiz
1
No testat, but exercises are exam material
• Exercises:
– Exercises 1-3:
– Exercises 4-11:
Recomended to do alone
Teams of two are recomended (it is a lot
of work)
• Hand-in (by Email)
– Stefan Stevsic, [email protected]
– Seonwook Park, [email protected]
– Deadline:
Two weeks after hand-in (there are exceptions)
 Assistants may not be able to provide feedback for late submissions
– Ask specific questions
24 February 2017
(C) Hochschule für Technik
Fachhochschule Nordwestschweiz
2
Today
• Installation of Java + Eclipse
• First steps with eclipse (Demo)
• Hello World in Java
• Vektor-Class
24 February 2017
(C) Hochschule für Technik
Fachhochschule Nordwestschweiz
3
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
– 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.
24 February 2017
(C) Hochschule für Technik
Fachhochschule Nordwestschweiz
4
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
24 February 2017
(C) Hochschule für Technik
Fachhochschule Nordwestschweiz
5
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 static void main(String args[]) {
System.out.println(”Hello World!”);
}
}
object method
class
‘System’ static field (class variable)
public
visibility (all)
24 February 2017
(C) Hochschule für Technik
Fachhochschule Nordwestschweiz
6
Start java from the command line
• The Java VM (java.exe) as well as the compiler (javac.exe) can be
started from the console:
24 February 2017
(C) Hochschule für Technik
Fachhochschule Nordwestschweiz
7
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.8.0_121\bin;”
24 February 2017
(C) Hochschule für Technik
Fachhochschule Nordwestschweiz
8
Eclipse – IDE
• Download:
http://www.eclipse.org/downloads
recomended
24 February 2017
• 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)
(C) Hochschule für Technik
Fachhochschule Nordwestschweiz
9
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
24 February 2017
(C) Hochschule für Technik
Fachhochschule Nordwestschweiz
10
Eclipse
24 February 2017
(C) Hochschule für Technik
Fachhochschule Nordwestschweiz
11
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...
24 February 2017
Stefan Müller· Software Design – Übungsstunde · Frühlingssemester 2012
(C) Hochschule für Technik
Fachhochschule Nordwestschweiz
12
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:
24 February 2017
(C) Hochschule für Technik
Fachhochschule Nordwestschweiz
13
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 assistantsto 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
24 February 2017
(C) Hochschule für Technik
Fachhochschule Nordwestschweiz
14
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.
• Seperation of words by an upper-case letter:
get_first_element()
• Brackets:
void foo()
{
if (x>y)
{
}
else
{
}
}
getFirstElement()
void foo() {
if (x>y) {
} else {
}
}
• Indent code blocks
24 February 2017
(C) Hochschule für Technik
Fachhochschule Nordwestschweiz
15
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);
24 February 2017
Output:
[email protected]
(Classe+hash value)
(C) Hochschule für Technik
Fachhochschule Nordwestschweiz
16
A class for vector operations
• Encapsulation: Class “Vector” capsulates vector operations in
R3
• Possible operations (not complete):
–
–
–
–
–
–
–
Constructors: Construct vector from the three components
R3 x R3  R3 : addition, subtraction
R3 x R  R3 : scaling
R3  R : norm
getter and setter methods for the individual components.
normalizing
Output as string
• Source code available online after the exercise
24 February 2017
(C) Hochschule für Technik
Fachhochschule Nordwestschweiz
17
Basic structure
public class Vector {
private double x, y, z;
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);
}
}
24 February 2017
(C) Hochschule für Technik
Fachhochschule Nordwestschweiz
18
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());
}
}
24 February 2017
(C) Hochschule für Technik
Fachhochschule Nordwestschweiz
19
Herunterladen