Presentation is loading. Please wait.

Presentation is loading. Please wait.

Week 10 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.

Similar presentations


Presentation on theme: "Week 10 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham."— Presentation transcript:

1 Week 10 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham

2 Week 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: ArrayList data = new ArrayList (); // 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 TypeWrapper Class byteByte booleanBoolean charCharacter doubleDouble floatFloat intInteger longLong shortShort

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: ArrayList data = new ArrayList (); // 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 double[100]; … 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. ArrayList accounts = new ArrayList (); … 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 int[LEN] private valSize = 0; // companion var

12 10.1.3 Partially Filled Arrays Cont. public int addElement(int inVal) { if (this.valSize >= LEN) return -1; this.values[this.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() >= 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 < accounts.size(); i++) { BankAccount a = account.get(i); if (a.getBalance() > 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 int[2][3]; The array identified by table will have 2 rows (the row is the FIRST subscript) and 3 columns (the column is the SECOND subscript). [0][0][0][1][0][2] [1][0][1][1][1][2]

19 10.1.6 Two-Dimensional Arrays Cont. table[0][0] = 22; table[0][1] = 1301;... table[1][2] = 43 // Traversing the 2-D array for (int i = 0; i < 2; ++i) // rows for (int j = 0; j < 3; ++j) // cols System.out.println(table[i][j];

20 Reference: 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)


Download ppt "Week 10 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham."

Similar presentations


Ads by Google