Benutzer-Werkzeuge

Webseiten-Werkzeuge


Seitenleiste

skriptsose2020:code-beispiele_aus_den_vorlesungnsvideos_sose2020

Dies ist eine alte Version des Dokuments!




Code-Beispiele aus den Vorlesungsvideos SoSe2020

Woche 2: Rekapitulation Woche 1 und Start in die Woche 2

Strings

Strings

int counter = 1; // counts how many times draw() has run
void setup() {
}
 
void draw() {
  String word1 = "Hallo";
  String word2 = "Person";
  System.out.println(word1 + " " + word2 + " " + counter); // alternative to println()
  counter++;
}

Konstanten

Konstanten

int pos = 0; // coordinates of the circle center
final int SIZE_CIRCLE = 100; // circle diameter
 
void setup() {
  size(600, 400);
}
 
void draw() {
  background(200);
  circle(pos, pos, SIZE_CIRCLE);
  pos++;
}


Woche 2: Arrays

Code

Code

int punkteStud1 = 11;
int punkteStud2 = 9;
int punkteStud3 = 8;
int punkteStud4 = 12;
//...
 
//Arrays anlegen
//Variante 1
int[] punkteStudVar1 = new int[4];
punkteStudVar1[0] = 11;
punkteStudVar1[1] = 9;
punkteStudVar1[2] = 8;
punkteStudVar1[3] = 12;
 
//Variante 2
int[] punkteStudVar2 = {11,9,8,12};
 
//Werte ändern und auslesen
punkteStudVar2[2] = 10;
 
punkteStudVar1[2] = punkteStudVar2[2];
 
//Länge von Arrays
//!Wichtig! Arrays fangen bei 0 an
int arrayLange = punkteStudVar2.length;
println(arrayLaenge);
 
//Zweidimensionale Arrays
int[][] StudentenHA = new int[4][2];
StudentenHA[0][0] = 11;
StudentenHA[1][0] = 9;
StudentenHA[2][0] = 8;
StudentenHA[3][0] = 12;
 
StudentenHA[0][1] = 8;
StudentenHA[1][1] = 13;
StudentenHA[2][1] = 15;
StudentenHA[3][1] = 9;


Woche 2: Schleifen

while und for

while und for

void setup(){
 
  int x=0;
  int y=-10;
  while (x>y){
    y++;
    print (y+", ");
  }
 
  println();
 
 
  for (int i = 0; i<20; i++){
    print (i +", ");
  }
 
}
 
void draw(){
}

Beispiel mit Arrays

Beispiel mit Arrays

int[] intArray0 = {0,1,2,3,4,5,6,7,8,9}; //Deklaration und initialisierung von intArray0, alle Werte sind fest.
 
int[] intArray2 = new int[10];//Deklaration und initialisierung von intArray2
 
void setup(){
 
 
  intArray2[9] = 23;
 
  println("Länge von intArray0: "+intArray0.length);
  println("Länge von intArray2: "+intArray2.length);
 
 
 
 
 
  print("intArray0:["); // Gebe "intArray0:[" aus (ohne Zeilenumbruch)
 
  int counter = 0; // Lege Variable counter zum durchgehen des Arrays-0 an
 
  while (counter < intArray0.length){ //Solange counter kleiner ist als die Arraylänge tue:
 
    print(intArray0[counter]+", "); //Gebe den Arrayeintrag an der Stelle "counter" aus.
    counter++; //Erhoehe counter um 1.
  }
 
  println("]");
 
 
  print("intArray2:[");
  for ( int i = 0; i < intArray2.length; i++){
    print(intArray2[i]+", ");
  }
  println("]");
 
 
}
 
 
void  draw(){
}

Beispiel mit zweidimensionalen Arrays

Beispiel mit zweidimensionalen Arrays

int[][] intArray0 = {{0,1,2,3,4,5,6,7,8,9,10},{0,1,2,3,4,5,6,7,8,9},{0,1,2,3,4,5,6,7,8,9},
{0,1,2,3,4,5,6,7,8,9},{0,1,2,3,4,5,6,7,8,9},{0,1,2,3,4,5,6,7,8,9},
{0,1,2,3,4,5,6,7,8,9},{0,1,2,3,4,5,6,7,8,9},{0,1,2,3,4,5,6,7,8,9},{0,1,2,3,4,5,6,7,8,9}}; 
//Deklaration und initialisierung von intArray0
 
int[][] intArray2 = new int[10][10];//Deklaration und initialisierung von intArray2
 
