Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lec 06 Agenda 1/ the 6 rules of Java

Similar presentations


Presentation on theme: "Lec 06 Agenda 1/ the 6 rules of Java"— Presentation transcript:

1 Lec 06 Agenda 1/ the 6 rules of Java
2/ Casting Objects, casting primitives. 3/ OOAD and CRC –aggregation versus composition 3/ Swing and Threading 4/ Java Event Model. Event api and support for Swing 5/ Swing labs, live templates, form designer

2 Fight Club Rules of Java
1/ Only concrete classes can be instantiated.

3 Fight Club Rules of Java
1/ Only concrete classes can be instantiated. 2/ Only concrete classes can be instantiated.

4 Fight Club Rules of Java
1/ Only concrete classes can be instantiated. 2/ Only concrete classes can be instantiated. 3/ The type of any Object must be of type Concrete_class. (corollary to 1 and 2) 4/ References may be concrete, abstract, or interface.

5 Fight Club Rules of Java
5/ You may create type-anonymous abstract- classes and interfaces by overriding ALL their contract methods when you declare them. 6/ The “type” of a type-anonymous class is $x aka Robert Paulson. See casting package in lec06

6 Casting Casting down (opening the aperture) is potentially very dangerous and could produce a class-cast-exception. You must be a good free-mason. Casting up (closing the aperture) is called automatic promotion and an explicit cast is not required because a subclass object may always be stored in a superclass reference (or an interface that it implements).

7

8 The Debugger See debugger package in lec06

9 Assertions See exceptions/AssertionDriver

10 OOAD Object-oriented analysis and design (OOAD) is a popular technical approach for analyzing, designing an application, system, or business by applying the object-oriented paradigm and visual modeling throughout the development life cycles to foster better stakeholder communication and product quality. (from wiki) Solve a simple word problem. Red are verbs and blue are nouns. Model the system! Nouns are either objects or fields (properties of objects). Verbs are methods. 10

11 using CRC: Classes, Responsibilities, Collaborators
OOAD using CRC: Classes, Responsibilities, Collaborators Solve a simple word problem. Red are verbs and blue are nouns. Model the system! Nouns are either objects or fields (properties of objects). Verbs are methods. 11

12 OOAD Solve a simple word problem. Red are verbs and blue are nouns. Model the system! Nouns are either objects or fields (properties of objects). Verbs are methods. 12

13 Agile

14 "If I can't picture it, I can't understand it."
14

15 Write a very application for a vending machine
Write a very application for a vending machine. You may insert coins into the vending machine and the vending machine will vend dry products such as candy bars. The user is allowed to either insert coins or select a product. If there is insufficient coins, the vending machine will indicate this. Solve a simple word problem. Red are verbs and blue are nouns. Model the system! Nouns are either objects or fields (properties of objects). Verbs are methods. 15

16 UML: Unified Modeling Language
CRC: Class, Responsibilities, Collaborators Build Cmd and Gui Vending

17 Aggregation versus Composition
Aggregation is used to represent ownership or a whole/part relationship. Composition is used to represent an even stronger form of ownership. With composition, we get coincident lifetime of part with the whole.

18 Aggregation versus Composition
Aggregation: A student has a laptop. Composition: A student has a brain. (coincident lifecycle)

19 Java entity May be a reference? May be an instantiated? Concrete Class YES Abstract Class NO Interface

20 Java Event Model Used to program behavior in GUIs
Used extensively in Android.

21 Yes, sound is a natural phenomena. But in Java, the answer is no.
If a tree falls in a forest and no one is around to hear it, does it make a sound? 21

22 Step 1a/ Create a concrete Listener class that implements the Particular listener interface.
Step 1b/ or Let the enclosing Activity implement Listener interface and override the methods from the Interface Step 2/ register the event-source with the event-listener Step 3/ Define the behavior 22

23 No Event Listener Event-Source (Button) No Event-Listener listening
(onClick) No Catcher The event often encapsulates data about that event, the coordinates where you clicked, the time you clicked, the user_name, etc. If no one is around to listen, it's just ignored. The event source could be any number of GUI elements. No Event Listener 23

