Programmieren 2 Übung Semesterwoche 4

Ähnliche Dokumente
Übung Nr. 20 Freitag, 09:00-11:00 Uhr in E3.301

GUI-Programmierung. Teil I. Unterlagen zum Modul OOP mit Java V by MOU2/BFH-TI. Berner Fachhochschule Technik und Informatik

GUI Programmierung mit JAVA Swing

Programmieren 2 Selbststudium Semesterwoche 4

Probeklausur Informatik 2 Sommersemester 2013

Grafische Benutzeroberflächen mit Swing (2)

Programmieren 2 Übung Semesterwoche 6

DHBW Karlsruhe, Angewandte Informatik Programmieren in JAVA W. Geiger, T. Schlachter, C. Schmitt, W.

Aufgabe 1: Objekte, Klassen, Vererbung (15 Punkte)

Prof. Dr. Oliver Haase Karl Martin Kern Achim Bitzer. Programmiertechnik GUI-Programmierung mit Swing

EAD II Übung 5. Graphische Benutzungsoberfläche mit BlueJ

Programmieren in Java

Ereignisse (Events) in Java

Einstieg in die Informatik mit Java

Einstieg in die Informatik mit Java

DHBW Karlsruhe, Vorlesung Programmieren, Events Musterlösung

Objektorientierte Software-Entwicklung

NTB Druckdatum:

Grundlagen der Programmierung (GP) Prof. Dr. H. Schlingloff Joachim Hänsel

Fakultät Angewandte Informatik Programmierung verteilter Systeme Übungen zur Vorlesung Informatik II, Blatt 8

Einführung in die Informatik: Programmierung und Software-Entwicklung, WS 15/16. Kapitel 7. Grafische Benutzeroberflächen 1

Eventhandling. Delegation model Eventhandler model

Info B VL 11: Innere Klassen/Collections

Objektorientierte Programmierung

Sommersemester Implementierung III: GUI und Verhalten (Teil 2)

Java - Programmierung - Prozedurale Programmierung 1

Institut für Programmierung und Reaktive Systeme. Java 2. Markus Reschke

Programmiertechnik GUI Programmierung mit Java Swing

Programmieren II. Java im Vergleich zu anderen Sprachen. Einführung: GUI. Einführung: Grafische Benutzeroberflächen. Dr.

Eingabe von Texten. Vorlesung Software-Entwicklung / Folie 101

Grafische Benutzeroberflächen

Betreutes Programmieren Vorlesung Informatik II, Blatt 5

B1. Ein-/Ausgabebetonte Programmierung

Vorkurs Informatik WiSe 16/17

Java-Schulung Grundlagen

Fachhochschule Stuttgart Prof. Uwe Schulz 14. Juli 2005 Hochschule der Medien Klausur Informatik, EDV-Nr (42021) Seite 1 von 4

Institut für Programmierung und Reaktive Systeme 17. Juli Programmieren II. Übungsklausur

(C) Sortieren und Suchen mit Java.-AWT Seite 1

Ziel dieses Kapitels: Einführung in die Grundzüge der Programmierung graphischer Benutzeroberflächen (engl.: "graphical user interface", GUI) in Java

Einführung: Grafische Benutzeroberflächen. Programmieren II. Dr. Klaus Höppner. Hochschule Darmstadt Sommersemester / 22

7. Architektur interaktiver Systeme

Ein Manager mit CardLayout layout/cards.java

Instrumentierung und Dekodierung

Grafische Benutzeroberflächen

Hochschule der Medien Prof. Uwe Schulz 13. Juli 2009 Klausur Informatik, EDV-Nr Seite 1 von 6. Teil 2: Aufgaben

Informatik II. Aufgabe 1: Image Viewer ImageViewer.java

JAVA BASICS. 2. Primitive Datentypen. 1. Warum Java? a) Boolean (logische Werte wahr & falsch)

Technische Universität Braunschweig

Funktionale und Objekt-Orientierte Programmierkonzepte

Institut für Programmierung und Reaktive Systeme. GUIs mit Swing. Markus Reschke

Eventhandling. Delegation model Eventhandler model

Vorkurs Informatik WiSe 17/18

1. Konventionelle Ein-/Ausgabebetonte Programmierung

Java - Programmierung - Applets 1

Wiederholung: Objektorientierte Oberflächen-Programmierung mit Java und Swing

jcontentpane.setlayout(new BorderLayout()); // unten JPanel unten = new JPanel(); unten.setlayout( new BoxLayout(unten, BoxLayout.

Einführung in die Informatik: Programmierung und Software-Entwicklung, WS 11/12. Kapitel 7. Grafische Benutzeroberflächen

Grafische Benutzeroberflächen

