190 likes | 417 Views
Agenda. Instanceof keyword Generic programming Grid class AbstractGrid class BoundedGrid class Preview topic for next class homework. Instanceof keyword. It checks if an object reference is an instance of a type, and returns a boolean value. Parent - superclass, Child –subclasses
E N D
Agenda • Instanceof keyword • Generic programming • Grid class • AbstractGrid class • BoundedGrid class • Preview topic for next class • homework
Instanceof keyword • It checks if an object reference is an instance of a type, and returns a boolean value. • Parent - superclass, Child –subclasses • objP – Parent object; objC – Child object • (objP instanceof Child) returns a ? • (objP instanceof Parent) returns a ? • (objC instanceof Child) returns a ? • (objC instanceof Parent) returns a ? • (objC instanceof Circle) returns a ?
Why do we need generic data type? Suppose we have a method(function) which takes an array as parameter and print out the elements. Normally we would have to specify the data type for the parameter array. It would be nice if this array can be any type. How to achieve this? • write overloading methods • write a generic parameters.
Generic Methods • All generic method declarations have a type parameter section delimited by angle brackets (< and >) that precedes the method's return type. • Each type parameter section contains one or more type parameters separated by commas. A type parameter, also known as a type variable, is an identifier that specifies a generic type name.
Generic Methods • The type parameters can be used to declare the return type and act as placeholders for the types of the arguments passed to the generic method, which are known as actual type arguments. • A generic method's body is declared like that of any other method. Note that type parameters can represent only reference types not primitive types (like int, double and char).
Generic method example public static < E > void printArray( E[] inputArray ) { for ( E element : inputArray ) { System.out.print( “ “ + element ); } System.out.println(); }
public static void main( String args[] ) { intArray = { 1, 2, 3, 4, 5 }; Double[] doubleArray = { 1.1, 2.2, 3.3, 4.4 }; Character[] charArray = { 'H', 'E', 'L', 'L', 'O' }; printArray( intArray ); printArray( doubleArray ); printArray( charArray ); }
ArrayList signature • ArrayList <variableName> = new ArrayList(); //type in the following statement, warning?? ArrayList studentList = new ArrayList(); studentList.add(“Andre”); studentList.add(“Nathan”); for (Object name : studentList) { System.out.println(name);
add(int index, Object element) inserts element at index position of the array Existing elements are shifted to the next position up in the array. • add(Object element) adds element to the end of the array. • get(int index) Returns the element at index position in the array. • remove(int index) removes the element at index position in the array. • set(int index, Object element) replaces the element at index position with element. • size() returns the number of elements in the array
ArrayList class • Implements a dynamic array. • A dynamic array varies in size during run time and is used in applications where the size of an array may need to grow or shrink. • Only stores objects, not primitive types.
Test program ArrayList<Integer> intList = new ArrayList<Integer>(); strList.add(new Integer(10)); strList.add(new Integer(20)); for(Object num:intList) System.out.print(num+" "); • designates the ArrayList as a list of Integers. This allows the ArrayList get() method to be used without casting. • change the enhanced for loop to regular for loop!!
Interface • An interface is a class with method declarations that have NO implementations. (there is no method body). • An interface describes aspects of a class other than those that it inherits from its parent. • An interface is a list of constants and method declarations. It doesn’t have instance variables. • A class that implements an interface must implement each of the methods listed in the interface.
Grid Interface public interface Grid<E> { int getNumRows(); int getNumCols(); boolean isValid(Location loc); E put(Location loc, E obj); E remove(Location loc); E get(Location loc); ArrayList<Location> getOccupiedLocations(); ArrayList<Location> getEmptyAdjacentLocations(Location loc) ArrayList<Location> getOccupiedAdjacentLocations(Location loc); ArrayList<E> getNeighbors(Location loc); }
The AbstractGrid<E> Class public abstract class AbstractGrid<E> implements Grid<E> { public ArrayList<E> getNeighbors(Location loc){..} public ArrayList<Location> getValidAdjacentLocations(Location loc){ ..} public ArrayList<Location> getEmptyAdjacentLocations(Location loc){..} public ArrayList<Location> getOccupiedAdjacentLocations(Location loc){..} public String toString(){…} }//this class implements 5 methods of the Grid<E>, Hence it is abstract
BoundedGrid class • What is its superclass? • How do you know that it is a generic class? • Does it override any methods in the superclass? • Does AbstractGrid has constructor? • Does BoundedGrid has constructor(s)? • What is BoundedGrid’s state variable? • What data type and data structure is its state variable?
public ArrayList<E> getNeighbors(Location loc) • Return a list of all actors in locations adjacent to loc. • public ArrayList<Location> getValidAdjacentLocations(Location loc) • Return all valid locations adjacent to loc. • public ArrayList<Location> getEmptyAdjacentLocations(Location loc) • Return all valid empty locations adjacent to loc. • public ArrayList<Location> getOccupiedAdjacentLocations(Location loc) • Return all valid occupied locations adjacent to loc. • public String toString() • Return a representation of the grid in string form.
How to display a grid? BoundedGrid<Bug> gr = new BoundedGrid<Bug>(20,20); World<Bug> world = new World<Bug>(gr); gr.put(new Location(row, col), new Bug()); world.setMessage(“..”); world.show();
Preview • 12/27 Critter Class Student Manual p29 – 31 and related topic on Barron’s book • 1/3 extending the critter class Student Manual p32-36 • 1/7 group activity • 1/10 review for the final
Homework • Write a program that produces a pile of flowers that is shaped like a righ angle triangle. • Write a program that produces a pile of bugs that is shaped like a equilateral triangle.