510 likes | 521 Views
Learn why data structures can be complex, the importance of interfaces, inheritance challenges, and specialized class concepts. Dive into linked structures, stack, queue implementations, and practical coding examples in Java.
E N D
Big-Picture & Chapter 5 Why are things so complicated? & Linked Structures
Big Picture List (Objects)<interface> List (Batters)<array-based implementation> Linked-list based<implements List> Array-based List<implements List> List (Pitchers)<array-based implementation > … List (Batters)<linked-list implementation> … … List (Pitchers)<linked-list implementation > List (Coaches)<linked-list implementation > List (Coaches)<array-based implementation >
Why Interfaces? • It defines a standard way that a data structure is used. • Tells you what methods are available • Method name • Parameters • etc. • Programmers can switch to a completely different underlying implementation and they only have to change one line of code.
Big Picture List (Listable)<interface> Listable<interface> Linked-list based<implements List> Array-based List<implements List> Batter<implements Listable> … Pitcher<implements Listable> … … Coach<implements Listable > Fruitcake<implements Listable> Cockroach<implements Listable>
Why the Listable interface? • Wasn’t a list of Objects good enough? • Isn’t everything in Java an Object? • Yes, but the java.lang.Object does not have methods for comparing Objects • Thus, your list can never compare Objects to see if they are in the right order. • Thus, you can never have a sorted list.
Why the Listable interface? • The Listable Interface just provides an abstract method called compare, which can be implemented by any Class. • Thus, any Class that implements Listable must implement the compare function. • Thus, our List can maintain a sorted list without even knowing any details about the objects stored in the list.
Big Picture List (Player)<interface> Player<base-class> Linked-list based<implements List> Array-based List<implements List> Batter<extends Player> Pitcher<extends Player>
Can we use inheritance? • Yes, but here are the problems: • List (Players) can only call functions that are implemented in the Player base-class. • Players are sorted based on Rating • Rating is based on the 10 statistical categories, which are different for batters vs pitchers. • Thus, Rating can not be implmented inside of the Player base class.
Can we use inheritance? • We can create a sorted list of Batters or Pitchers • But we can’t put both Batters and Pitchers in a sorted list of Players. • Inheritance is really only useful in consolidating fields and methods from a set of classes that are similar. • Inheritance does NOT help us create generic data structures.
Can we use inheritance? • This is why Dr. B thinks that inheritance is not so important • Its just a useful way of organizing classes into hierarchies • Interfaces are much more useful in terms of recycling code and making generic data structures.
Special Classes • Self-referential class A class that includes an instance variable or variables that can hold a reference to an object of the same class • Inner class A class defined inside of another class. The outer class can access the private variables of the inner class.
Chapter 5 Linked Structures
Abstract View Internal View Results of Stack Operations Using StackNode (Cont’d)
Internal View Abstract View Results of Stack Operations Using StackNode (Cont’d)
Internal View Abstract View Results of Stack Operations Using StackNode (Cont’d)
Internal View Abstract View Results of Stack Operations Using StackNode (Cont’d)
Linked Structures Implementing a Queue
A Basic Linked List Sorted and Unsorted
The Remaining Methods • We define isThere and insert as abstract methods, leaving their implementation to the concrete classes. • We implement both retrieve and delete with our abstract LinkedList class.