Graphische Benutzungsoberflächen

Übungen zum Bioinformatik-Tutorium. Blatt 3

Einführung in die Programmierung I. 2.0 Einfache Java Programme. Thomas R. Gross. Department Informatik ETH Zürich

Institut für Programmierung und Reaktive Systeme 6. Juli Programmieren II. Übungsklausur

Tag 9: Grafische Benutzeroberflächen

Subnetting Version 0 Munz FTE1- JAVA Programm Analyse

Abteilung Informatik, JFC/Swing 2004 Diego Schmidlin V2.2

Objektorientierte Programmierung Studiengang Medieninformatik

'($)*'+ $, *$+*$'- $..$+/, 01.2$, 2*$0, - $'($)*'+ $, *$+*$

Lösungen. 1. Aufgabe. // ActionExample.java import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.event.

Zentrale Objekte zur Programmierung graphischer Benutzeroberflächen (GUI)

Kapitel 8: Grafische Benutzerschnittstellen

Algorithmen und Programmierung III

Session 1: Classes and Applets

Prüfung Softwareentwicklung II (IB)

Angewandte Mathematik und Programmierung

Javakurs für Fortgeschrittene

Teil 5 - Java. Programmstruktur Operatoren Schlüsselwörter Datentypen

Einführung in GUI-Programmierung. javax.swing

Übungen zur Vorlesung Softwaretechnologie - Wintersemester 2018/19 - Dr. Günter Kniesel. Übungsblatt 10 Lösungen

Programmieren 2 Java Überblick

Anweisungen zur Ablaufsteuerung

Prof. Dr. Uwe Schmidt. 30. Januar 2017

Einführung in GUI-Programmierung. javax.swing

Ziele. Kapitel 8: Grafische Benutzerschnittstellen. AWT und Swing. Ziele (2)

Universität Paderborn Prof. Dr. Stefan Böttcher. Kapitel 3: Java Applets

SE2-6. Übung Johannes Bürdek

Kurs OPR. Objektorientierte Programmierung

Informatik - Übungsstunde

Hochschule der Medien Prof. Uwe Schulz 14. Juli 2010 Klausur Informatik, EDV-Nr Seite 1 von 5. Teil 2: Aufgaben

Geschrieben von: Sonntag, den 18. September 2005 um 20:01 Uhr - Aktualisiert Donnerstag, den 01. Oktober 2009 um 23:39 Uhr

B1. Ein-/Ausgabebetonte Programmierung

Java Einführung Abstrakte Klassen und Interfaces

Transkript:

Programmieren 2 Übung Semesterwoche 4 1. Aufgabe: RechnerGUI In diesem Schritt geht es darum, das GUI zu implementieren. Der Rechner macht noch nichts! Ein paar Hinweise: Calculator ist Unterklasse von Frame. Der Rechner hat eine Breite von 250 und eine Höhe von 210 Pixeln. Diese Grösse darf nicht veränderbar sein. Erzeugen Sie 17 Buttons. Sie sollen wie oben dargestellt beschriftet sein. Die Buttons sind auf ein Panel mit GridLayout zu positionieren. Mit add() kann man einen Button dem Panel hinzufügen. Das Gitter füllt man damit zeilenweise auf. Für die Anzeige verwenden wir ein TextField. Mit seteditable(false) machen wir Textfeld nicht mehr editierbar; mit setbackground(color.white) setzen wir seinen Hintergrund auf weiss. Im Frame positionieren wir das Textfeld im Norden und das Panel im Süden. 2. Aufgabe: "mit Ende" Wir erweitern die Klasse Calculator so, dass man die Anwendung ordentlich beenden kann. Rechnen ist weiterhin noch nicht möglich! Ein paar Hinweise: Calculator implementiert nun das Interface WindowListener bzw. die 6 entsprechenden Methoden. Für uns ist aber einzig die Methode windowclosing() relevant. Das Frame kann nun auch als WindowListener agieren bzw. seine eigenen Events verarbeiten. Entsprechendes Registrieren ist aber vorgängig notwendig. 3. Aufgabe: jetzt mit RechenPower Die Klasse Calculator wird nun definitiv zu einem Rechner ausgebaut. Auch dazu ein paar Hinweise: Der Rechner soll seinen Zustand festhalten können. Dazu führen wir die Instanzvariablen operand1, operand2, result und operator ein, alle vom Typ int. Die Werte von operator interpretieren wir folgt: 0 "Operator unbekannt", 1 +, 2, 3 *, 4 / Calculator muss zusätzlich das Interface ActionListener implementieren. Die Methode actionperformed() realisiert schlussendlich die Rechenlogik. Sie bedingt einige Zeilen Code, die wir ebenfalls "step by step" implementieren. In einem ersten Schritt kann der Methodenrumpf ja noch leer bleiben. Das Frame muss sich bei allen Buttons als ActionListener registrieren. Mit folgender Anweisung kann man aus dem ActionEvent e die EventSource bzw. das erste Zeichen der korrespondierenden ButtonBeschriftung extrahieren: char ch = (e.getactioncommand()).charat(0); Mit Hilfe einer switch-anweisung nehmen wir uns den verschiedenen Tastendrücken an. Die 10 Ziffern behandelt man vorteilhaft am Schluss bzw. im default-teil. Überlegen Sie sich für verschiedene Eingaben, wie sich beim Eintippen der einzelnen Zeichen die Anzeige des Rechners fortlaufend verändern muss. Im nachfolgenden Beispiel wird ein eingetipptes Zeichen in " " und die resultierende Anzeige in [ ] dargestellt: "C" [0] "1" [1] "2" [12] "+" [12] "7" [7] "=" [19] "9" [9] "/" [9] "0" [0] "=" [Error] "5" [5] "1" [51] "S" [51] "*" [51] "3" [3] "=" [153] optional: Kettenrechnungen "1" [1] "+" [1] "8" [8] "8" [88] "" [89] "9" [9] "=" [80] "C" [0] Vielleicht hilft Ihnen noch folgender Tipp: char ch = '7'; int digit = ch '0'; // > digit mit intwert 7 Seite 1 von 10

