View
847
Download
0
Tags:
Embed Size (px)
DESCRIPTION
Citation preview
LIS4930 © PIC
Do You Remember the SimpleDotComGame?
LIS4930 © PIC
What if we could do this?
REPEAT with each of the location cells in the int array
//COMPARE the user guess to the location cellIF 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 cellIF 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 IFELSE user guess did not match, so
RETURN “miss”END IF
END REPEAT
LIS4930 © PIC
Welcome To The Java API!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.
LIS4930 © PIC
The ArrayList class in the java.util package
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
LIS4930 © PIC
SO how is the ArrayList class different to regular arrays?
CHECK OUT PAGE 136
LIS4930 © PIC
SO how is the ArrayList class different to regular arrays?
1 A plain old array has to know its size at the time it’s created.
new String[2]
new ArrayList <String>
2 To put an object in a regular array, you must assign it to a specific location.
myList[1] = b
3 Arrays use array syntax that’s not used anywhere else in Java.
myList[1]
4 ArrayLists in Java 5.0 are parameterized.
ArrayList <String>
myList.add(b)
parameterized type
“a list of … Strings”
LIS4930 © PIC
How can we use the ArrayList class to improve our SimpleDotComGame?
LIS4930 © PIC
Using the Library (the Java API)
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()).
LIS4930 © PIC
You have to know the FULL name of the class you want to use in your code.
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
package name
class name
You have to tell Java which ArrayList you want to use. You have two options:
1 IMPORTPut an import statement at the top of your source code file.
2 TYPEType the full name everywhere in your code. Each time you use it. Anywhere you use it.
LIS4930 © PIC
How to play with the API!
1What classes are in the library? 2
Once you find a class, how do you know what it can do?
Browse a book: Use the HTML API docs:http://java.sun.com/j2se/1.5.0/docs/api/
LIS4930 © PIC
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}
int xPoint;int yPoint;
rotate ( ) {//code to rotate an
amoeba //using amoeba’s x
and y}
playSound ( ) {// code to play the // new .hif file for an// amoeba
}
Amoeba
Chair Wars Revisited…
LIS4930 © PIC
SO did Brad win the vacation?
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?This is what Larry
did…Square
rotate ( ) playSound ( )
rotate ( ) playSound ( )
Triangle
rotate ( ) playSound ( )
Circle rotate ( )
playSound ( )
AmoebaLarry looked at what all four classes had in common.
1
2
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.
Shape
rotate ( )playSound ( )
LIS4930 © PIC
3
Then Larry linked the other four classes to the new Shape class, in a relationship called inheritance.
Shape
rotate ( )playSound ( )
superclass
subclasses
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
superclass called Shape. The other four are the
subclasses of Shape. The subclasses inherit the
methods of the superclass.
Square Triang
le
Circle Amoeba
LIS4930 © PIC
What about the Amoeba rotate()?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 ( )
Square Triang
le
Circle
rotate ( ) {// code to
rotate a //circle
}
playSound ( ) {// code to play
the // new .hif file
for an// amoeba
}
Amoeba
4
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 just means that a subclass redefines one of its inherited methods when it needs to change or extend the behavior of that method.
Overriding methods
LIS4930 © PIC
Understanding Inheritance
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
suittightsspecialPoweruseSpecPow()putOnSuit()FriedEgg
ManPantherM
anuseSpecPow()putOnSuit()
Overriding methods
LIS4930 © PIC
Let’s Look At Another Example
LIS4930 © PIC
Design Steps of Inheritance (pages 170 -175)
1 Look for objects that have common attributes and behaviors.
2 Design a class that represents the common state and behavior.
3 Decide if a subclass needs behaviors (method implementations) that are specific to that particular subclass type.
4 Look for more opportunities to use abstraction, by finding two or more subclasses that might need common behavior.
5 Finish the class hierarchy.