hallo liebe java programmierer, ich habe euren thread gelesen das ihr keine hausaufgaben für andere erledigt was auch gut so ist, aber ihr helft dabei.warum ich mich an euch wende ist um in erster linie fachkenntnis zu erlangen und andere anszusprechen die die gleiche problematik haben(uni,etc).daher ist das denke ich mal die geeignetste plattform.
meine problematik ist einen java rechner zu schreiben nach MVC muster, vorgegeben sind eine Control klasse und 2 schnittstellen. anforderung ist 2 klassen zu schreiben(Model und View)die die beiden schnittstellen implementieren. control steuert model und view.model und view wissen nichts voneinander.nur verbunden durch die schnittstellen.
Jetzt erzähle ich euch was ich recherchiert habe:über google mehre mvc taschenrechner bsp angeguckt und versucht nachzubauen. problem wie kann ich das auf meine aufgabe realisieren. ich stelle mal den die Control klasse und die schnittstellen rein.
[Java]public class RechnerControl {
3 private RechnerModel_I aModel;
4 private RechnerView_I aView;
5 // ---------------------------------------------------------------------
6 public RechnerControl(RechnerModel_I aModel, RechnerView_I aView) {
7 this.aModel = aModel;
8 this.aView = aView;
9 // 1 listener for the submitButton:
10 ActionListener mal = new MyActionListener();
11
12 // 2 listeners for the input text fields
13 KeyListener mkl1 = new MyKeyListener();
14 KeyListener mkl2 = new MyKeyListener();
15
16 // Register the 3 listeners with the view:
17 aView.register(mal, mkl1, mkl2);
18 } // Konstruktor
19 // ---------------------------------------------------------------------
20 // The following listener classes for GUI elements are declared here
21 // (and not in the view), because they call methods which are visible
22 // here, but not in the view (calculate and isValidEntry)
23 //
24 // ActionListener for the submitButton:
25 private class MyActionListener implements ActionListener {
26 public void actionPerformed(ActionEvent e) {
27 String s1 = aView.getNumber1();
28 String s2 = aView.getNumber2();
29 String[] res = aModel.calculate(s1, s2);
30 aView.setResults(res);
31 }
32 }
33
34 // KeyListener for the input text fields:
35 private class MyKeyListener extends KeyAdapter {
36 String oldContent = "";
37
38 public void keyReleased(KeyEvent ke) {
39 JTextField tf = (JTextField) ke.getSource();
40 String newContent = tf.getText();
41 int newCaretPos = tf.getCaretPosition();
42 boolean isValid = aModel.isValidEntry(newContent);
43
44 if (isValid) {
45 oldContent = newContent;
46 } else {
47 // If the newConten is not valid, this is probably due to
48 // the character typed last and the following holds:
49 // 1. The caret (cursor) is positioned immediately to
50 // the right of the faulty character.
51 // 2. This character is not present in the oldContent.
52 // 3. The caret position in the oldContent was probably
53 // one less than it is in the newContent.
54 // If the user used "copy and paste" (instead of typing)
55 // things might be different.// Restore the oldContent and the old caret position:
58 tf.setText (oldContent);
59 tf.setCaretPosition(Math.max(0, newCaretPos-1));
60 }
61 } // keyReleased
62 } // class MyKeyAdapter
63 // ---------------------------------------------------------------------
64 // A method with a short name:
65 static void printf(String f, Object... v) {System.out.printf(f, v);}
66 // ---------------------------------------------------------------------
67 } // class RechnerControl [/Java]
Die beiden schnittstellen.
wie ich die buntzeroberfläche der view klasse baue kein problem,was ich noch nicht gecheckt habe wenn ich zum beispiel class View implements rechnerView_I mus ich dann in der klasse exakt die methoden bauen die in der schnittstelle schon vorhanden sind und dies per event an die model klasse senden, nach dem observer prinzip. ich werde nach und nach meine ergebnisse vorzeigen grüßle
meine problematik ist einen java rechner zu schreiben nach MVC muster, vorgegeben sind eine Control klasse und 2 schnittstellen. anforderung ist 2 klassen zu schreiben(Model und View)die die beiden schnittstellen implementieren. control steuert model und view.model und view wissen nichts voneinander.nur verbunden durch die schnittstellen.
Jetzt erzähle ich euch was ich recherchiert habe:über google mehre mvc taschenrechner bsp angeguckt und versucht nachzubauen. problem wie kann ich das auf meine aufgabe realisieren. ich stelle mal den die Control klasse und die schnittstellen rein.
[Java]public class RechnerControl {
3 private RechnerModel_I aModel;
4 private RechnerView_I aView;
5 // ---------------------------------------------------------------------
6 public RechnerControl(RechnerModel_I aModel, RechnerView_I aView) {
7 this.aModel = aModel;
8 this.aView = aView;
9 // 1 listener for the submitButton:
10 ActionListener mal = new MyActionListener();
11
12 // 2 listeners for the input text fields
13 KeyListener mkl1 = new MyKeyListener();
14 KeyListener mkl2 = new MyKeyListener();
15
16 // Register the 3 listeners with the view:
17 aView.register(mal, mkl1, mkl2);
18 } // Konstruktor
19 // ---------------------------------------------------------------------
20 // The following listener classes for GUI elements are declared here
21 // (and not in the view), because they call methods which are visible
22 // here, but not in the view (calculate and isValidEntry)
23 //
24 // ActionListener for the submitButton:
25 private class MyActionListener implements ActionListener {
26 public void actionPerformed(ActionEvent e) {
27 String s1 = aView.getNumber1();
28 String s2 = aView.getNumber2();
29 String[] res = aModel.calculate(s1, s2);
30 aView.setResults(res);
31 }
32 }
33
34 // KeyListener for the input text fields:
35 private class MyKeyListener extends KeyAdapter {
36 String oldContent = "";
37
38 public void keyReleased(KeyEvent ke) {
39 JTextField tf = (JTextField) ke.getSource();
40 String newContent = tf.getText();
41 int newCaretPos = tf.getCaretPosition();
42 boolean isValid = aModel.isValidEntry(newContent);
43
44 if (isValid) {
45 oldContent = newContent;
46 } else {
47 // If the newConten is not valid, this is probably due to
48 // the character typed last and the following holds:
49 // 1. The caret (cursor) is positioned immediately to
50 // the right of the faulty character.
51 // 2. This character is not present in the oldContent.
52 // 3. The caret position in the oldContent was probably
53 // one less than it is in the newContent.
54 // If the user used "copy and paste" (instead of typing)
55 // things might be different.// Restore the oldContent and the old caret position:
58 tf.setText (oldContent);
59 tf.setCaretPosition(Math.max(0, newCaretPos-1));
60 }
61 } // keyReleased
62 } // class MyKeyAdapter
63 // ---------------------------------------------------------------------
64 // A method with a short name:
65 static void printf(String f, Object... v) {System.out.printf(f, v);}
66 // ---------------------------------------------------------------------
67 } // class RechnerControl [/Java]
Die beiden schnittstellen.
Java:
public interface RechnerModel_I {
3 String getTypeName();
4 // Each model (class) implementing this interface should
5 // "work and calculate" with numbers of a certain type,
6 // e.g. of type int or of type float or ... .
7 // This method returns the name of this type as a String
8 // e.g. "int" or "float" or ... .
9
10 boolean isValidEntry(String pre);
11 // Returns true, iff there is a String suf, such that
12 // the String pre+suf can be interpreted as a number
13 // of the type expected by the implementing model.
14
15 String[] calculate(String number1, String number2);
16 // Trys to interpret number1 and number2 as numbers z1 and z2
17 // (e.g. of type int or float or ... ), calculates the 5 values
18 // z1+z2, z1-z2, z1*z2, z1/z2, z1%z2 and returns them as an
19 // array of 5 String-objects. In case of problems the String-
20 // objects may contain some error messages.
21 } // inteface RechnerModel_I
Java:
public interface RechnerView_I {
3 // Each implementing class is expected to declare at least
4 // - 1 submit button (e.g. of type JButton)
5 // - 2 input text fields (e.g. of type JTextField)
6 // - 5 result text fields (e.g. of type JTextField)
7
8 void register(ActionListener al, KeyListener kl1, KeyListener kl2) {
9 // Registers (using the appropriate addXXXListener method)
10 // al with the submit button,
11 // kl1 with with the first input text field and
12 // kl2 with with the second input text field.
13
14 String getNumber1();
15 // Returns the content of the first input text field.
16 String getNumber2();
17 // Returns the content of the second input text field,
18 void setResults(String[] results);
19 // Expects the array results to be of lenght 5.
20 // Assigns each of its elements to one of the result text fields.
21 } // inteface RechnerView_I