Informatik -- Lehrveranstaltung vom 10.06.2024
(EN google-translate)
(PL google-translate)
Schnittstellen
public interface Figur
{
public abstract float berechneUmfang();
}
public class Quadrat implements Figur
{
float kante;
public Quadrat(float kante)
{
this.kante = kante;
}
public float berechneUmfang()
{
return 4.0*kante;
}
}
public class Kreis implements Figur
{
float radius;
public Kreis(float radius)
{
this.radius = radius;
}
public float berechneUmfang()
{
return 2.0*PI*radius;
}
}
public void setup()
{
Figur[] figur = new Figur[3];
figur[0] = new Quadrat(100.0);
figur[1] = new Kreis(100.0);
figur[2] = new Quadrat(10.0);
for(int i=0;i<figur.length;i++)
println("figur["+i+"], Umfang:"+figur[i].berechneUmfang());
}
Code 0-1: Beispiel, vergl. Ausführungen im Unterricht.
Übungen zu verschiedenen Themenbereichen
Dazu am Ende lösen: Aufgabe 10.2 -- Schnittstellen, in 10_LV_03_06_2024
|
Aufgabe 11.1 C/C++, Einfache Datentypen
#include <iostream>
using namespace std;
int main(void)
{
cout<<endl<<"Hello world!"<<endl<<endl;
return 0;
}
Code 0-2: Hello World Programm in C/C++.
|
Aufgabe 11.2 C/C++, Fehlersuche
#include <iostream>
using namespace std;
int Main(void)
{
double messwerte()={1.0,2.4,3.8,4.0};
double mittelwert = 0.0;
for(int i=0,i<4,i++)
mittelwert=mittelwert+messwerte[i];
mittelwert=mittelwert/4.0
cout>>"mittelwert=">>mittelwert>>endl;
return 0;
}
Code 0-3: Fehlerhaftes Programm.
|
Aufgabe 11.3 C/C++, Ein- und Ausgabe
Schreiben Sie ein Programm, das folgenden Anforderungen genügt:
|
Aufgabe 11.4 C/C++, Arrays
#include <iostream>
using namespace std;
int main(void)
{
int arr[26];
char ascii[26];
int anzahl = sizeof(arr)/sizeof(int);
//....
return 0;
}
Code 0-4: Arrays.
|
#include <iostream>
using namespace std;
int main(void)
{
int arr[26];
char ascii[26];
int anzahl = sizeof(arr)/sizeof(int);
for(int i=0;i<anzahl;i++)
{
ascii[i] = (char)(i+(int)'A');
}
for(int i=0;i<anzahl;i++)
{
arr[i] = (i+(int)'A');
}
for(int i=0;i<anzahl;i++)
{
cout<<ascii[i]<<" | "<<arr[i]<<endl;
}
return 0;
}
Code 0-5: Musterlösung zu 11.4
Aufgabe 11.5 C/C++, Kontrollstrukturen
#include <iostream>
using namespace std;
int main(void)
{
for(int i=0;i<10;i++)
{
if(i%2==0)
cout<<"A"<<endl;
else if(i%3==0)
cout<<"B"<<endl;
else if(i%5==0)
cout<<"C"<<endl;
else
cout<<"D"<<endl;
}
return 0;
}
Code 0-6: Programm zum Thema Kontrollstrukturen.
|
Aufgabe 11.6 C/C++, Funktionen
|
#include <iostream>
using namespace std;
//Hier die fehlende Funktion implementieren.
int main(void)
{
for(int x=10;x<=15;x++)
{
for(int y=2;y<=3;y++)
{
if(xohneRestTeilbarDurchy(x,y)==true)
{
cout<<x<<" ist ohne Rest durch "<<y<<" teilbar."<<endl;
}
else
{
cout<<x<<" ist NICHT ohne Rest durch "<<y<<" teilbar."<<endl;
}
}
}
return 0;
}
Code 0-7: Programm, in der noch eine Funktion fehlt.
10 ist ohne Rest durch 2 teilbar. 10 ist NICHT ohne Rest durch 3 teilbar. 11 ist NICHT ohne Rest durch 2 teilbar. 11 ist NICHT ohne Rest durch 3 teilbar. 12 ist ohne Rest durch 2 teilbar. 12 ist ohne Rest durch 3 teilbar. 13 ist NICHT ohne Rest durch 2 teilbar. 13 ist NICHT ohne Rest durch 3 teilbar. 14 ist ohne Rest durch 2 teilbar. 14 ist NICHT ohne Rest durch 3 teilbar. 15 ist NICHT ohne Rest durch 2 teilbar. 15 ist ohne Rest durch 3 teilbar.
Code 0-8: Dies ist die Ausgabe, die das Programm produziert, wenn die Funktion richtig implementiert wurde.
#include <iostream>
using namespace std;
//Hier die fehlende Funktion implementieren.
bool xohneRestTeilbarDurchy(int a, int b)
{
if(a%b==0)
return true;
else
return false;
}
int main(void)
{
for(int x=10;x<=15;x++)
{
for(int y=2;y<=3;y++)
{
if(xohneRestTeilbarDurchy(x,y)==true)
{
cout<<x<<" ist ohne Rest durch "<<y<<" teilbar."<<endl;
}
else
{
cout<<x<<" ist NICHT ohne Rest durch "<<y<<" teilbar."<<endl;
}
}
}
return 0;
}
Code 0-9: Musterlösung 11.6
Aufgabe 11.7 C/C++, Algorithmen
#include <iostream>
using namespace std;
int iTePermutation(int stellen, int i)
{
int platz[stellen];
for(int k=0;k<stellen;k++)
platz[k]=0;
int s = stellen;
for(int k=0;k<stellen;k++)
{
int index = i%s;
int u=0;
for(int p=0;p<stellen;p++)
{
if(platz[p]==0 && u==index)
{
platz[p]=k+1;
break;
}
if(platz[p]==0)
u++;
}
i/=s;
s--;
}
int ergebnis = platz[0];
for(int k=1;k<stellen;k++)
{
ergebnis*=10;
ergebnis+=platz[k];
}
return ergebnis;
}
int main(void)
{
for(int i=0;i<6;i++)
{
cout<<iTePermutation(3,i)<<endl;
}
return 0;
}
Code 0-10: Permutationen.
|
#include <iostream>
using namespace std;
int iTePermutation(int stellen, int i)
{
int platz[stellen];
for(int k=0;k<stellen;k++)
platz[k]=0;
int s = stellen;
for(int k=0;k<stellen;k++)
{
int index = i%s;
int u=0;
for(int p=0;p<stellen;p++)
{
if(platz[p]==0 && u==index)
{
platz[p]=k+1;
break;
}
if(platz[p]==0)
u++;
}
i/=s;
s--;
}
int ergebnis = platz[0];
for(int k=1;k<stellen;k++)
{
ergebnis*=10;
ergebnis+=platz[k];
}
return ergebnis;
}
int fakultaet(int n)
{
int ergebnis = 1;
for(int p=2;p<=n;p++)
{
// 2*3*...n
ergebnis = ergebnis*p;
}
return ergebnis;
}
int main(void)
{
//int n = 5*4*3*2*1;
int n = fakultaet(5);
for(int i=0;i<n;i++)
{
cout<<i<<".: "<<iTePermutation(5,i)<<endl;
}
return 0;
}
Code 0-11: Musterlösung zu 11.7
Aufgabe 11.8 Java / Processing
Nachfolgendes Programm erzeugt mittels der entsprechenden Library ein pdf-Dokument, das als Screenshot weiter unten angezeigt wird:
import processing.pdf.*; size(600, 600); beginRecord(PDF, "line.pdf"); background(255); stroke(0, 20); strokeWeight(20.0); line(200, 0, 400, height); endRecord();
Code 0-12: OneFrame001
Bild 0-1: Screenshot zu line.pdf.
Schreiben Sie das Programm so um, dass es eine pdf Datei mit Namen ball.pdf erzeugt, die nachfolgenden Inhalt hat (gleiche Größe wie zuvor):
Bild 0-2: Screenshot zu ball.pdf.
Aufgabe 11.9 Java, Einfache Datentypen
public void setup()
{
// for(int i=0;i<65536;i++) //ALLE sichtbaren UNICODE-Zeichen
for(int i=0;i<1000;i++)
{
char c = (char)i;
if(!Character.isWhitespace(c))
{
print(c);
}
}
}
public void draw()
{
Code 0-13: }
|
Aufgabe 11.10 Java, Arrays
|
float[][] punkte = new float[10000][2];
int anzahl=0;
public void setup()
{
size(640,480);
frameRate(30);
}
public void draw()
{
background(255);
stroke(0);
for(int i=0;i<anzahl-1;i++)
line(punkte[i][0],punkte[i][1],punkte[i+1][0],punkte[i+1][1]);
}
public void mouseDragged()
{
if(anzahl==0 || (mouseX!=punkte[anzahl][0] || mouseY!=punkte[anzahl][1]))
{
punkte[anzahl][0] = mouseX;
punkte[anzahl][1] = mouseY;
anzahl++;
}
}
Code 0-14: Malprogramm.
|
Aufgabe 11.11 Java, Klassen, Objekte, Konstruktormethoden
public class Planet
{
float R; //Umlaufradius in Pixeln
float r; //Radius Planet in Pixeln
float dphi; //Änderung des Winkels pro Frame in Grad
float phi0; //Startwinkel in Grad
float phi; //Aktueller Winkel in Grad
public Planet(float R, float r, float dphi, float phi0)
{
this.R=R;
this.r=r;
this.dphi=dphi;
this.phi0=phi0;
this.phi=phi0;
}
public void draw()
{
}
public void step()
{
phi+=dphi;
}
}
Planet[] p = new Planet[3];
public void setup()
{
p[0] = new Planet(200,30,9,0);
p[1] = new Planet(300,40,6,45);
p[2] = new Planet(400,50,3,90);
size(700,700);
frameRate(30);
}
public void draw()
{
background(0);
for(int i=0;i<p.length;i++)
{
p[i].draw();
p[i].step();
}
}
Code 0-15: Planetarium
|
public class Planet
{
float R; //Umlaufradius in Pixeln
float r; //Radius Planet in Pixeln
float dphi; //Änderung des Winkels pro Frame in Grad
float phi0; //Startwinkel in Grad
float phi; //Aktueller Winkel in Grad
public Planet(float R, float r, float dphi, float phi0)
{
this.R=R;
this.r=r;
this.dphi=PI*dphi/180.0;
this.phi0=PI*phi0/180.0;
this.phi=phi0;
}
public void draw()
{
float xx = width/2 + R*cos(phi);
float yy = height/2 - R*sin(phi);
fill(255);
ellipse(xx,yy,2.0*r,2.0*r);
}
public void step()
{
phi+=dphi;
}
}
Planet[] p = new Planet[3];
public void setup()
{
p[0] = new Planet(200,30,9,0);
p[1] = new Planet(300,40,6,45);
p[2] = new Planet(400,50,3,90);
size(700,700);
frameRate(30);
}
public void draw()
{
background(0);
for(int i=0;i<p.length;i++)
{
p[i].draw();
p[i].step();
}
}
Code 0-16: Teillösung.
Aufgabe 11.12 Java, Kapselung, Modifikatoren und Zugriffsrechte
|
Aufgabe 11.13 Java, Vererbung
|
Aufgabe 11.14 Java, Polymorphismus
|
public class Planet
{
float R; //Umlaufradius in Pixeln
float r; //Radius Planet in Pixeln
float dphi; //Änderung des Winkels pro Frame in Grad
float phi0; //Startwinkel in Grad
float phi; //Aktueller Winkel in Grad
public Planet(float R, float r, float dphi, float phi0)
{
this.R=R;
this.r=r;
this.dphi=dphi;
this.phi0=phi0;
this.phi=phi0;
}
public Planet(float R, float r)
{
this.R=R;
this.r=r;
this.dphi=1.0;
this.phi0=0.0;
this.phi=phi0;
}
public Planet()
{
this.R=400.0;
this.r=30.0;
this.dphi=1.0;
this.phi0=0.0;
this.phi=phi0;
}
public void draw()
{
}
public void step()
{
phi+=dphi;
}
}
Code 0-17: Mehrere Konstruktoren in Planet.
|
Aufgabe 11.15 Java, Anwendungsprogrammierung
|
Aufgabe 11.16 C/C++, Anwendungsprogrammierung
|