Übungsaufgaben Java Vorlesung Cloud Computing SoSe 2009 Cloud Computing SoSe 2009 Dr. M.Kunze 1
Ü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 2
Ü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 3
Übung J2b Deklaration von Variablen Datentypen: byte, short, long, int, float, double, char, boolean public class J2b public static void main (String argv[ ]) int i; float x; // 32 bit Fixpunkt, 32 bit Gleitpunkt i = 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 4
Ü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 5
Übung J4 Cloud Computing SoSe 2009 Dr. M.Kunze 6
Ü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 7
Übung J4b Grafische Darstellung mit Bibliothek: Installieren Sie dazu die IOTools von 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 8
Ü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 9
Übung J4b (c td) Cloud Computing SoSe 2009 Dr. M.Kunze 10
Eclipse: Integrierte Entwicklungsumgebung (IDE) http://www.eclipse.org/ Cloud Computing SoSe 2009 Dr. M.Kunze 11
Übung J5: Iterierte Funktionssysteme Cloud Computing SoSe 2009 Dr. M.Kunze 12
Übung J5 (continued) Cloud Computing SoSe 2009 Dr. M.Kunze 13
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 14
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 15
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 16
Übung J6: Klimasimulation Cloud Computing SoSe 2009 Dr. M.Kunze 17
Übung J6 (continued) Cloud Computing SoSe 2009 Dr. M.Kunze 18
Übung J6 (continued) Cloud Computing SoSe 2009 Dr. M.Kunze 19
Übung J6 (continued) Schreiben SIe ein Programm zur Lösung der gekoppelten Gleichungen und testen Sie es mit den folgenden Szenarien: Cloud Computing SoSe 2009 Dr. M.Kunze 20
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 21
public static void main(string[] argv) Klima gegenwart = new Klima("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 = new Klima("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 22
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 Bodentemperatur double Ta; // Anfangswert fuer Atmosphaerentemperatur double r1; // Reflexionskoeff. Atmosphaere im kurzwell.bereich double r2; // Reflexionskoeff. Boden im kurzwell.bereich double r3; // Reflexionskoeff. Atmosphaere im langwell.bereich double t1; // Transmissionskoeff. Atmosphaere im kurzwell.bereich double 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 23
// Energiebilanz der Atmosphaere double 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 Bodens double 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 24
// 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);// Funktionswerte f1 = df1(tb,r3,t2); f2 = df2(ta);// Part.Ableitungen nicht g1 = dg1(tb,r3); g2 = dg2(ta);// abhaengig von allen Variablen det = f1*g2-f2*g1;// Jacobi-Determinante der part.ableit. delta1 = gg*f2 -ff*g2;// Aenderung Tb delta2 = ff*g1 -gg*f1;// Aenderung Ta Tb += delta1/det;// Korrektur Tb Ta += delta2/det;// Korrektur Ta return Tb; Cloud Computing SoSe 2009 Dr. M.Kunze 25
// part.ableitung nach df/dtb double df1(double Tb,double r3,double t2) return -c-4.*sigma*math.pow(tb,3.)*(1.-t2-r3); // part.ableitung nach df/dta double df2(double Ta) return +c+8.*sigma*math.pow(ta,3.); // part.ableitung nach dg/dtb double dg1(double Tb,double r3) return +c+4.*sigma*math.pow(tb,3.)*(1.-r3); // part.ableitung nach dg/dta double dg2(double Ta) return -c-4.*sigma*math.pow(ta,3.); Cloud Computing SoSe 2009 Dr. M.Kunze 26
Übung J7: Google App Engine Anleitung unter http://code.google.com/appengine/docs/java/tools/eclipse.html Installation des Google App Engine Plugin für Eclipse durch Installation 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 27
Guestbook-Applikation Cloud Computing SoSe 2009 Dr. M.Kunze 28
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 29
Editieren von GegenwartServlet.java Cloud Computing SoSe 2009 Dr. M.Kunze 30
Editieren von WAR index.html Cloud Computing SoSe 2009 Dr. M.Kunze 31
Editieren von WAR web.xml Cloud Computing SoSe 2009 Dr. M.Kunze 32
Ausführung Im Browser http://localhost:8080/ Cloud Computing SoSe 2009 Dr. M.Kunze 33
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 34
Verwaltung der Applikationen Monitoring verfügbar unter http://appengine.google.com/ Cloud Computing SoSe 2009 Dr. M.Kunze 35