• Keine Ergebnisse gefunden

BASICS IN PROGRAMMING BASICS IN PROGRAMMING

N/A
N/A
Protected

Academic year: 2022

Aktie "BASICS IN PROGRAMMING BASICS IN PROGRAMMING"

Copied!
25
0
0

Wird geladen.... (Jetzt Volltext ansehen)

Volltext

(1)

BASICS IN PROGRAMMING BASICS IN PROGRAMMING

INTERFACES INTERFACES

Created by Beat Rossmy

(2)

SCOPE SCOPE

1. 1.

2.

3.

4.

2. 1.

2.

3.

Variables and Data Types

Variables Datatypes

Boolean(-operators) Colors

Control Structures

If and Else Loops Functions

3. 1.

2.

4. 1.

2.

3.

Arrays

Arrays Iterate

Classes

Class

Inheritance Interfaces

(3)

VARIABLES AND DATA VARIABLES AND DATA

TYPES

TYPES

(4)

VARIABLES VARIABLES

Declaration: give values a

keyword/name (variable) to make them "memorize-able".

Initilization: give these variables initial values.

Usage: use variables instead of static values (e.g arguments). PC looks up the values of variables during execution.

int x;

void setup () { size(600,600);

x = 100;

}

void draw () {

background(0);

rect(x,200,200,200);

}

(5)

VARIABLES VARIABLES

int x ;

Datatype:

variables can be of different types.

Name:

names can be single letters but also words. Always start with lowercase.

End

(6)

DATATYPES DATATYPES

If we declare variables we have to specify their types.

Different datatypes require different space in the working memory.

Integer Float String

Character Boolean

int i = 10;

float f = 3.33;

String s = "hello world!";

char c = 'a';

boolean b = false;

(7)

BOOLEAN(-OPERATORS) BOOLEAN(-OPERATORS)

Statements generate boolean values.

greater than x > 100 greater or equal x >= 100

equal x == 100

smaller or equal x <= 100 smaller than x < 100

unequal x != 100

(8)

BOOLEAN(-OPERATORS) BOOLEAN(-OPERATORS)

Booleans can be combined or manipulated to new boolean values.

and true && true == true true && false == false false && true == false false && false == false or true || true == true

true || false == true false || true == true false || false == false not !true == false

!false == true

(9)

COLORS COLORS

Colors are either entered as gray values or RGB values.

The number of arguments specifies the color type.

Each color channel can take values from 0-255.

Gray

RGB

Transparent colors.

background(0);

fill(123);

stroke(255);

background(255,0,0);

fill(0,255,0);

stroke(0,0,255);

fill(r,g,b,a);

fill(g,a);

(10)

CONTROL STRUCTURES

CONTROL STRUCTURES

(11)

IF AND ELSE IF AND ELSE

Based on a condition we can execute specific code sections.

if the condition is true execute {...}. else execute {***}

void draw () {

background(0);

x = x+1;

if (x>100) {...}

else {***}

rect(x,200,200,200);

}

(12)

IF AND ELSE IF AND ELSE

Keyword Condition:

a statement that describes a certain state. A statement is either true or false.

Consequence:

if the condition is true the included commands are performed and otherwise skipped.

if (x>0)

{...}

(13)

LOOPS LOOPS

Loops help us to solve recurring patterns.

The three instructions in the () define the execution.

for (int i=0; i<10; i = i+1) {

ellipse(300,300,200-10*i,200-10*i);

}

(14)

LOOPS LOOPS

for (int i=0; i<100; i=i+1) {...}

Start:

initial value of the

counter.

End:what is the maximum value of the counter?

Steps:

how to

increment after each loop.

Body:

commands to be

performed.

(15)

FUNCTIONS FUNCTIONS

Functions allow us to use generalized sets of instructions.

A set of commands is performed when calling the function, using the attributes from the attribute list.

Functions can return values of datatypes or do not return any value (void).

void printRandomCharacters () {

for (int i=0; i<100; i = i+1) { print(char((int)random(255)));

} }

int double (int v) { return 2*v;

}

(16)

ARRAYS

ARRAYS

(17)

ARRAYS ARRAYS

