cis110 203 intro to computer science lab 11 l.
Download
Skip this Video
Loading SlideShow in 5 Seconds..
CIS110-203 Intro to Computer Science Lab #11 PowerPoint Presentation
Download Presentation
CIS110-203 Intro to Computer Science Lab #11

Loading in 2 Seconds...

play fullscreen
1 / 14

CIS110-203 Intro to Computer Science Lab #11 - PowerPoint PPT Presentation


  • 445 Views
  • Uploaded on

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?

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about 'CIS110-203 Intro to Computer Science Lab #11' - JasminFlorian


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.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
cis110 203 intro to computer science lab 11

CIS110-203Intro to Computer Science Lab #11

TA: Catherine Stocker

Mentor: Jay Fiddelman

University of Pennsylvania

15 November 2007

today s agenda
Today’s Agenda
  • Review
  • Review Question
  • Lab Work
  • Space Invaders
monitors
Monitors…
  • Turn ‘em off
general stuff
General Stuff
  • How is the homework going?
  • Exam next Tuesday. Review sessions this weekend.
  • Projects start when we get back from Thanksgiving break.
slide5
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)
what s the point of interfaces
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.
what s the point of interfaces7
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!
what s the point of interfaces8
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!
what s the point of interfaces9
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”

how do we code interfaces
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");}

}

put all of that together and review arrays of objects
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(); }

}

}

upcasting v downcasting
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)
lab work
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)
later
Later…
  • Finish up Space Invaders
  • Review Sheet by Sunday
  • Good luck on the exam!