24 Event-Listener listening ActionListener Event-Source (Button)
(onClick) Any Object Catcher ready to catch The catcher is registered to listen to events from all the players on his team. If a player throws his bat into the dugout, the catcher will not try to catch it. If someone in the stands throws a soccer ball into the field, the catcher will not catch it. Conditions the catcher will catch the event: 1/ The event source (thrower) is registered by the catcher 2/ it's the correct event (ball) The event encapsulates data about that event, the coordinates where you clicked, the time you clicked, the user_name, etc. If no one is around to listen, it's just ignored. In Android, the events contain data, but since it's a resource-constrained environment, not as much as Java. In most cases, it's a good idea to leave the controller methods inside the enclosing activity as anonymous inner-classes or just implment approriate methods and call "this". The catcher can catch the event and take appropriate action (if runner stealing second, then throw ball to second-base) Method called catchBall(Player ply){ if (ply == pitcher){ catch the ball and throw it back if (ply == third-baseman) tag the runner stealing home } 24

25 Wrong event. The catcher is a baseball-throw-event-listener
Wrong event. The catcher is a baseball-throw-event-listener. If you throw a football at the baseball catcher, it's going to sail over his head. Wrong Event 25

26 Event source not registered

27 Event-Listener listening Event-Source (Button)
Action-Listener OnMouse-Listener Event-Listener listening Event-Source (Button) Event (Action) Any Object Event (Mouse) Catcher ready to catch The catcher is registered to listen to events from all the players on his team. If a player throws his bat into the dugout, the catcher will not try to catch it. If someone in the stands throws a soccer ball into the field, the catcher will not catch it. Conditions the catcher will catch the event: 1/ The event source (thrower) is registered by the catcher 2/ it's the correct event (ball) The event encapsulates data about that event, the coordinates where you clicked, the time you clicked, the user_name, etc. If no one is around to listen, it's just ignored. In Android, the events contain data, but since it's a resource-constrained environment, not as much as Java. In most cases, it's a good idea to leave the controller methods inside the enclosing activity as anonymous inner-classes or just implment approriate methods and call "this". The catcher can catch the event and take appropriate action (if runner stealing second, then throw ball to second-base) Method called catchBall(Player ply){ if (ply == pitcher){ catch the ball and throw it back if (ply == third-baseman) tag the runner stealing home } 27

28 Inner and Anonymous Classes
In Java7, functions are second class citizens. You can only pass objects around. Though you are captive in the OO paradigm, you can use inner classes and anonymous classes to get around this constraint and write lambda-like code. Lamdas in Java8 replace anonymous inner classes. Often times, no one but the enclosing class cares about an object. In this case, you may consider using inner or anonymous classes.

29

30 Write a very simple application for a contact manager
Write a very simple application for a contact manager. The the sake of simplicity, each contact will have a name and a phone number only. The user should be able to create new contacts and display all contacts. Create a Latin dictionary with entries for Latin and English equivalents. Store them in a list and allow the user to delete them. Solve a simple word problem. Red are verbs and blue are nouns. Model the system! Nouns are either objects or fields (properties of objects). Verbs are methods. 30

31 Interfaces A class implements an interface rather than extends it. Any class that implements the interface must override all the interface methods with it's own methods. Interface names often end with "able" to imply that they add to the capabilty of the class. An interface is a contract; it defines the methods

32 The Painter GUI App Lec05/moreswing/Painter

33 The ColorSelector GUI App

34 The Leet Translator GUI App

35 JavaFX From Firefox samples/Ensemble/index.html#SAMPLES

36 Reference anonymous - gravity
Concrete classes: rules of fight club Type anonymous : Fight Club - Robert Paulson Dubugger: x-men quicksilver

37 References Objects are like astronauts.

38 References Object objDate = new Date();
This date has a reference. The reference is an Object.

39 Reference Anonymous MyTime myTime = new MyTime(new Date());
The date is reference-anonymous, but we can still get a reference to it myTime.getDate(); new Date(); The date is reference-anonymous and we have no reference to it – it is space-junk and will be garbage collected.

40 Reference Anonymous Date dat1 = new Date(); Date dat2 = new Date();
dat1 = dat2; The Object originally stored in dat1 is orphaned and becomes space junk.

41 Reflection Very useful to inspect the underlying object type.
Very useful when first learning an OO language.

42 Reflection Reflection allows you to inspect the type (class) of the implicit parameter at runtime. We will use reflection to gain a deeper understanding of polymorphism and the java event model. Every class has a class object which you can access like so: java.util.Date.class, or like so: Class.forName(strFullyQualifiedClass); See reflection example

43 How to use the Form Designer in IntelliJ
best video on Form Designer in Intellij (in German, genießen) (ok video, no sound, English subtitles)

44 Reflection Name of Driver implemnts Implicit param EventListener type
Defined TimeTestOuter ActionListener yes EventListenerOuter In separate java file TimeTestInner EventListenerInner In same java file TimeTestLocal anonymous Same method TimeTestAnon no inline See inner example

45 Layouts in Swing Layouts:
/layout/border.html ng/layout/flow.html make-bottom-panel-in-borderlayout-to- expand-like-center-panel

46 Swing – and Threading

47 Step 1/ define the event-listener object and override the appropriate methods
ActionListener mActionListener = new ActionListener() { @Override public void actionPerformed(ActionEvent e) { //behavior here }}; Step 2/ register (add) the event-listener to the event source. mButton.addActionListener(mActionListener);


Download ppt "Lec 06 Agenda 1/ the 6 rules of Java"

Similar presentations


Ads by Google