1 / 27

SENG 531: Labs

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. How the Labs Work. Labs will cover Course material in depth as needed Assignments, Presentations, Final Review

myra
Download Presentation

SENG 531: Labs

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

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/ • 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 • 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. <<interface>> IRockBand AbstractGrungeBand Nirvana FooFighters Polymorphism: So What?

  10. <<interface>> IRockBand playMusic()::void Polymorphism: Aggregation 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. AbstractGrungeBand playLoudMusic()::void Nirvana singerOverdose()::void FooFighters singerOverdose()::void pumpOutAlbum()::void

  11. <<interface>> IRockBand playMusic()::void Polymorphism: Sub-Typing Look at this example: publicclass RockConcert { publicvoid playAGig( AbstractGrungeBand band ) { band.playMusic(); band.playLoudMusic(); } } AbstractGrungeBand playLoudMusic()::void Nirvana 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? singerOverdose()::void FooFighters singerOverdose()::void pumpOutAlbum()::void

  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. Polymorphic Overloading • Are there situations where we want • function overloading, AND • calling a method using an object’s real sub-type, not its “current” sub-type?

  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 This is A 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 This is A 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. Single Dispatch: Fix The crummy way to do this: 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. Single Dispatch: Fix The elegant way to do this: The Visitor Design Pattern

  24. Extra slides . . .

  25. Polymorphic Overloading What’s the output from this code? 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" ); } } } Caveat: this isn’t really polymorphic overloading, but it’s a related example that gets the gist

  26. Polymorphic Overloading What’s the output from this code? 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" ); } } }

  27. Polymorphic Overloading This is how a SANE person would have written that example: 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" ); } } }

More Related