Unformatted text preview:

ArraysStrings = Char SequencesOther Sequences as Predefined TypesSequences of Programmer-Defined TypeOther Sequences as Array TypesInitializing Array DeclarationsInitializing Array DeclarationArray OperationsArray Types have Variable-SizeUninitializing Array DeclarationArray Elements UninitializedObject Array Elements UninitializedUninitialized Array Vs ElementExamplegetStrings()print()main()Slide 18Variable-Size CollectionSlide 20Slide 21Special TypeSupporting EncapsulationHistoryImplementing a HistoryUsing a HistoryPrinting a HistoryDatabaseSlide 29deleteElement (String element)Slide 31Multi-element windowSlide 33indexOf (String element)Slide 35public boolean member(String element)public void clear()Slide 38addElement(“Mary Doe”)Logical but not Physical ExtensionsSlide 41Physical and Logical ExtensionsExtending an InterfaceExtending a ClassPhysical and Computer InheritanceNo Explicit ExtensionEquivalent Class DefinitionSome Methods of Class ObjectIS-A RelationshipsIS-A & PolymorphismSlide 51Assignment Rules for Primitive TypesAssignment Rules for Object TypesIS-A DefinitionType Checking ExamplesGetting an UpgradeGetting a DowngradeSlide 58Slide 59Slide 60SetOverriding Inherited MethodsOverriding addElement()superOmitting SuperMore OverridingSlide 67Motivation for SwitchMain with SwitchMulti-case armsOmitting breakIllegal SwitchOrdinal TypeArrays•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 LoanStrings = Char SequencesJ o h n F . K e n n d yeh e l l oString {sequences of characters}1 4 3 l 0Other Sequences as Predefined Types100 98 99 100 9060 40 50{sequences of integers}80JFK FDR{sequence of Strings}JC BC RR GB3.8 3.1{sequences of doubles}3.7 3.1 3.6 3.9IntSequenceDoubleSequenceStringSequenceSequences of Programmer-Defined Typeloan1 loan2{sequence of loans}loan3temperature1{sequence of temperatures}temperature2 temperature3temperature1 temperature2TemperatureSequenceLoanSequenceLoan[]Temperature[]Array TypesArraysArray ElementOther Sequences as Array Types100 98 99 100 9060 40 50{sequences of integers}80{sequence of strings}3.8 3.1{sequences of doubles}3.7 3.1 3.6 3.9int[]double[]String[]JFK FDR JC BC RR GBInitializing Array Declarations100 98 99 100 90 80int[] assignmentScores = {100, 98, 99, 80};3.8 3.1 3.7 3.1 3.6 3.9double[] gpas = {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 WWassignmentScoresArray TypeElement TypeArray LiteralArray VariableInitializing Array Declaration<ElementType> [] <arrayVariable> = {<element1>, …, <elementN>}Loan [] loans = {new ALoan(100000), new AnotherLoan (100)};Array OperationsString[] initials = {“JFK”, “FDR”, “JC”, “BC”, “GW”, “WW”}; JFK FDR JC BC GW WWinitials.length  6initials[0]initials[initials.length - 1]initials[initials.length]  ArrayIndexOutOfBoundsExceptionpublic named constantinitials[0] = “HT”HTinitials[initials.length] = “HT”  ArrayIndexOutOfBoundsExceptionArray Instance Size FixedArray Types have Variable-Size100 98 99 100 9060 40 5080int[]100 98 99 100 90 80int[] assignmentScores = {100, 98, 99, 100, 99, 80};assignmentScoresassignmentScores = {60, 40, 50};assignmentScores60 40 50Uninitializing Array Declarationint[] assignmentScores;assignmentScoresnullassignmentScores = {60, 40, 50};assignmentScores60 40 50Array Elements Uninitializedint[] assignmentScores = new int[3];assignmentScores0 0 0Object Array Elements UninitializedString[] initials = new String[3];initialsnullnull nullUninitialized Array Vs ElementString[] initials = new String[3];initialsnullnull nullString[] initials;initialsnullinitials[0] nullinitials[0]initials[0].charAt(0)  StringIndexOutOfBounds ArrayIndexOutOfBoundsExceptionExamplegetStrings()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;}variableprint()static void print(String[] strings) { System.out.println("******************"); for ( int elementNum = 0; elementNum < strings.length; elementNum++) System.out.println(strings[elementNum]); System.out.println("******************");}String array of arbitrary dimension (size)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 Collectionfilled partunfilled partcurrent sizemaximum size3James DeanJoe DoeJane Smithsize arrayVariable-Size Collectionfilled partunfilled partcurrent sizemaximum sizeVariable-Size Collectionpublic class <ClassNeedingVariableSizeCollection> { …final static int A_MAX_SIZE = 50;String[] a = new String [MAX_SIZE];int aSize = 0;…//process afor (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 Typepublic class <ClassNeedingVariableSizeCollection> { ... AVariableSizeCollection a = new AVariableSizeCollection(); ... for (int index = 0; index < a.size; index++) System.out.println(a.contents[index]); … AVariableSizeCollection b = new AVariableSizeCollection(); ...}public class AVariableSizeCollection {public static final int MAX_SIZE = 50;public String[] contents = new String [MAX_SIZE];public int size = 0;}No encapsulation!a.contents[a.size] = Keyboard.readString();Size not updatedSupporting Encapsulationpublic interface …. {public static final int MAX_SIZE = 50;}public void addElement (String element);public void print ();Implementation specificUser specificHistorypublic interface StringHistory {}public void addElement (String element);public int size();public String elementAt (int index);Implementing a Historypublic class AStringHistory


View Full Document

UNC-Chapel Hill COMP 14 - Arrays

Download Arrays
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Arrays and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Arrays 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?