1 / 14

CIS110-203 Intro to Computer Science Lab #11

CIS110-203 Intro to Computer Science Lab #11 TA: Catherine Stocker Mentor: Jay Fiddelman University of Pennsylvania 15 November 2007 Today’s Agenda Review Review Question Lab Work Space Invaders Monitors… Turn ‘em off General Stuff How is the homework going?

Download Presentation

CIS110-203 Intro to Computer Science Lab #11

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. CIS110-203Intro to Computer Science Lab #11 TA: Catherine Stocker Mentor: Jay Fiddelman University of Pennsylvania 15 November 2007

  2. Today’s Agenda • Review • Review Question • Lab Work • Space Invaders

  3. Monitors… • Turn ‘em off

  4. General Stuff • How is the homework going? • Exam next Tuesday. Review sessions this weekend. • Projects start when we get back from Thanksgiving break.

  5. What’s the point of interfaces if they’re just blank? When should we use them and why are they helpful? • Point of extending classes: • Polymorphic data structures (arrays, lists, etc) and methods • Code Reuse • Point of implementing interfaces: • Polymorphic data structures (arrays, lists, etc) and methods • Can implement multiple (because no code reuse) • Polymorphic data structures and methods • Implementing an interface is like making a promise • Ex. Anything implementing the List interface promises to be able to add, remove, get, etc • Anything that makes that promise can be used interchangably (sort of)

  6. What’s the point of interfaces… • Back to the Governator example: • Say you have a Governator class. • We all know a Governator is both a Governor and a Terminator. • Say the Governor and Terminator CLASSES each have a resolveConflict() method • Which resolveConflict method is used when the Governator class extends both classes? Answer: This is why you’re not allowed to extend multiple classes.

  7. What’s the point of interfaces … • BUT THAT’S NOT FAIR! 50% of the reason we extend classes is to allow for polymorphic code! • What if we want to make two separate arrays, one that holds Governors and one that holds Terminators? Which one does he belong in? • What if we want to pass a Governer to a makeLaws() method and a Terminator to a killPeople() method? How can he do both? • Never fear…interfaces are here!

  8. What’s the point of interfaces … • We’re allowed to implement MULTIPLE interfaces!!! • Since the methods of an interface have no body (these type of methods are called abstract methods) then if we implement a Governor and Terminator INTERFACE we don’t run into the problem of which resolveConflict() to use. We just implement our own Governator specific resolveConflict(). • BUT we have still made the Promise that we will implement all of the methods declared in each interface. So if we want to pass it to method that requires a Governor as an argument, that’s ok because we know it will have all of the functionality that a Governor can have. Same with data structures – if we want to store it in a Governor array and a Terminator array, it has promised to be able to act like both!

  9. What’s the point of interfaces … • One more thing…sometimes we just don’t care to implement the interface method. • If you have an Animal class, how do you implement a makeNoise() method? “bark”? “meow”? “neigh”? • What about a Horse class? makeNoise() should “neigh”. • So Animal needs only be an interface that promises that all Animals will do something – makeNoise(), sleep(), eat() • Then…you can have a method: • public void scareAnimals(Animal[] a) { for(int i=0 ; i<a.length ; i++) a[i].makeNoise(); } Which may look something like: “bark” “bark” “neigh”

  10. How do we code interfaces? • My Animal, Dog and Horse code: • public interface Animal { public void makeNoise(); } • public class Dog implements Animal { public Dog() {} public void makeNoise() {System.out.println("bark");} } • public class Horse implements Animal { public Horse() {} public void makeNoise() {System.out.println(“neigh");} }

  11. Put all of that together…(and review arrays of objects) • public class AnimalExample { public static void main(String[] args) { Animal[] a = new Animal[3]; a[0] = new Dog(); //upcasting a[1] = new Dog(); a[2] = new Horse(); scareAnimals(a); } public static void scareAnimals(Animal[] a) { for(int i=0 ; i<a.length ; i++) { a[i].makeNoise(); } } }

  12. upcasting v. downcasting • Given a class Person (also applies to interfaces, but since you cannot instantiate an interface, I’ll use classes for simplicity) and the class that inherits from it Student: • Upcasting (GOOD) • Casting to the class/interface “above” (parent). Going from more specific (Student) to more general (Person) • Person[] p = new Person[3];p[0] = new Student(); (automatically happens here) • Person p; Student s; p = (Person) s; • Downcasting (BE CAREFUL) • Casting to the class “below” (child). Going from general to specific….only if it was specific first. • Person p; Student s1,s2; p=(Student) s1; s2=(Student)p; • Person p; Student s1; s1=p; (NOT OK)

  13. Lab Work • Work alone or together • Linked listshttp://www.seas.upenn.edu/~cis1xx/projects/LinkedList_NonGeneric/ • Review Problem • Space Invaders (only if you’ve finished the other two)

  14. Later… • Finish up Space Invaders • Review Sheet by Sunday • Good luck on the exam!

More Related