Cloud Computing | SoSe 2009 | Dr. M.Kunze 1
ÜbungsaufgabenJava
Vorlesung “Cloud Computing”SoSe 2009
Cloud Computing | SoSe 2009 | Dr. M.Kunze 2
Übung J1
• Installieren Sie das Java Software-Entwicklungspaket JDK 6 Update 13– http://java.sun.com/javase/downloads/index.jsp
– Evtl. muss in der Systemsteuerung bei den Umgebungsvariablen der Pfad „C:\Programme\Java\jdk1.6.0_13\bin“ ergänzt werden, damit die Programme in der Konsole gefunden werden
Cloud Computing | SoSe 2009 | Dr. M.Kunze 3
Übung J2a
• Das erste Java Programm– Öffnen Sie ein Konsolfenster
– Schreiben Sie ein kleines Programm mit „edit HelloCloud.java“
public class J2a
{ public static void main (String argv[ ])
{
System.out.println("Hello Cloud!");
}}
– Übersetzen Sie es mit „javac J2a.java“– Führen Sie es aus mit „java J2a“
Cloud Computing | SoSe 2009 | Dr. M.Kunze 4
Übung J2b
• Deklaration von Variablen– Datentypen: byte, short, long, int, float, double, char, boolea n
public class J2b {
public static void main (String argv[ ])
{
int i; float x; // 32 bit Fixpunkt, 32 bit Gleitpunkti = 3;
x = (float) 4.0; // Umwandlung in einfache Genauigkeit
System.out.println(" i = " + i + ", x = " + x);x = x + i;
i++; // Inkrementiert i um 1
System.out.println(" i = " + i + ", x = " + x);
}}
Welchen Wert hat x, wenn man schreibt: x = x + i++; ?
Cloud Computing | SoSe 2009 | Dr. M.Kunze 5
Übung J3
• Iteration: Programmschleifen (Bsp. Sinustabelle)
public class J3
{
public static void main (String argv[ ]) {
int i; double x = 0.0;
for (i = 0; i<10; i++){
System.out.println(" x = " + x + ", sin(x) = " + Math.sin(x) );
x += 0.1;}
}
}
Cloud Computing | SoSe 2009 | Dr. M.Kunze 6
Übung J4
Cloud Computing | SoSe 2009 | Dr. M.Kunze 7
Übung J4a
• Eigene Member-Funktionen aufrufen
public class J4a {
public static void main (String argv[ ]) {
int i; double x = -5.0, dx = 0.1, time = 0.3;for (i = 0; i<150; i++) {
System.out.println(" x = " + x + ", psi(x,t) = " + psi(x,time) );
x += dx;}
}
private static double psi(double x, double t) {
double nenner = 3*cosh(x-28*t)+cosh(3*x-36*t);return -12*(4*cosh(2*x-8*t)+cosh(4*x-64*t))/(nenner*nenner);
}
private static double cosh(double x) {return 0.5 * (Math.exp(x) + Math.exp(-x));
}
}
Cloud Computing | SoSe 2009 | Dr. M.Kunze 8
Übung J4b
• Grafische Darstellung mit Bibliothek: Installieren Sie dazu die IOToolsvon http://www.grundkurs-java.de/software.htm
import Prog1Tools.*; // Importieren der Bibliothek
// Plotter Interface implementieren
public class J4b implements Plottable {
static double xmin = -5.0, xmax = 10.0, time = 0.3;
public double inf () { return xmin; }public double sup () { return xmax; }
public double x (double p) { return p; }
public double y (double p) { return psi(p,time); }
Cloud Computing | SoSe 2009 | Dr. M.Kunze 9
Übung J4b (c‘td)
• Hier kommt das Hauptprogramm, die den Plotter instanziert
public static void main (String argv[ ]) {
plotter = new Plotter( new J4b(), "Soliton" );
plotter.setNumOfPoints ( 100 );plotter.showGrid( true );
plotter.adjustGrid( 0.2, 0.2 );
plotter.setVisible( true );
}public double psi(double x, double t) {
double nenner = 3*cosh(x-28*t)+cosh(3*x-36*t);
return -12*(4*cosh(2*x-8*t)+cosh(4*x-64*t))/(nenner*nenner);}
public double cosh(double x) {
return 0.5 * (Math.exp(x) + Math.exp(-x));}
}
Cloud Computing | SoSe 2009 | Dr. M.Kunze 10
Übung J4b (c‘td)
Cloud Computing | SoSe 2009 | Dr. M.Kunze 11
Eclipse: Integrierte Entwicklungsumgebung (IDE)http://www.eclipse.org/
Cloud Computing | SoSe 2009 | Dr. M.Kunze 12
Übung J5: Iterierte Funktionssysteme
Cloud Computing | SoSe 2009 | Dr. M.Kunze 13
Übung J5 (continued)
Cloud Computing | SoSe 2009 | Dr. M.Kunze 14
import java.applet.*;import java.awt.*;
public class IFS extends Applet {private double[] a,b,c,d,e,f,p;private static IFS ifs;private static int maxIter = 30000;
public IFS() {}
public IFS(double[] a,double[] b,double[] c,double[] d, double[] e,double[] f,double[] p){
this.a = a;this.b = b;this.c = c;this.d = d;this.e = e;this.f = f;this.p = p;
}
Cloud Computing | SoSe 2009 | Dr. M.Kunze 15
void draw(Graphics g){
double x,x1,y,pk, xs=40., ys=40.;int xd,yd;x = y = 0;for (int i=1; i<=maxIter; i++){
int k;pk = Math.random();if (pk<= p[0]) k = 0;else if (pk<=p[1]) k = 1;else if (pk<=p[2]) k = 2;else k = 3;x1 = a[k]*x + b[k]*y + e[k];y = c[k]*x + d[k]*y + f[k];x = x1;xd = (int )(x*xs+200.);yd = (int )(400.-y*ys);g.drawLine(xd,yd,xd,yd);
}}
Cloud Computing | SoSe 2009 | Dr. M.Kunze 16
public void init(){
double [] a = {0,0.197,-0.15,0.849};double [] b = {0,-0.226,0.283,0.037};double [] c = {0,0.226,0.26,-0.037};double [] d = {0.16,0.197,0.237,0.849};double [] e = {0,0,0,0};double [] f = {0,1.6,0.44,1.6};double [] p = {0.03,0.16,0.27,1.0};
ifs = new IFS(a,b,c,d,e,f,p);}
public void paint(Graphics g){
g.drawString("Iterierte Funktionssysteme: ",0,20);ifs.draw(g);
}}
Cloud Computing | SoSe 2009 | Dr. M.Kunze 17
Übung J6: Klimasimulation
Cloud Computing | SoSe 2009 | Dr. M.Kunze 18
Übung J6 (continued)
Cloud Computing | SoSe 2009 | Dr. M.Kunze 19
Übung J6 (continued)
Cloud Computing | SoSe 2009 | Dr. M.Kunze 20
Übung J6 (continued)
Schreiben SIe ein Programm zur Lösung der gekoppelten Gleic hungenund testen Sie es mit den folgenden Szenarien:
Cloud Computing | SoSe 2009 | Dr. M.Kunze 21
public class Klima{
Simulation s;double temperatur = 0.;String name;
Klima(String Name,double TB,double TA,double R1,double R2,double R3,double T1,double T2){
name = Name;s = new Simulation(TB,TA,R1,R2,R3,T1,T2);
}
public void berechne(){
double t;do{
t = temperatur;temperatur = s.iteration();
}while (Math.abs(t - temperatur) > 0.001);
}
double temperatur() { return temperatur; }
String name() { return name; }
String print() { return (name+"\t- Temperatur: "+temperatur+" K"); }
Cloud Computing | SoSe 2009 | Dr. M.Kunze 22
public static void main(String[] argv){
Klima gegenwart = newKlima("Gegenwart",280.,320.,0.30,0.11,0.31,0.53,0.06);
gegenwart.berechne();System.out.println(gegenwart.name()+":"+gegenwart.temperatur());
Klima komet = new Klima("Komet",280.,320.,0.36,0.11,0.37,0.43,0.05);
komet.berechne();System.out.println(komet.name()+":"+komet.temperatur());
Klima treibhaus = newKlima("Treibhauseffekt",280.,320.,0.30,0.10,0.34,0.53,0.05);
treibhaus.berechne();System.out.println(treibhaus.name()+":"+treibhaus.temperatur());
}}
Cloud Computing | SoSe 2009 | Dr. M.Kunze 23
class Simulation{
// Physikalische Konstanten
final static double sigma = 5.6703e-8;// Stefan-Boltzmann-K.final static double S = 1360.;// Solarkonstante(W/m^2)final static double c = 2.6;// Konstante der Konvektion(W/m^2/K)
// Simulationsparameter
double Tb; // Anfangswert fuer Bodentemperaturdouble Ta; // Anfangswert fuer Atmosphaerentemperaturdouble r1; // Reflexionskoeff. Atmosphaere im kurzwell.Bereichdouble r2; // Reflexionskoeff. Boden im kurzwell.Bereichdouble r3; // Reflexionskoeff. Atmosphaere im langwell.Bereichdouble t1; // Transmissionskoeff. Atmosphaere im kurzwell.Bereichdouble t2; // Transmissionskoeff. Atmosphaere im langwell.Bereich
Simulation(double TB,double TA,double R1,double R2,double R3,double T1,double T2){
Tb = TB; Ta = TA; r1 = R1; r2 = R2; r3 = R3; t1 = T1; t2 = T2;}
Cloud Computing | SoSe 2009 | Dr. M.Kunze 24
// Energiebilanz der Atmosphaeredouble f(double Tb,double Ta,double r1,double r2,double
r3,double t1,double t2){return -(1.-r1-t1+r2*t1)*S/4.-c*(Tb-Ta)-
sigma*Math.pow(Tb,4.)*(1.-t2-r3)+2.*sigma*Math.pow(Ta,4.);
}
// Energiebilanz des Bodensdouble g(double Tb,double Ta,double r2,double r3,double t1){return (-t1)*(1-r2)*S/4.+c*(Tb-
Ta)+sigma*Math.pow(Tb,4.)*(1.-r3)-sigma*Math.pow(Ta,4.);}
Cloud Computing | SoSe 2009 | Dr. M.Kunze 25
// Newton-Raphson-Iteration
double iteration() {
double det,delta1,delta2,ff,f1,f2,gg,g1,g2;ff = f(Tb,Ta,r1,r2,r3,t1,t2);gg = g(Tb,Ta,r2,r3,t1);// Funktionswertef1 = df1(Tb,r3,t2); f2 = df2(Ta);// Part.Ableitungen nichtg1 = dg1(Tb,r3); g2 = dg2(Ta);// abhaengig von allen Variablendet = f1*g2-f2*g1;// Jacobi-Determinante der part.Ableit.delta1 = gg*f2 -ff*g2;// Aenderung Tbdelta2 = ff*g1 -gg*f1;// Aenderung TaTb += delta1/det;// Korrektur TbTa += delta2/det;// Korrektur Tareturn Tb;
}
Cloud Computing | SoSe 2009 | Dr. M.Kunze 26
// part.Ableitung nach df/dTbdouble df1(double Tb,double r3,double t2){
return -c-4.*sigma*Math.pow(Tb,3.)*(1.-t2-r3);}
// part.Ableitung nach df/dTadouble df2(double Ta) {
return +c+8.*sigma*Math.pow(Ta,3.);}
// part.Ableitung nach dg/dTbdouble dg1(double Tb,double r3) {
return +c+4.*sigma*Math.pow(Tb,3.)*(1.-r3);}
// part.Ableitung nach dg/dTadouble dg2(double Ta) {
return -c-4.*sigma*Math.pow(Ta,3.);}
}
Cloud Computing | SoSe 2009 | Dr. M.Kunze 27
Übung J7: Google App Engine
• Anleitung unterhttp://code.google.com/appengine/docs/java/tools/ec lipse.html
• Installation des Google App Engine Plugin für Eclipse durchInstallation des Software Updates von http://dl.google.com/eclipse/plugin/3.4
• Ausführen der Guestbook-Applikation• File -> New -> Web Application Project • Project Name: Guestbook• Run -> Run as -> Web Application
Führt das Programm auf einem lokalen Webserver aus
http://localhost:8080/Guestbook.html
Cloud Computing | SoSe 2009 | Dr. M.Kunze 28
Guestbook-Applikation
Cloud Computing | SoSe 2009 | Dr. M.Kunze 29
• Anlegen einer neuen Web-Applikation mit dem Namen “CloudVorlesung”– Achtung: GWT-Button deselektieren und App-Engine selektieren !
• Einfügen der Klimasimulation als Package “klima”• Erstellen eines Servlet: GegenwartServlet.java
package klima;
import java.io.IOException;import javax.servlet.http.*;
@SuppressWarnings("serial")public class GegenwartServlet extends HttpServlet {
public void doGet(HttpServletRequest req, HttpServletResponse resp)throws IOException {
resp.setContentType("text/plain");resp.getWriter().println("Klimasmulation");
Klima gegenwart = new Klima("Gegenwart",280.,320.,0.30,0.11,0.31,0.53,0.06);gegenwart.berechne();resp.getWriter().println(gegenwart.name()+":"+gegenwart.temperatur());
}}
Cloud Computing | SoSe 2009 | Dr. M.Kunze 30
Editieren von GegenwartServlet.java
Cloud Computing | SoSe 2009 | Dr. M.Kunze 31
Editieren von WAR index.html
Cloud Computing | SoSe 2009 | Dr. M.Kunze 32
Editieren von WAR web.xml
Cloud Computing | SoSe 2009 | Dr. M.Kunze 33
Ausführung• Im Browser http://localhost:8080/
Cloud Computing | SoSe 2009 | Dr. M.Kunze 34
Ausführen auf Google-Infrastruktur• App Engine Button drücken• Application ID auswählen (Muss zuvor angelegt werden, z.B. cloudvorlesung)
• Deploy drücken + Ausführen unter http://cloudvorlesung.appspot.com
Cloud Computing | SoSe 2009 | Dr. M.Kunze 35
Verwaltung der Applikationen• Monitoring verfügbar unter http://appengine.google.com/