Weitere ähnliche Inhalte
Ähnlich wie 07 java api and inheritance (20)
Mehr von Program in Interdisciplinary Computing (20)
07 java api and inheritance
- 2. 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
- 3. 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.
- 4. 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
- 5. SO how is the ArrayList class different to regular arrays? LIS4930 © PIC CHECK OUT PAGE 136
- 6. 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”
- 7. How can we use the ArrayList class to improve our SimpleDotComGame? LIS4930 © PIC
- 8. 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()).
- 9. 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.
- 10. 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/
- 11. 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 }
- 12. 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.
- 13. 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.
- 14. 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
- 15. 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
- 17. 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.