Title: Week 10
1Week 10
- Introduction to Computer Science and
Object-Oriented Programming - COMP 111
- George Basham
2Week 10 Topics
- 10.1.1 Wrappers and Auto-boxing
- 10.1.2 The Enhanced for Loop
- 10.1.3 Partially Filled Arrays
- 10.1.4 Common Array Algorithms
- 10.1.5 Regression Testing
- 10.1.6 Two-dimensional Arrays
3 10.1.1 Wrappers and Auto-boxing
- Because numbers are not objects in Java, you
cannot directly enter them in array lists - For example, this will not compile
- ArrayListltdoublegt data new ArrayListltdoublegt()
// No - To store sequences of numbers in an array list,
you must turn them into wrapper classes
4 10.1.1 Wrappers and Auto-boxing Cont.
Primitive Type Wrapper Class
byte Byte
boolean Boolean
char Character
double Double
float Float
int Integer
long Long
short Short
5 10.1.1 Wrappers and Auto-boxing Cont.
- Beginning with Java 5.0, if you assign a
primitive type to a wrapper class, the conversion
is automatic (called auto-boxing) - Double d 29.95
- Conversely, beginning with Java 5.0, wrapper
classes are automatically converted to primitive
types (called auto-unboxing) - double x d
6 10.1.1 Wrappers and Auto-boxing Cont.
- This will work to store simple number types in an
array list - ArrayListltDoublegt data new ArrayListltDoublegt()
// Yes - data.add(29.95)
- double x data.get(0)
7 10.1.2 The Enhanced for Loop
- Java version 5.0 introduces a very convenient
shortcut for a common loop type, when you need to
iterate through an sequence of elements - The new loop construct is know as the for each
loop - The for each loop has a very specific purpose,
traversing the elements of a collection from the
beginning to the end
8 10.1.2 The Enhanced for Loop Cont.
- double data new double100
- assume that array is then populated
- double sum 0
- for (double e data)
-
- sum sum e
9 10.1.2 The Enhanced for Loop Cont.
- ArrayListltBankAccountgt accounts new
ArrayListltBankAccountgt() - assume that array list is then populated
- double sum 0
- for (BankAccount a accounts)
-
- sum sum a.getBalance()
10 10.1.3 Partially Filled Arrays
- Since an array is fixed in length, and every
element may not be populated with a value, we
need to make sure not to access an element of an
array that does not contain a valid value - For an array of object references, this is even
more important from the aspect that calling a
method on a null reference will throw a runtime
exception
11 10.1.3 Partially Filled Arrays Cont.
- With a partially filled array, keep a companion
variable to track how many elements are used - public static final int LEN 100
- private int values new intLEN
- private valSize 0 // companion var
12 10.1.3 Partially Filled Arrays Cont.
- public int addElement(int inVal)
-
- if (this.valSize gt LEN)
- return -1
- this.valuesthis.valSize inVal
- // element index will be returned
- // then incremented for next use
- return this.valSize
-
13 10.1.4 Common Array Algorithms
- Demonstrated
- Counting Matches
- Searching for a Value (linear search)
- Finding the Maximum or Minimum
- Also (refer to textbook) Filling, Computing Sum
and Average Values, Locating the Position of an
Element, Removing an Element, Inserting an
Element, Copying and Growing an Array, Printing
Element Separators
14 10.1.4 Common Array Algorithms Cont.
- Counting Matches
- double atLeast 5000.00
- int matches 0
- for (BankAccount a accounts)
-
- if (a.getBalance() gt atLeast)
- matches
-
- System.out.println(matches)
15 10.1.4 Common Array Algorithms Cont.
- Searching for a Value (linear search)
- int accountNumber 1001
- for (BankAccount a accounts)
-
- if (a.getAccountNumber()
- accountNumber)
- System.out.println
- (a.getBalance()) break
16 10.1.4 Common Array Algorithms Cont.
- Finding the Maximum or Minimum
- BankAccount max accounts.get(0)
- for (int i 1 i lt accounts.size() i)
-
- BankAccount a account.get(i)
- if (a.getBalance() gt max.getBalance())
- max a
-
- System.out.println(max.getAccountNumber())
17 10.1.5 Regression Testing
- A test suite is a set of tests for repeated
testing - Regression testing involves repeating previously
run tests to ensure that known failures of prior
versions do not appear in new versions of the
software - Refactoring seeks to improve the internal design
and implementation of code without affecting its
externally visible behavior
18 10.1.6 Two-Dimensional Arrays
- int table new int23
- The array identified by table will have 2 rows
(the row is the FIRST subscript) and 3 columns
(the column is the SECOND subscript).
00 01 02
10 11 12
19 10.1.6 Two-Dimensional Arrays Cont.
- table00 22
- table01 1301
- . . .
- table12 43
- // Traversing the 2-D array
- for (int i 0 i lt 2 i) // rows
- for (int j 0 j lt 3 j) // cols
- System.out.println(tableij
20Reference Big Java 4th Edition by Cay Horstmann
- 10.1.1 Wrappers and Auto-boxing (section 7.3 in
Big Java) - 10.1.2 The Enhanced for Loop (section 7.4 in Big
Java) - 10.1.3 Partially Filled Arrays (section 7.5 in
Big Java) - 10.1.4 Common Array Algorithms (section 7.6 in
Big Java) - 10.1.5 Regression Testing (section 7.7 in Big
Java) - 10.1.6 Two-dimensional Arrays (section 7.8 in
Big Java)