Title: Arrays
1Arrays
- Arrays
- Collections (Set, Database, History)
- Inheritance
- inheriting ancestors traits
- inheriting benefactors 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
2Strings Char Sequences
String
sequences of characters
3Other Sequences as Predefined Types
sequences of integers
IntSequence
100
98
99
100
90
80
60
40
50
sequences of doubles
DoubleSequence
3.8
3.1
3.7
3.1
3.6
3.9
sequence of Strings
StringSequence
JFK
FDR
JC
BC
RR
GB
4Sequences of Programmer-Defined Type
sequence of loans
LoanSequence
Loan
loan1
loan2
loan3
sequence of temperatures
TemperatureSequence
Temperature
5Other Sequences as Array Types
sequences of integers
int
100
98
99
100
90
80
60
40
50
sequences of doubles
double
3.8
3.1
3.7
3.1
3.6
3.9
sequence of strings
String
JFK
FDR
JC
BC
RR
GB
6Initializing Array Declarations
int assignmentScores 100, 98, 99, 80
assignmentScores
100
98
99
100
90
80
7Initializing Array Declaration
ltElementTypegt ltarrayVariablegt ltelement1gt,
, ltelementNgt
Loan loans new ALoan(100000), new
AnotherLoan (100)
8Array Operations
initials.length
? 6
Array Instance Size Fixed
initials0
initialsinitials.length - 1
initialsinitials.length
? ArrayIndexOutOfBoundsException
initials0 HT
initialsinitials.length HT
? ArrayIndexOutOfBoundsException
9Array 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
100
98
99
100
90
80
10Uninitializing Array Declaration
int assignmentScores
assignmentScores 60, 40, 50
null
11Array Elements Uninitialized
int assignmentScores new int3
12Object Array Elements Uninitialized
String initials new String3
13Uninitialized Array Vs Element
String initials
initials0
? ArrayIndexOutOfBoundsException
String initials new String3
? null
initials0
initials0.charAt(0)
? StringIndexOutOfBounds
14Example
15getStrings()
static String getStrings()
System.out.println("Number of Strings")
int numElements Keyboard.readInt()
System.out.println("Please enter " numElements
" strings") String strings new
StringnumElements for (int elementNum
0 elementNum lt numElements elementNum)
stringselementNum Keyboard.readLine()
return strings
16print()
static void print(String strings)
System.out.println("")
for ( int elementNum 0 elementNum lt
strings.length elementNum)
System.out.println(stringselementNum)
System.out.println("")
17main()
public static void main(String args)
String names getStrings() while
(true) String command
Keyboard.readLine() if
(command.length gt 0 command.charAt(0)
'q') break if
(command.length gt 0 command.charAt(0)
'p') print(names)
18main()
19Variable-Size Collection
unfilled part
filled part
20Variable-Size Collection
size
array
3
filled part
unfilled part
21Variable-Size Collection
public class ltClassNeedingVariableSizeCollectiongt
final static int A_MAX_SIZE
50 String a new String MAX_SIZE int aSize
0 //process a for (int index 0 index lt
aSize index)
System.out.println(aindex) final int
B_MAX_SIZE 50 String b new String
MAX_SIZE int bSize 0 //process
b
22Special Type
public class ltClassNeedingVariableSizeCollectiongt
... AVariableSizeCollection a new
AVariableSizeCollection() ... for (int
index 0 index lt a.size index)
System.out.println(a.contentsindex)
AVariableSizeCollection b new
AVariableSizeCollection() ...
a.contentsa.size Keyboard.readString()
public class AVariableSizeCollection public
static final int MAX_SIZE 50 public String
contents new String MAX_SIZE public int size
0
23Supporting Encapsulation
public interface . public
static final int MAX_SIZE 50
public void addElement (String element)
public void print ()
24History
public interface StringHistory
public void addElement (String element)
public int size()
public String elementAt (int index)
25Implementing a History
public class AStringHistory implements
StringHistory public final int MAX_SIZE
50 String contents new
StringMAX_SIZE int size 0 public
int size() return size public String
elementAt (int index) return contentsindex
boolean isFull() return size MAX_SIZE
public void addElement(String element)
if (isFull()) System.out.println(
"Adding item to a full history") else
contentssize element
size
26Using a History
public static void main(String args)
StringHistory names new AStringHistory()
while (true) String input
Keyboard.readLine() if (input.length gt 0)
if (input.charAt(0) 'q') break
else if (input.charAt(0) 'p' )
print(names)
else names.addElement(input)
27Printing a History
static void print(StringHistory strings)
System.out.println("")
for ( int elementNum 0 elementNum lt
strings.size() elementNum) System.out.println
(strings.elementAt(elementNum))
28Database
29Database
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()
30deleteElement (String element)
deleteElement(Joe Doe)
size
array
public void deleteElement (String element)
contentsindexOf(element) contentssize -
1 size--
4
3
John Smith
John Smith
31deleteElement (String element)
deleteElement(Joe Doe)
size
array
public void deleteElement (String element)
shiftUp(indexOf(element))
4
John Smith
32Multi-element window
deleteElement(Joe Doe)
size
array
public void deleteElement (String element)
shiftUp(indexOf(element))
4
Jane Smith
John Smith
contentsindex contentsindex 1
33deleteElement (String element)
deleteElement(Joe Doe)
size
array
public void deleteElement (String element)
shiftUp(indexOf(element))
4
3
Jane Smith
void shiftUp (int startIndex) for (int
index startIndex index 1 lt size index)
contentsindex contentsindex
1 size--
John Smith
John Smith
34indexOf (String element)
indexOf(Joe Doe)
size
array
4
John Smith
35indexOf (String element)
deleteElement(Joe Doe)
size
array
4
public int indexOf (String element) for
( index 0 index lt size
!element.equals(contentsindex
index)
return index
John Smith
36public boolean member(String element)
member(Joe Doe)
size
array
public int indexOf (String element) for (
index 0 index lt size
!element.equals(contentsindex
index)
return index
4
John Smith
public boolean member (String element)
return indexOf (element) lt size
37public void clear()
clear()
size
array
4
3
2
1
0
public void clear() while ( size gt 0)
deleteElement(size -1)
John Smith
38public void clear()
clear()
size
array
4
public void clear() size 0
0
John Smith
39addElement(Mary Doe)
size
array
4
1
John Smith
40Logical but not Physical Extensions
41Database
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()
42Physical and Logical Extensions
43Extending an Interface
public interface StringDatabase extends
StringHistory
public void deleteElement(String element)
public void member (String element)
public void clear()
44Extending a Class
public class AStringDatabase extends
AStringHistory implements StringDatabase
public void deleteElement (String element)
int indexOf (String element)
void shiftUp (int startIndex)
public boolean member(String element)
public void clear()
45Physical and Computer Inheritance
AStringDatabase
Human
StringDatabase
Deluxe Accord
46No Explicit Extension
public class AStringHistory implements
StringHistory public final int MAX_SIZE
50 String contents new
StringMAX_SIZE int size 0 public
int size() return size public String
elementAt (int index) return contentsindex
boolean isFull() return size MAX_SIZE
public void addElement(String element)
if (isFull()) System.out.println(
"Adding item to a full history") else
contentssize element
size
47Equivalent Class Definition
public class AStringHistory extends Object
implements StringHistory public final int
MAX_SIZE 50 String contents new
StringMAX_SIZE int size 0 public
int size() return size public String
elementAt (int index) return contentsindex
boolean isFull() return size MAX_SIZE
public void addElement(String element)
if (isFull()) System.out.println(
"Adding item to a full history") else
contentssize element
size
48Some Methods of Class Object
toString( )
size
size( )
EXTENDS
AString History
Object
contents
elementAt( )
equals( )
MAX_SIZE
addElement( )
clone( )
isFull( )
(new AStringHistory()).toString()
EXTENDS
(new AStringDatabase()).toString()
member( )
AString Database
(new ALoan()).toString()
deleteElement( )
hello.toString()
indexOf( )
h.toString()
shiftUp( )
clear( )
5.toString()
49IS-A Relationships
Object
StringHistory
AStringHistory
String
AStringDatabase
StringDatabase
implements
extends
50IS-A Polymorphism
public static Loan add(Loan loan1, Loan loan2)
return new ALoan(loan1.getPrincipal()
loan2.getPrincipal()))
IS-A
51Printing a History
static void print(StringHistory strings)
System.out.println("")
for ( int elementNum 0 elementNum lt
strings.size() elementNum) System.out.println
(strings.elementAt(elementNum))
AStringHistory IS-A StringHistory
AStringDatabase IS-A AStringHistory
AStringDatabase IS-A StringHistory
52Assignment Rules for Primitive Types
- If T1 narrower than T2 (Set of instances of T1 ?
Set of instances of T2) - Expression of type T1 can be assigned to Variable
of type T2 - Expression of type T2 can be assigned to Variable
of type T1 with cast.
53Assignment Rules for Object Types
- If T1 IS-A T2
- Expression of type T1 can be assigned to Variable
of type T2 - Expression of type T2 can be assigned to Variable
of type T1 with cast.
54IS-A Definition
- Implements T1 implements T2 gt T1 IS-A T2
- Extends T1 extends T2 gt T1 IS-A T2
- Transitive
- T1 IS-A T2
- T2 IS-A T3
- gt T1 IS-A T3
- Reflexive
- T1 T2 gt T1 IS-A T2
55Type Checking Examples
StringHistory stringHistory new
AStringDatabase()
StringDatabase stringDatabase new
AStringHistory()
56Getting an Upgrade
ARegularModel myCar new ADeluxeModel ()
57Getting a Downgrade
ADeluxeModel myCar new ARegularModel ()
58Type Checking Examples
StringHistory stringHistory new
AStringDatabase()
stringHistory.size()
stringHistory .clear()
((StringDatabase) stringHistory) .clear()
StringDatabase stringDatabase new
AStringHistory()
stringDatabase.clear()
59Type Checking Examples
Object objects Joe Doe, new
AStringDatabase(), new AStringHistory()
String strings Joe Doe, new Object()
60Database
61Set
62Overriding Inherited Methods
toString( )
size
size( )
EXTENDS
AString History
Object
contents
elementAt( )
equals( )
MAX_SIZE
addElement( )
clone( )
isFull( )
EXTENDS
member( )
AString Database
addElement( )
deleteElement( )
indexOf( )
shiftUp( )
clear( )
63Overriding addElement()
public void addElement(String element)
if (isFull()) System.out.println("Addi
ng item to a full history") else
contentssize element
size
public void addElement(String element)
if (member(element)) return if
(isFull()) System.out.println("Adding
item to a full history") else
contentssize element
size
64super
public void addElement(String element)
if (isFull()) System.out.println("Addi
ng item to a full history") else
contentssize element
size
public void addElement(String element)
if (member(element)) return
super.addElement()
65Omitting Super
public void addElement(String element)
if (isFull()) System.out.println("Addi
ng item to a full history") else
contentssize element
size
public void addElement(String element)
if (member(element)) return
addElement()
66More Overriding
toString( )
size
size( )
EXTENDS
AString History
Object
contents
elementAt( )
equals( )
MAX_SIZE
addElement( )
clone( )
isFull( )
EXTENDS
member( )
AString Database
addElement( )
deleteElement( )
indexOf( )
shiftUp( )
clear( )
toString( )
67More Overriding
stringSet.toString()
? AStringSet_at_1eed58
public String toString() String retVal
for (int i 0 i lt size i) retVal
contentsi return retVal
stringSet.toString()
? James DeanJohn Smith
68Motivation for Switch
public static void main(String args)
StringDatabase names new AStringDatabase()
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 if
(input.charAt(0) 'd')
names.deleteElement(input.substring(2,
input.length())) else if
(input.charAt(0) 'm')
System.out.println(names.member(input.substring(2,
input.length()))) else if
(input.charAt(0) 'c')
names.clear() else
names.addElement(input)
69Main with Switch
public static void main(String args)
StringDatabase names new AStringDatabase()
while (true) String input
Keyboard.readLine() if
(!(input.length() 0)) if
(input.charAt(0) 'q') break else switch
(input.charAt(0)) case 'p'
print(names) break case 'd'
names.deleteElement(input.substring(2,
input.length())) break case 'm'
System.out.println(names.member(input.substri
ng(2, input.length()))) break case
'c' names.clear() break
default names.addElement(input)
70Multi-case arms
public static void main(String args)
StringDatabase names new AStringDatabase()
while (true) String input
Keyboard.readLine() if
(!(input.length() 0)) if
(input.charAt(0) 'q') break else switch
(input.charAt(0)) case 'p, P
print(names) break case 'd, D
names.deleteElement(input.substring(2,
input.length())) break case 'm,
M System.out.println(names.member(input
.substring(2, input.length()))) break
case 'c, C names.clear()
break default names.addElement(input
)
71Omitting break
public static void main(String args)
StringDatabase names new AStringDatabase()
while (true) String input
Keyboard.readLine() if
(!(input.length() 0)) if
(input.charAt(0) 'q') break else switch
(input.charAt(0)) case 'p, P
print(names) case 'd, D
names.deleteElement(input.substring(2,
input.length())) case 'm, M
System.out.println(names.member(input.substring(2,
input.length()))) case 'c, C
names.clear() default
names.addElement(input)
72Illegal Switch
Type of switch expression must be ordinal type
switch (input ) case print
print(names) case clear
names.clear() default
names.addElement(input)
73Ordinal Type
- Values of type are ordered.
- Each value has a unique successor and predecessor
int
char
String
double