Professional Documents
Culture Documents
www.ima-zlw-ifu.rwth-aachen.de
21.10.2015
Organization
Your lecturers
Contact
ase@ima.rwth-aachen.de
0241 80 911 77
Office hours: By appointment
https://www3.elearning.rwth-aachen.de/ws15/15ws-05458/
21.10.2015
Organization
Synopsis
Todays mechanical engineering relies heavily on advanced software tools. Both
industry and research expect you not only to use these tools but to design, develop
and deploy them as well. During this course we teach you how.
Java 101
Object
Oriented SE
Project
Management
in SE
Applications
Exam
Lec./Ex. 1 - 3
Lec./Ex. 4 - 6
Lec./Ex. 7 - 9
Lec./Ex. 10 - 12
09.03.2016
21.10.2015
Motivation
Advanced Software Tools are everywhere in Engineering!
From Computer Aided Design (CAD) to Robotics
21.10.2015
Motivation
Advanced Software Tools are everywhere in Engineering!
or Virtual Production Intelligence (at our Institute)
21.10.2015
Motivation
Advanced Software Tools are everywhere in Engineering!
to self-optimizing Production Systems!
21.10.2015
Whats Java?
Brief History
Java invented June 1991 by James Gosling at Sun (2010 acquired by Oracle)
Whats Java?
Its widely spread
21.10.2015
Whats Java?
A great deal of tools, libraries and frameworks
21.10.2015
Whats Java?
Motto: Write Once, Run Anywhere
Design Time
Compile
Time
Java Byte
Code
OS X & Unix
Text files
101010101
Android
.java
.class
OS-Specific
JRE
Java Source
Java
Compiler
Windows
Run Time
21.10.2015
10
Whats Java?
Terminology
JVM: Java Virtual Machine. Executes Java byte code.
JRE: Java Runtime Environment. Contains the JVM + set of libraries
JDK: Java Development Kit. Contains the JRE + Development tools (e.g. javac)
JVM Features
21.10.2015
11
Whats Java?
What are the basic requirements for writing a Java program?
A computer with a Java supported OS (e.g. Windows or OS X)
A text editor, e.g. notepad++ (but well use
IDE during this course)
The Java JDK: http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html
21.10.2015
12
Whats Java?
Writing, Compiling and Executing
1
1: Write
2: Compile
3: Yields a .class file
4: Execute
21.10.2015
13
Methods
Statements
void bar() {
void bar() {
statement1;
statement2;
statement3;
}
}
21.10.2015
}
Advanced Software Engineering
}
}
14
Method
Statement
System.out.println("Hello, World!");
}
}
21.10.2015
15
Opening
Curly Brace
public
access
21.10.2015
The name
of the class
Closing
Curly Brace
16
keyword
static
arguments of
the method
return type
public
access
the name of
the method
Opening
Curly Brace
21.10.2015
17
print to standard
output
What to print in
apostophes
System.out.println("Hello, World!");
Statement
must end in a
semicolon!
21.10.2015
18
Examples
public class HelloWorld { // Its my first class!
public static void main (String[] args) {
/* I want to
print on the command line */
System.out.println("Hello, World!");
}
}
21.10.2015
19
Variables
What are variables?
A container, a box or a cup. It contains something.
They come in different kinds
They got a name
Examples
short numberOfEngines = 5;
double temperature = 23.7;
boolean engineStarted = true;
char c = 'e';
int depth = -343535;
21.10.2015
20
Variables
Two ways of constructing variables
First declare, than initialize: int length;length = 5;
Or by defining them in one single statement: int length = 5;
Examples
short numberOfEngines = 5;
double temperature = 23.7;
boolean engineStarted = true;
char c = 'e';
int depth = -343535;
21.10.2015
21
Variables
Four Primitive Data Types in Java
boolean, char, integer and floating point
They got a default value
They only hold one value
Data Type
Example
Keyword
Logical value
true, false
boolean
Single character
a, b,
char
Whole number
1, -3, 87,
Real number
-2.6, 9.4,
float, double
22
Variables
Rules I
Variables must have a type, e.g. double!
Variables must have a name, e.g. temperature!
double temperature;
type
name
23
Variables
Three Kinds of Variables in Java
public class Cylinder {
instance variables
class/static
variables
local variables
21.10.2015
24
Variables
Defining Constants Variables
Are all-round in Mathematics, physics, engineering ...
Are declared with the keyword final
Convention for naming of constants: UPPERCASE, e.g. PI or E
Typing errors
Changing code in different places
Bad Readability
Good readability
DRY principle (dont repeat
yourself)
21.10.2015
25
Variables
Operators and Variables
Allocation (=)
Arithmetic (+, -, *, /, %)
Comparison (==, !=, <, >)
Unary (++, --)
Logical (! (not), &&, ||)
26
Variables
Operators and Variables
Allocation (=)
Arithmetic (+, -, *, /, %)
Comparison (==, !=, <, >)
Unary (++, --)
Logical (! (not), &, |)
27
Variables
Operators Priorities
How does Java evaluate an complex expression? E.g.:
int a = 5;
int b = 7;
int c = 2
a = a b + c % (a * c++); // (a is 4)
With an internal priority table! Excerpt from:
https://docs.oracle.com/javase/tutorial/java/nutsandbolts/operators.html
Priority
Operator
Unary, e.g. ++
Additive, e.g. +
11
Logical OR e.g. |
13
Allocation, e.g. =
If two operators have same priority, Java parses the expression left to right.
21.10.2015
28
Variables
Type Casts
It can be necessary to convert one type of data into an other one
There are two types of casts
Implicit type casts. Target type is computed automatically via context.
Upgrading.
Explicit type casts. Target type has to be explicitly defined. Downgrading.
Target type has to be defined in ( and ) brackets
Examples
int i = 70;
double radius = i; // radius contains 70.0
double d = 70.3456;
int num = (int)d; // num contains 70
21.10.2015
29
Variables
Implicit type cast only one way
int variable
double variable
double variable
21.10.2015
int Variable
Advanced Software Engineering
30
Variables
Type cast overview
byte
short
int
long
float
char
implicit type cast
explicit type cast
double
boolean
21.10.2015
String
31
Variables
Output
How does Java output information on the screen?
Without linefeed: System.out.print (<output>)
With: linefeed: System.out.print (<output>)
Examples
System.out.print("Hello, World");
System.out.print ("!"); // Output: Hello, World!
System.out.println ("Hello, World");
21.10.2015
32
21.10.2015
33
21.10.2015
34
21.10.2015
35
36
37
default:
}
21.10.2015
//instruction
//instruction
//instruction
//instruction
38
Output:
medium
Without
break?
39
Output:
undefined
40
41
Output:
Loop 0
Loop 1
Loop 14
System.out.println("Loop "+i);
}
}
21.10.2015
42
System.out.println("Countdown "+i);
Output:
Countdown 10
Countdown 9
Countdown 1
}
}
21.10.2015
43
Infinite
Output:
Loop
Loop
Loop
System.out.println("Loop");
}
}
21.10.2015
The three
expressions
are optional!
44
21.10.2015
45
Output:
Count 1
Count 2
Count 10
21.10.2015
46
47
Output:
Count 1
int count = 1;
do {
System.out.println("Count: " + count);
}
while (count < 1);
}
}
21.10.2015
48
21.10.2015
49
Features
if-then-else
switch
while
while-do
for
21.10.2015
50
Arrays
Recap and Motivation
Primitive data types (e.g. int) can only hold a single value
E.g. int val = 17;
Array Features
21.10.2015
51
Arrays
Two ways of array creation (Examples)
Initialize with values (e.g. six): int[] array1 = {1,2,3,4,5,6};
Declaration (e.g. length nine): int[] array2 = new int[9];
Access to elements I
public static void main(String[] args) {
int[] ar = new int[3];
ar[0] = 100;
ar[1] = 200;
ar[2] = 300;
System.out.println("Array value on pos 1:" +ar[0]);
System.out.println("Array value on pos 2:" +ar[1]);
System.out.println("Array value on pos 3:" +ar[2]);
}
21.10.2015
52
Arrays
Access to elements II
public static void main(String[] args) {
int[]
ar[0]
ar[1]
ar[2]
ar = new int[3];
= 100;
= 200;
= 300;
Determines the
size of the array
21.10.2015
53
Arrays
Multi dimensional arrays are possible
Example: Positions on a chessboard or a Matrix
Are realized with arrays of arrays
int[][] chessboard = new int[8][8];
21.10.2015
54
Arrays
For the curious mind You can declare the following arrays:
byte[] anArrayOfBytes;
short[] anArrayOfShorts;
long[] anArrayOfLongs;
float[] anArrayOfFloats;
double[] anArrayOfDoubles;
boolean[] anArrayOfBooleans;
char[] anArrayOfChars;
String[] anArrayOfStrings;
21.10.2015
55
Strings
Recap and Motivation
String features
Keyword String
Strings are denoted by quotation marks, e.g. "A String"
Example: String name = "RWTH";
21.10.2015
56
Upcoming Lecture
Outlook
Well continue with basics Java concepts
take a closer look at Methods
and well introduce the core concept of Java: Objects!
In the meantime
Make yourself familiar with todays topics
check the web for Java tutorials
theres a ton of Java Books out there.
21.10.2015
57