Arrays Arrays Collections Set Database History Inheritance inheriting ancestor s traits inheriting benefactor s assets inheriting instance members methods variables IS A Relationship human IS A Mammal salmon IS A Fish Joe Doe IS A Student ALoan IS A Loan Strings Char Sequences String sequences of characters J o h n h e l l F o 1 K 4 3 l e n 0 n e d y Other Sequences as Predefined Types IntSequence 100 sequences of integers 98 99 100 90 80 60 40 50 DoubleSequence sequences of doubles 3 8 3 1 3 7 StringSequence JFK FDR JC 3 1 3 6 3 9 sequence of Strings BC RR GB Sequences of Programmer Defined Type LoanSequence Loan sequence of loans Array Types loan1 loan2 loan3 Arrays Temperature TemperatureSequence sequence of temperatures temperature1 temperature2 temperature3 temperature1 temperature2 Array Element Other Sequences as Array Types int 100 sequences of integers 98 99 100 90 80 60 40 50 double 3 8 3 1 sequences of doubles 3 7 String JFK FDR JC 3 1 3 6 3 9 sequence of strings BC RR GB Initializing Array Declarations Array Type Element Type int assignmentScores 100 98 99 80 assignmentScores 100 98 99 100 90 80 Array Literal Array Variable double gpas 3 8 3 1 3 7 3 1 3 6 3 9 3 8 3 1 3 7 3 1 3 6 3 9 String initials JFK FDR JC BC GW WW JFK FDR JC BC GW WW Initializing Array Declaration ElementType arrayVariable element1 elementN Loan loans new ALoan 100000 new AnotherLoan 100 Array Operations String initials JFK FDR JC BC GW WW HT JFK FDR JC BC GW WW public named constant initials length 6 initials 0 Array Instance Size Fixed initials initials length 1 initials initials length ArrayIndexOutOfBoundsException initials 0 HT initials initials length HT ArrayIndexOutOfBoundsException Array Types have Variable Size int 100 98 99 100 90 80 60 40 50 int assignmentScores 100 98 99 100 99 80 assignmentScores 60 40 50 assignmentScores 60 10040 98 50 99 100 90 80 Uninitializing Array Declaration int assignmentScores assignmentScores 60 40 50 assignmentScores 60 40 50 null Array Elements Uninitialized int assignmentScores new int 3 assignmentScores 0 0 0 Object Array Elements Uninitialized String initials new String 3 initials nullnull null Uninitialized Array Vs Element String initials initials 0 ArrayIndexOutOfBoundsException initials null String initials new String 3 initials 0 null initials 0 charAt 0 StringIndexOutOfBounds initials nullnull null Example getStrings variable static String getStrings System out println Number of Strings int numElements Keyboard readInt System out println Please enter numElements strings String strings new String numElements for int elementNum 0 elementNum numElements elementNum strings elementNum Keyboard readLine return strings print String array of arbitrary dimension size static void print String strings System out println for int elementNum 0 elementNum strings length elementNum System out println strings elementNum System out println main public static void main String args String names getStrings while true String command Keyboard readLine if command length 0 command charAt 0 q break if command length 0 command charAt 0 p print names main Variable Size Collection unfilled part filled current part size maximum size Variable Size Collection size array 3 James Dean Joe Doe filled part Jane Smith current size maximum size unfilled part Variable Size Collection public class ClassNeedingVariableSizeCollection final static int A MAX SIZE 50 String a new String MAX SIZE int aSize 0 process a for int index 0 index aSize index System out println a index final int B MAX SIZE 50 String b new String MAX SIZE int bSize 0 process b Special Type public class ClassNeedingVariableSizeCollection AVariableSizeCollection a new AVariableSizeCollection Size not updated for int index 0 index a size index System out println a contents index a contents a size Keyboard readString AVariableSizeCollection b new AVariableSizeCollection No encapsulation public class AVariableSizeCollection public static final int MAX SIZE 50 public String contents new String MAX SIZE public int size 0 Supporting Encapsulation public interface public static final int MAX SIZE 50 public void addElement String element public void print User specific Implementation specific History public interface StringHistory public void addElement String element public int size public String elementAt int index Implementing a History public class AStringHistory implements StringHistory public final int MAX SIZE 50 String contents new String MAX SIZE int size 0 public int size return size public String elementAt int index return contents index boolean isFull return size MAX SIZE public void addElement String element if isFull System out println Adding item to a full history else contents size element size Using a History public static void main String args StringHistory names new AStringHistory while true String input Keyboard readLine if input length 0 if input charAt 0 q break else if input charAt 0 p print names else names addElement input Printing a History static void print StringHistory strings System out println for int elementNum 0 elementNum strings size elementNum System out println strings elementAt elementNum Database Database public interface StringDatabase from history public int size public void addElement String element public String elementAt int index additional methods public void deleteElement String element public void member String element public void clear deleteElement String element deleteElement Joe Doe size 34 array James Dean public void deleteElement String element contents indexOf element contents size 1 size Joe John Doe Smith Jane Smith John Smith Elements out of order deleteElement String element deleteElement Joe Doe size array 4 James Dean index Joe Doe 1 Jane Smith John Smith public void deleteElement String element shiftUp indexOf element Multi element window deleteElement Joe Doe size array 4 James Dean public void deleteElement String element shiftUp indexOf element JaneDoe Smith index Joe 1 Jane Smith John Smith contents index contents index 1 deleteElement String element deleteElement Joe Doe size array 34 James Dean JaneDoe Smith index Joe 2 Jane John Smith Smith John Smith public void deleteElement String element shiftUp indexOf element void shiftUp int startIndex for int index startIndex index 1 size index contents index contents index 1 size indexOf String element indexOf Joe Doe size array 4 James Dean index Joe Doe 0 Jane Smith John Smith indexOf String element
View Full Document