void setup(){
 
   //setze einige werte
  intArray2[9][2] = 92;  
  intArray2[9][9] = 99;
  intArray2[1][1] = 11;
 
  //gebe längen der arrays aus 
  println("Länge von intArray0: "+intArray0.length);
  println("Länge von intArray2: "+intArray2.length);
  println();
  println();
 
 
  /*Gebe das gesamte 2D Array intArray0 aus.*/
 
  int zeile = 0; // Lege Variable counter zum durchgehen des Arrays-0 an
  while (zeile < intArray0.length){ //Solange counter kleiner ist als die Arraylänge tue:
 
    int spalte= 0;
    while (spalte< intArray0[zeile].length){
       print(intArray0[zeile][spalte] + " "); //Gebe den Arrayeintrag an der stelle [zeile][spalte] aus.
       spalte++; //Erhoehe spalte um 1.
    }
 
    println();
    zeile++; //Erhoehe zeile um 1.
  }
 
 
  //Zeilenumbrüche für die übersichtlichkeit
  println();
  println();
 
 
 
  /*Gebe das gesamte 2D Array intArray2 aus.*/
 
  for ( int z = 0; z < intArray2.length; z++){ // z gibt die zeile an.
 
    for (int s = 0; s <intArray2[z].length; s++){ //s gibt die Spalte an.
      print(intArray2[z][s] + "\t"); // Gebe den Eintrag [z][s] aus.
    }
 
    println();
  }
 
 
}
 
 
void  draw(){
 
}


Woche 2: Methoden

Beispeiele von Methoden

Beispeiele von Methoden

void setup() {
  boolean result = isEven(10);
}
 
void draw() {
  int[] pointerCoords= getCoords();
  println(pointerCoords[0] + " " + pointerCoords[1]);
}
 
// Returns the x and y of the mouse 
int[] getCoords() {
  int[] coords = {mouseX, mouseY};
  return coords;
}
 
// Determines if the given number if even. Returns true if even, false otherwise.
boolean isEven(int number) {
  if (number%2==0) {
    return true;
  } else {
    return false;
  }
}
 
// Returns true if the first number in array if greater than the second number, false otherwise. The array should have the length = 2 
boolean isGreater(int[] numbers) {
  if (numbers[0] > numbers[1]) {
    return true;
  } else {
    return false;
  }
}
 
// Calculates the sum of two numbers
int sumNumbers(int number1, int number2) {
  int result = number1 + number2;
  return result;
}
 
// Prints a greeting according to the person's index
void greet(int index) {
  println("Hallo Person " + index);
}
 
// Prints a greeting
void greet() {
  println("Hallo Welt");
}


Woche 3: Steuerung eines zweirädigen Roboters

Über globale Variablen

Über globale Variablen

float posX = 300; // x coordinate of the circle center
float posY = 200; // y coordinate of the circle center
float angle = PI/4; // current direction of the robot
final int RADIUS = 50; // radius of the robot
 
 
void setup() {
  size(600, 400);
}
 
void draw() {
  background(200);
  show();
  move(4);
  turn(0.05);
}
 
// Turns the robot by the given angle value
void turn(float angleDiff) {
  angle = angle + angleDiff;
}
 
// Moves the robot forward by the given distance
void move(float distance) {
  posX = posX + distance * cos(angle);
  posY = posY + distance * sin(angle);
}
 
// Draws the robot
void show() {
  circle(posX, posY, RADIUS*2); 
  line(posX, posY, posX + RADIUS * cos(angle), posY + RADIUS * sin(angle));
}


Woche 3: Klassen

Haupt-Sketch

Haupt-Sketch

Robot robot1; // large robot
Robot robot2; // small robot
 
void setup() {
  size(600, 400);
  robot1 = new Robot(300, 200, PI/4, 50); 
  robot2 = new Robot(500, 200, PI, 20);
}
 
void draw() {
  background(200);
  robot1.show();
  robot2.show();
  robot1.move(4);
  robot1.turn(0.05);
  robot2.move(2);
  float distance = robot1.calculateDistance(robot2);
  if (distance <= 0) { 
  // dies ist anders als im Vorlesungs-Video, weil die Radienlängen in der Methode calculateDistance() abgezogen werden
    robot1.turn(PI);
    robot2.turn(PI);
  }
}

Klasse Robot

Klasse Robot

class Robot {
 
  // 1. Attribute
  float posX; // x coordinate of the circle center
  float posY; // y coordinate of the circle center
  float angle; // current direction of the robot
  int radius; // radius of the robot
 
 
  // 2. Konstruktor, x: x coordinate of the center, y: y coordinate of the center, direction: where the roboter is looking towards, size: radius of the robot
  Robot(float posX, float posY, float direction, int size) {
    this.posX = posX;
    this.posY = posY;
    this.angle = direction;
    this.radius = size;
  }
 
  // 3. Methoden
  // Turns the robot by the given angle value
  void turn(float angleDiff) {
    this.angle = this.angle + angleDiff;
  }
 
  // Moves the robot forward by the given distance
  void move(float distance) {
    posX = posX + distance * cos(angle);
    posY = posY + distance * sin(angle);
  }
 
  // Draws the robot
  void show() {
    circle(posX, posY, radius*2); 
    line(posX, posY, posX + radius * cos(angle), posY + radius * sin(angle));
  }
 
  // Calculates distance from this robot to the other
  float calculateDistance(Robot other) {
    float deltaX = this.posX - other.posX;
    float deltaY = this.posY - other.posY;
    float distance = sqrt(deltaX*deltaX + deltaY*deltaY);
    return distance - this.radius - other.radius; // dies ist anders als im Vorlesungs-Video
  }
}


skriptsose2020/code-beispiele_aus_den_vorlesungnsvideos_sose2020.1589375461.txt.gz · Zuletzt geändert: 2020/05/13 15:11 von d.golovko