In an array you can store multiple values of one datatype.

You can access these values by referencing the array and the specific index.

int [] a;

void setup () { size(600,600);

a = new int [3];

a [0] = 255;

a [1] = 100;

a [2] = 30;

}

void draw () {

background(a[0],a[1],a[2]);

}

(18)

ITERATE ITERATE

Use loops to automatically iterate over all elements of an array.

Use this technique for: initialization or handling all elements..

int [] a;

void setup () { size(600,600);

a = new int [3];

for (int i = 0; i<3; i = 1+1) { a[i] = (int)random(255);

} }

void draw () {

background(a[0],a[1],a[2]);

}

(19)

CLASSES

CLASSES

(20)

CLASS CLASS

A class allows us to define data structures.

The class (Ball) is the abstract description and the objects (b) are

instances of that class.

Each objects contains it own set of variables

defined in the class as fields.

Ball b;

void setup () { size(600,600);

b = new Ball (235,237,52);

}

void draw () {...}

class Ball { float x;

float y;

float d;

Ball (float x, float y, float d) { this.x = x;

this.y = y;

this.d = d;

} }

(21)

CLASS CLASS

keyword class + classname Fields

Constructor: classname + arguments

Methods:

End of class.

class Ball {

float x;

float y;

float d;

public Ball (intx ,int y, int d) { this.x = x;

this.y = y;

this.d = d;

}

void move () { x = x+1;

y = y+1;

} }

(22)

INHERITANCE INHERITANCE

Inheritance enables us to define functionalities and datastructures once that are shared by different classes.

The child class (B) inherits all the fields and

methods of its parent class (A).

A class can have multiple children but only one

parent.

class A { float x;

...

void doSomething () {...}

}

class B extends A { // float x;

...

// void doSomething () {...}

}

A a;

B b;

void setup () { a = new A();

b = new B();

a.doSomething();

b.doSomething();

}

(23)

INHERITANCE INHERITANCE

keyword extends + parentname

Fields

Constructor: call parent constructor

Methods:

End of class.

class B extends A {

float x;

float y;

// fields of A are inherited

public B (int x ,int y) {

super(...); // call A constructor this.x = x;

this.y = y;

}

void doSomething () {...}

// methods of A are inherited }

(24)

INTERFACES INTERFACES

Interfaces define methods that have to be

implemented by the

classes implementing this interface.

The interface specifies the return type, the name and the arguments, but not the commands that are performed.

A class can implement multiple interfaces.

interface Doable {

void doSomething ();

void doSomethingElse (int x);

}

class A implements Doable { ...

void doSomething () {...}

void doSomethingElse (int x) {...}

}

class Z { ...

void doSomething () {...}

void doSomethingElse (int x) {...}

}

(25)

REFERENCES

REFERENCES

Referenzen

ÄHNLICHE DOKUMENTE

The challenges of modern times do not stop at the ceramics and refrac- tory industry: Refractory linings in kilns should improve the energy foot- print, their components should be as

Zeigen Sie, dass das Parkplatzproblem genau eine L¨ osung der am Ende von Kapitel 3.1 spezifizierten Art f¨ ur alle Eingabegr¨ oßen besitzt, die die Vorbedingungen erf¨ ullen?.

Gehen Sie analog vor beim Beispiel zur Berechnung einer nichtnega- tiven ganzzahligen Potenz einer ganzen Zahl nach dem Algorithmus von Seite 29 der

Underflow soll nur in dem Falle auftreten d¨ urfen, wenn das (exakte) Ergebnis im Absolutwert kleiner als FLT MIN ist5.

Implement the required classes in Java without the support of an IDE (e.g. on a sheet of paper). All attributes should be accessible only using getters and setters.. b) Not only

Bei solchen Untersuchungen wurde bereits festgestellt, dass nach einer relativ kurzen Einarbeitungszeit in den neuen Programmierstil zwar der Gesamt- aufwand

None of the LINC instructions makes explicit reference to the Memory Address register or Memory Contents register; rather, in referring to memory.. register X,

“the importance of the support of local communities, private sector, civil society and media for increasing awareness about the threats of terrorism and more effectively