• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
07 java api and inheritance
 

07 java api and inheritance

on

  • 1,051 views

 

Statistics

Views

Total Views
1,051
Views on SlideShare
996
Embed Views
55

Actions

Likes
0
Downloads
10
Comments
0

2 Embeds 55

http://pic.fsu.edu 44
https://pic.fsu.edu 11

Accessibility

Categories

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

    07 java api and inheritance 07 java api and inheritance Presentation Transcript

    • Do You Remember the SimpleDotComGame?
      LIS4930 © PIC
    • What if we could do this?
      LIS4930 © PIC
      REPEAT with each of the location cells in the int array
      //COMPARE the user guess to the location cell
      IF the user guess matches
      INCREMENT the number of hits
      //FIND OUT if it was the last location cell:
      IF number of hits is 3, RETURN “kill”
      ELSE it was not a kill, so RETURN “hit”
      END IF
      ELSE user guess did not match, so RETURN “miss”
      END IF
      END REPEAT
      REPEAT with each of the remaining location cells
      //COMPARE the user guess to the location cell
      IF the user guess matches
      REMOVE this cell from the array
      //FIND OUT if it was the last location cell:
      IF the array is now empty RETURN “kill”
      ELSE it was not a kill, so RETURN “hit”
      END IF
      ELSE user guess did not match, so RETURN “miss”
      END IF
      END REPEAT
    • Welcome To The Java API!
      LIS4930 © PIC
      You don’t have to reinvent the wheel if you know how to find what you need in the Java library, known as the Java API (Application Programming Interface).
      The core Java library is a giant pile of classes just waiting for you to use like building blocks, to assemble your own program out of largely pre-built code.
      The Java API is full of code you don’t even have to type. All you need to do is learn to use it.
    • The ArrayList class in the java.util package
      LIS4930 © PIC
      ArrayList
      add(Object element)
      Adds the object parameter to the list
      remove(int index)
      Removes the object at the index parameter
      remove(Object element)
      Removes this object (if it’s in the ArrayList)
      contains(Object element)
      Returns ‘true’ if there’s a match for the object parameter
      isEmpty()
      Returns ‘true’ if the list has no element
      indexOf(Object element)
      Returns either the index of the object parameter, or -1
      size()
      Returns the number of elements currently in the list
      get(int index)
      Returns the object currently at the index parameter
    • SO how is the ArrayList class different to regular arrays?
      LIS4930 © PIC
      CHECK OUT PAGE 136
    • SO how is the ArrayList class different to regular arrays?
      LIS4930 © PIC
      1
      2
      3
      4
      A plain old array has to know its size at the time it’s created.
      new String[2]
      new ArrayList <String>
      To put an object in a regular array, you must assign it to a specific location.
      myList[1] = b
      myList.add(b)
      Arrays use array syntax that’s not used anywhere else in Java.
      myList[1]
      ArrayLists in Java 5.0 are parameterized.
      parameterized type
      ArrayList <String>
      “a list of … Strings”
    • How can we use the ArrayList class to improve our SimpleDotComGame?
      LIS4930 © PIC
    • Using the Library (the Java API)
      LIS4930 © PIC
      In the Java API, classes are grouped into packages.
      To use a class in the API, you have to know which package the class is in.
      Every class in the Java library belongs to a package!
      ArrayList is in the package called java.util
      To use a class from the Java API, you MUST indicate the full name of the library class you want to use, and that means package name + class name.
      Even if you didn’t know it, you’ve already been using classes form a package. System (System.out.println), String, Math (Math.random()), and Integer (Integer.parseInt()).
    • You have to know the FULL name of the class you want to use in your code.
      LIS4930 © PIC
      1
      2
      ArrayList is not the full name of ArrayList, just as ‘Kelly’ isn’t a full name. The full name of ArrayList is actually:
      java.util.ArrayList
      You have to tell Java which ArrayList you want to use. You have two options:
      IMPORT
      Put an import statement at the top of your source code file.
      package name
      class name
      TYPE
      Type the full name everywhere in your code. Each time you use it. Anywhere you use it.
    • How to play with the API!
      LIS4930 © PIC
      What classes are in the library?
      Once you find a class, how do you know what it can do?
      1
      2
      Browse a book:
      Use the HTML API docs:
      http://java.sun.com/j2se/1.5.0/docs/api/
    • LIS4930 © PIC
      Chair Wars Revisited…
      Larry’s Cube
      (procedural approach)
      Brad’s Café
      (OOP approach)
      rotate (shapeNum, xPt, yPt) {
      //if the shape is not an amoeba,
      //calculate the center point
      //based on a rectangle, then
      //rotate.
      //else
      //use the xPt and yPt as the
      //rotation point offset and
      //then rotate.
      }
      playSound (shapeNum) {
      //if the shape is not an
      //amoeba, use shapeNum
      //to lookup which AIF
      //sound to play, and play it
      //else
      //play amoeba .hif sound
      }
      Amoeba
      intxPoint;
      intyPoint;
      rotate ( ) {
      //code to rotate an amoeba
      //using amoeba’s x and y
      }
      playSound ( ) {
      // code to play the
      // new .hif file for an
      // amoeba
      }
    • SO did Brad win the vacation?
      LIS4930 © PIC
      Triangle
      rotate ( )
      playSound ( )
      Well Larry didn’t go down without a fight and pointed out that Brad’s code had a lot of duplicated code in it…???
      Ahaa but did Larry see Brad’s final design?
      Square
      rotate ( )
      playSound ( )
      This is what Larry did…
      1
      rotate ( )
      playSound ( )
      Circle
      Amoeba
      Larry looked at what all four classes had in common.
      rotate ( )
      playSound ( )
      2
      Shape
      rotate ( )
      playSound ( )
      They’re all shapes, and they all rotate and playSound. So Larry abstracted out the common features and put them into a new class called Shape.
    • LIS4930 © PIC
      3
      Shape
      rotate ( )
      playSound ( )
      superclass
      Then Larry linked the other four classes to the new Shape class, in a relationship called inheritance.
      subclasses
      Circle
      Amoeba
      Square
      Triangle
      You can read this as, “Square inherits from Shape”, “Circle inherits from Shape”, and so on. rotate() and playSound() have been taken out of all other shapes and replaced by one copy in a superclasscalled Shape.The other four are the subclassesof Shape. The subclasses inherit the methodsof the superclass.
    • What about the Amoeba rotate()?
      LIS4930 © PIC
      4
      Wasn’t that the whole problem here – that the amoeba shape had a completely different rotate and playSound procedure? How can Amoeba do something different if it “inherits” its functionality from the Shape class???
      Shape
      rotate ( )
      playSound ( )
      Overriding just means that a subclass redefines one of its inherited methods when it needs to change or extend the behavior of that method.
      The Amoeba class overrides the methods of the Shape class. Then at runtime, the JVM knows exactly which rotate() method to run when someone tells the Amoeba to rotate.
      Overriding methods
      Amoeba
      rotate ( ) {
      // code to rotate a
      //circle
      }
      playSound ( ) {
      // code to play the
      // new .hif file for an
      // amoeba
      }
      Circle
      Square
      Triangle
    • Understanding Inheritance
      LIS4930 © PIC
      When one class inherits from another, the subclass inherits from the superclass.
      In Java, we say that the subclass extends the superclass.
      An inheritance relationship means that the subclass inherits the members (instance variables and methods) of the superclass.
      SuperHero
      suit
      tights
      specialPower
      useSpecPow()
      putOnSuit()
      Overriding methods
      PantherMan
      useSpecPow()
      putOnSuit()
      FriedEggMan
    • Let’s Look At Another Example
      LIS4930 © PIC
    • Design Steps of Inheritance (pages 170 -175)
      LIS4930 © PIC
      1
      2
      3
      4
      5
      Look for objects that have common attributes and behaviors.
      Design a class that represents the common state and behavior.
      Decide if a subclass needs behaviors (method implementations) that are specific to that particular subclass type.
      Look for more opportunities to use abstraction, by finding two or more subclasses that might need common behavior.
      Finish the class hierarchy.