Download presentation
Presentation is loading. Please wait.
1
Recitation 10 November 3, 2011
2
Today’s Goals: Learn Generics Practice with Generics
3
History Interface public interface History {
public void addElement(Object element); public Object elementAt (int index); public int size(); }
4
Generic History Interface
public interface History<T> { public void addElement(T element); public T elementAt (int index); public int size(); }
5
AHistory public class AHistory<T> implements History<T> {
public final int MAX_SIZE = 50; Object[] contents = new Object[MAX_SIZE]; int size = 0; public int size() {return size;} public T elementAt (int index) { return (T) contents[index]; } boolean isFull() {return size == MAX_SIZE;} public void addElement(T element) { if (isFull()) System.out.println("Adding item to a full history"); else { contents[size] = element; size++; A single implementation where T = Object is shared by all of its elaborations.
6
Instantiating A String History
History<String> stringHistory = new AHistory<String>(); History<String> stringHistory = new AHistory();
7
Recitation Specification
Download Recitation10.zip from the Recitations page Make changes to the given Stack interface to make it a “generic” interface. (Only change 3 lines) Now implement the interface with a class named AStack which is a generic class. (default size >=200) Internally just create an array of Objects to store the elements Cast to the correct type before returning in the get method. (This will signal a warning, which is okay.) Bonus (for candy): Create a “cool” design using the shape primitives provided, but specify coordinates using functions and loops. Ben will judge.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.