Presentation is loading. Please wait.

Presentation is loading. Please wait.

SENG 531: Labs TA: Brad Cossette Office Hours: Monday, Wednesday.

Similar presentations


Presentation on theme: "SENG 531: Labs TA: Brad Cossette Office Hours: Monday, Wednesday."— Presentation transcript:

1 SENG 531: Labs TA: Brad Cossette brad.cossette@gmail.com cossette@cpsc.ucalgary.ca http://pages.cpsc.ucalgary.ca/~cossette/ Office Hours: Monday, Wednesday 3-4pm ICT 524

2 How the Labs Work Labs will cover  Course material in depth as needed  Assignments, Presentations, Final Review  General help with course work Labs are optional, but encouraged  Upcoming lab schedule posted on TA site

3 Labs This Week: Monday  Assignments Overview  Polymorphism in OO  Polymorphic Overloading Wednesday  AST’s  Single/Double Dispatch  The Visitor Pattern

4 Assignments: Stuff to Remember Groups allowed/encouraged Everyone must submit individual write-ups  The write-ups are worth more then actual code, so don’t blow them off! Short, Concise, & Quality= Happy T.A. Long, Wordy, & full of B.S.= Grumpy T.A.

5 Assignments: Assignment 1 Link: http://pages.cpsc.ucalgary.ca/~rwalker/teaching/seng531/W07/http://pages.cpsc.ucalgary.ca/~rwalker/teaching/seng531/W07/ Key Points:  Determine all method invocations in the code AND  Determine what concrete methods are potentially invoked there Rob has suggested a starting form of your solution  Not required, but unless you have a better idea... Write-up – look at the questions posted on the assignment page

6 Assignments: Assignment 1 Decaff  no member, local, or anonymous types local = int x1, x2;  no arrays  no for statements  no exceptions, and so, no throw, throws, try/catch, etc.  no call chains allowed, e.g., a.b().c() (which means that a.b() would have to be assigned to a local variable)  no explicit constructor calls allowed, i.e., no this() or super()  no class selector Foo.class  no generics  no "on-demand" import statements import java.io.*

7 Assignments: Assignment 1 Coding style  Should mostly follow the Java Coding Standard  http://java.sun.com/docs/codeconv/html/CodeConvTOC.doc.html http://java.sun.com/docs/codeconv/html/CodeConvTOC.doc.html  I also expect JavaDoc formatted comments Some recommended exceptions (IMHO):  Use all lowercase for variable names, _ to separate words  Use iii, jjj for loop iterators, not i,j.  Use super when invoking any parent methods/variables  Always use generics when possible  Never use reflection unless absolutely necessary

8 Polymorphism and OO Polymorphism:  Definition In more precise terms, polymorphism (object-oriented programming theory) is the ability of objects belonging to different types to respond to method calls of methods of the same name, each one according to an appropriate type-specific behaviour. The programmer (and the program) does not have to know the exact type of the object in advance, so this behavior can be implemented at run time (this is called late binding or dynamic binding).  Simpler Version Lets you treat derived class members just like their parent class's members. Source: http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming

9 Polymorphism: So What? AbstractGrungeBandFooFightersNirvana > IRockBand

10 Polymorphism: Aggregation AbstractGrungeBand playLoudMusic()::void FooFighters singerOverdose()::void pumpOutAlbum()::void Nirvana singerOverdose()::void > IRockBand playMusic()::void This is how you’re probably used to thinking about Polymorphism in an OO context. The FooFighters class has all the functionality of the Nirvana class, perhaps even does a few things differently, and provides some new stuff.