Selbstverständlich testen Sie Ihren Rechner nach jedem Entwicklungsschritt wieder "live" aus! Optional: 4. Aufgabe noch ein Speicher dazu! Drei zusätzliche Buttons "sm" (store Memory), "rm" (recall Memory) und "cm" (clear Memory) ermöglichen das Handling eines einfachen Speichers. Ein Label signalisiert mit "M", dass ein Wert gespeicherter ist. Optional: 5. Aufgabe mit Kommazahlen wäre der Rechner perfekt! package calculator; /** * @author Thomas Galliker */ public class CalcApp //for testing purpose only public static void main(string[] args) new Calculator(); package calculator; import java.awt.*; import java.awt.event.*; import javax.swing.*; public class Calculator extends Frame implements WindowListener, ActionListener private final int WIDTH=250; private final int HEIGHT=210; private TextField text1; private Label lbl1; private double operand1, operand2, memory, result; private boolean isdotset = false; private boolean isnewoperand = false; private Operation operator = null; public Calculator() settitle("calculator"); setsize(width, HEIGHT); setresizable(false); BorderLayout layout1 = new BorderLayout(); setlayout(layout1); Panel p1 = new Panel(new GridLayout(5,5)); Seite 2 von 10

ExtButton b = new ExtButton("btn7", "7"); b = new ExtButton("btn8", "8"); b = new ExtButton("btn9", "9"); b = new ExtButton("btnDivide", "/"); b = new ExtButton("btnEmpty1", ""); b.setvisible(false); b = new ExtButton("btn4", "4"); b = new ExtButton("btn5", "5"); b = new ExtButton("btn6", "6"); b = new ExtButton("btnTimes", "*"); b = new ExtButton("btnEmpty2", ""); b.setvisible(false); b = new ExtButton("btn1", "1"); b = new ExtButton("btn2", "2"); b = new ExtButton("btn3", "3"); b = new ExtButton("btnMinus", "-"); Seite 3 von 10

b = new ExtButton("btnEmpty3", ""); b.setvisible(false); b = new ExtButton("btn0", "0"); b = new ExtButton("btnSign", "+/-"); b = new ExtButton("btnDot", "."); b = new ExtButton("btnPlus", "+"); b = new ExtButton("btnEqual", "="); b = new ExtButton("btnClear", "C"); b = new ExtButton("btnMemStore", "sm"); b = new ExtButton("btnMemRecall", "rm"); b = new ExtButton("btnMemClear", "cm"); text1 = new TextField(); text1.seteditable(false); text1.setbackground(color.white); lbl1 = new Label(); lbl1.setbackground(color.white); add(text1, BorderLayout.NORTH); add(lbl1, BorderLayout.CENTER); add(p1, BorderLayout.SOUTH); setvisible(true); addwindowlistener(this); Seite 4 von 10

clear(); public void windowclosing(windowevent e) System.exit(0); public void windowiconified(windowevent e) public void windowopened(windowevent e) public void windowclosed(windowevent e) public void windowdeiconified(windowevent e) public void windowactivated(windowevent e) public void windowdeactivated(windowevent e) public void actionperformed(actionevent e) ExtButton button = (ExtButton)e.getSource(); String name = button.getname(); // Debug Hash Codes of Buttons: // System.out.println(name.hashCode()); switch(name.hashcode()) //btn0 case 3034452: setoperand(0); //btn1 case 3034453: setoperand(1); //btn2 case 3034454: setoperand(2); //btn3 case 3034455: setoperand(3); //btn4 case 3034456: setoperand(4); //btn5 case 3034457: setoperand(5); //btn6 case 3034458: setoperand(6); //btn7 case 3034459: setoperand(7); //btn8 case 3034460: setoperand(8); Seite 5 von 10