11 Polymorphism: Sub-Typing AbstractGrungeBand playLoudMusic()::void FooFighters singerOverdose()::void pumpOutAlbum()::void Nirvana singerOverdose()::void > IRockBand playMusic()::void Look at this example: public class RockConcert { public void playAGig( AbstractGrungeBand band ) { band.playMusic(); band.playLoudMusic(); } We can pass either Nirvana or FooFighters objects as arguments to playMusic(). How hard would it to be to add new bands, or new rock music genres to the system?

12 Polymorphic Overloading Polymorphism  An object can be treated as having the type of any of its parents Overloading  More then one method with the same name, but accepting different types of arguments, exists

13 Are there situations where we want  function overloading, AND  calling a method using an object’s real sub- type, not its “current” sub-type? Polymorphic Overloading

14 Single Dispatch: Example Suppose we have the following classes: public class A { public String text = "This is A"; } public class A1 extends A { public String text_1 = "This is really A1"; } public class A2 extends A { public String text_2 = "This is really A2"; } Source:http://pages.cpsc.ucalgary.ca/~rwalker/teaching/seng531/W07/priv/OO%20Review.pdf

15 Single Dispatch: Example And this is our Main class: public class Main { public void doIt( A object ) { System.out.println( object.text ); } public void doIt( A1 object ) { System.out.println( object.text_1 ); } public void doIt( A2 object ) { System.out.println( object.text_2 ); } public void foo( A object ) { doIt( object ); }

16 Single Dispatch: Example What happens when we execute: public static void main( String args[] ) { Main test = new Main(); test.doIt( new A() ); test.doIt( new A1() ); test.doIt( new A2() ); }

17 Single Dispatch: Example What happens when we execute: public static void main( String args[] ) { Main test = new Main(); test.doIt( new A() ); test.doIt( new A1() ); test.doIt( new A2() ); } This is A This is really A1 This is really A2

18 Single Dispatch: Example What happens when we execute: public static void main( String args[] ) { Main test = new Main(); test.foo( new A() ); test.foo( new A1() ); test.foo( new A2() ); }

19 Single Dispatch: Example What happens when we execute: public static void main( String args[] ) { Main test = new Main(); test.foo( new A() ); test.foo( new A1() ); test.foo( new A2() ); } This is A

20 Single Dispatch: Example What happens when we execute: public static void main( String args[] ) { Main test = new Main(); test.doIt( new A() ); test.doIt( ( A )( new A1() ) ); test.doIt( ( A )( new A2() ) ); } This is A

21 Single Dispatch: Example What happens when we execute: public static void main( String args[] ) { Main test = new Main(); test.doIt( new A() ); test.doIt( ( A )( new A1() ) ); test.doIt( ( A )( new A2() ) ); }

22 The crummy way to do this: Single Dispatch: Fix public class CrummyMain { public void foo( A object ) { if ( object instanceof A1 ) doIt( ( A1 )object ); else if ( object instanceof A2 ) doIt( ( A2 )object ); else doIt( object ); }

23 The elegant way to do this: Single Dispatch: Fix The Visitor Design Pattern

24 Extra slides...

25 public class PolymorphicOverloading { public static void main( String args[] ) { try { throw new FileNotFoundException( "Blah!" );} catch( Exception e ) { System.out.println( "We caught an exception" ); } catch( FileNotFoundException fnfe ) { System.out.println( "We caught a File Not Found Exception" ); } Polymorphic Overloading What’s the output from this code? Caveat: this isn’t really polymorphic overloading, but it’s a related example that gets the gist

26 public class PolymorphicOverloading { public static void main( String args[] ) { try { throw new FileNotFoundException( "Blah!" );} catch( Exception e ) { System.out.println( "We caught an exception" ); } catch( FileNotFoundException fnfe ) { System.out.println( "We caught a File Not Found Exception" ); } Polymorphic Overloading What’s the output from this code?

27 public class PolymorphicOverloading2 { public static void main( String args[] ) { try { throw new FileNotFoundException( "Blah!" ); } catch( FileNotFoundException fnfe ) { System.out.println( "We caught a File Not Found Exception" ); } catch( Exception e ) { System.out.println( "We caught an exception" ); } Polymorphic Overloading This is how a SANE person would have written that example:


Download ppt "SENG 531: Labs TA: Brad Cossette Office Hours: Monday, Wednesday."

Similar presentations


Ads by Google