//btn9 case 3034461: setoperand(9); //btndivide case 154186229: setoperation(operation.divide); //btntimes case 2097951306: setoperation(operation.times); //btnminus case 2091488116: setoperation(operation.minus); //btnplus case 206107158: setoperation(operation.plus); //btnequal case 2084344376: getresult(); //btnsign case 206193209: changesign(); //btndot case -1378836147: setdot(); //btnclear case 2082333009: clear(); //btnmemstore case 1116964488: storememory(); //btnmemrecall case 223305290: recallmemory(); //btnmemclear case 1101939700: clearmemory(); default: private void setoperand(int value) //flush field "text1" if "isnewoperand=true" flag is set if(isnewoperand) Seite 6 von 10

text1.settext(""); isnewoperand = false; if(operator == null) String s = text1.gettext() + String.valueOf(value); if(!isdotset) Integer i = new Integer(s); text1.settext(i.tostring()); text1.settext(s); operand1 = Double.valueOf(text1.getText()); String s = text1.gettext() + String.valueOf(value); if(!isdotset) Integer i = new Integer(s); text1.settext(i.tostring()); text1.settext(s); operand2 = Double.valueOf(text1.getText()); private void setoperation(operation o) //perform calculation before the new operator is set calculate(); //set new operator operator = o; //flag "isnewoperand=true" declares that the current screen output //has to be cleared as soon as a new input was set isnewoperand = true; public enum Operation PLUS, MINUS, TIMES, DIVIDE; double eval(double x, double y) switch(this) case PLUS: return x + y; case MINUS: return x - y; case TIMES: return x * y; case DIVIDE: return x / y; throw new AssertionError("Unknown operator: " + this); Seite 7 von 10

private double calculate() //Calculate operand1 and operand2 with selected operator Operation o = operator; if(o!= null) //division by zero is not allowed if(operand2 == 0 && o == Operation.DIVIDE) JOptionPane.showMessageDialog(null, "Division by Zero Exception!","Exception", JOptionPane.ERROR_MESSAGE); clear(); //eval method performs the defined calculation result = o.eval(operand1,operand2); //exchange operand1 with result to allow linked calculations operand1 = result; operand2 = 0; result = operand1; //flush current operator sign operator = null; //flush decimal dot isdotset = false; //flag "isnewoperand=true" declares that the current screen output //has to be cleared as soon as a new input was set isnewoperand = true; return result; private void getresult() result = calculate(); text1.settext(double.tostring(result)); private void clear() operand1 = 0; operand2 = 0; operator = null; result = 0; setoperand(0); private void changesign() if(operator == null) //if no operator is set we negate operand1 operand1 = operand1 * (-1); text1.settext(double.tostring(operand1)); //if an operator is set, we negate the result result = result * (-1); Seite 8 von 10

text1.settext(double.tostring(result)); private void storememory() memory = operand1; refreshmemorycontent(true); private double recallmemory() text1.settext(double.tostring(memory)); if(operator == null) operand1 = memory; operand2 = memory; return memory; private void clearmemory() memory = 0; lbl1.settext(""); private void refreshmemorycontent(boolean showmemorycontent) if(showmemorycontent) lbl1.settext("m("+double.tostring(memory)+")"); lbl1.settext("m"); private void setdot() if(!isdotset) text1.settext(text1.gettext() + "."); isdotset = true; Seite 9 von 10

package calculator; import java.awt.*; public class ExtButton extends Button private String name = ""; public ExtButton(String label) this.setname(label); this.setlabel(label); public ExtButton(String name, String label) this.setname(name); this.setlabel(label); public void setname(string name) this.name = name; public String getname() return name; Weitere Informationen: http://de.wikibooks.org/wiki/java_standard:_grafische_oberfl%c3%a4chen_mit_awt http://www.saar.de/~awa/jgui.htm http://java.sun.com/j2se/1.5.0/docs/guide/language/enums.html http://www.cs.ubc.ca/local/computing/software/j2sdk-1.4.2/docs/api/javax/swing/joptionpane.html http://www.myfaq.com.cn/api/jdk142/java/awt/label.html http://www.daniweb.com/forums/thread16127.html http://java.sun.com/j2se/1.4.2/docs/api/java/text/decimalformat.html Seite 10 von 10