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

Slides:



Advertisements
Similar presentations
Arrays and ArrayLists Ananda Gunawardena. Introduction Array is a useful and powerful aggregate data structure presence in modern programming languages.
Advertisements

Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved. It is common to use two nested loops when filling or searching: for.
Week 10 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
Chapter 7: Arrays and Array Lists. To become familiar with using arrays and array lists To learn about wrapper classes, auto-boxing and the generalized.
Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved. Chapter Seven: Arrays and Array Lists.
Chapter 7 Arrays and Array Lists. Chapter Goals To become familiar with using arrays and array lists To learn about wrapper classes, auto-boxing and the.
Chapter 10 Introduction to Arrays
Grouping Objects Arrays and for loops. Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Fixed-Size Collections.
Chapter 7 – Arrays.
Computer Science A 10: 20/3. Array Array: Sequence of values of the same type Construct array: new double[10] Store in variable of type double[] double[]
Chapter 7  Arrays and Array Lists 1 Chapter 7 Arrays and Array Lists.
Datalogi A 8: 27/10. Array Array: Sequence of values of the same type Construct array: new double[10] Store in variable of type double[] double[] data.
Alice in Action with Java
Static Class Members Wrapper Classes Autoboxing Unboxing.
Chapter 9 Introduction to Arrays
CS 106 Introduction to Computer Science I 03 / 17 / 2008 Instructor: Michael Eckmann.
ARRAYS AND ARRAYLISTS Chapter 7. Array  Sequence of values of the same type  Primitive types  Objects  Create an Array  double[] values = new double[10]
CS0007: Introduction to Computer Programming Introduction to Arrays.
Chapter 6Java: an Introduction to Computer Science & Programming - Walter Savitch 1 l Array Basics l Arrays in Classes and Methods l Programming with Arrays.
Java Programming Week 6: Array and ArrayList Chapter 7.
For each primitive type there is a wrapper class for storing values of that type: Double d = new Double(29.95); Wrapper Classes Wrapper objects can be.
Week 9-10 – Arrays and Array Lists Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved.
ICOM 4015: Advanced Programming Lecture 7 Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Reading: Chapter Seven:
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 22 Java Collections.
Week 11 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
AP CS Workshop ArrayList It is very common for applications to require us to store a large amount of data. Array lists store large amounts of data.
ARRAYLIST Collections of Data. ArrayLists Array lists can grow and shrink as needed ArrayList is a generic class (similar to C++ template) ArrayList has.
Teach.NET Workshop Series Track 4: AP Computer Science with.NET and J#
French Territory of St. Pierre CSE 114 – Computer Science I Arrays.
Big Java Chapter 7-8 (ArrayList / Class Design) 2009.OOP.r1.
Chapter 11 Arrays Continued
ARRAYS 1 Week 2. Data Structures  Data structure  A particular way of storing and organising data in a computer so that it can be used efficiently 
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
When constructing a two-dimensional array, specify how many rows and columns are needed: final int ROWS = 3; final int COLUMNS = 3; String[][] board =
Arrays An array is a data structure that consists of an ordered collection of similar items (where “similar items” means items of the same type.) An array.
The while Loop Syntax while (condition) { statements } As long condition is true, the statements in the while loop execute.
Week 5 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
M180: Data Structures & Algorithms in Java Arrays in Java Arab Open University 1.
OBJECTS FOR ORGANIZING DATA -- As our programs get more sophisticated, we need assistance organizing large amounts of data. : array declaration and use.
ArrayList Class An ArrayList is an object that contains a sequence of elements that are ordered by position. An ArrayList is an object that contains a.
Fall 2006Slides adapted from Java Concepts companion slides1 Arrays and Array Lists Advanced Programming ICOM 4015 Lecture 7 Reading: Java Concepts Chapter.
Aug 9, CMSC 202 ArrayList. Aug 9, What’s an Array List ArrayList is  a class in the standard Java libraries that can hold any type of object.
Arrays…JavaCPython have fixed lengthyes*yesno are initialized to default values yesno? track their own lengthyesnoyes trying to access “out of bounds”
Week 14 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
Array Objectives To understand the concept of arrays To understand the purpose to which we use arrays. To be able to declare references to arrays. To be.
CSC 1051 M.A. Papalaskari, Villanova University Everyday objects: Strings and Wrappers CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari.
Chapter 7 – Arrays and Array Lists Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved.
Chapter 6 – Arrays and Array Lists Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved.
Arrays and ArrayLists Topic 6. One Dimensional Arrays Homogeneous – all of the same type Contiguous – all elements are stored sequentially in memory For.
CSE 1201 Object Oriented Programming ArrayList 1.
Array length = maximum number of elements in array Usually, array is partially filled Need companion variable to keep track of current size Uniform naming.
Arrays. Array: Sequence of values of the same type Construct array: Store in variable of type double[ ] new double[10] double[] data = new double[10];
Week 9 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
Chapter 7 – Arrays and Array Lists Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. 1.
Two Dimensional Arrays Found in chapter 8, Section 8.9.
Chapter 9 Introduction to Arrays Fundamentals of Java.
Chapter 7 – Arrays and Array Lists Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved.
LESSON 8: INTRODUCTION TO ARRAYS. Lesson 8: Introduction To Arrays Objectives: Write programs that handle collections of similar items. Declare array.
Week 9 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
Sections 10.1 – 10.4 Introduction to Arrays
Principles of Computer Science I
Chapter 8 – Arrays and Array Lists
Fundamentals of Java: AP Computer Science Essentials, 4th Edition
7.6 Common Array Algorithm: Filling
Introduction to Computer Science and Object-Oriented Programming
Java How to Program, Late Objects Version, 10/e
int [] scores = new int [10];
Presentation transcript:

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

Week 10 Topics Wrappers and Auto-boxing The Enhanced for Loop Partially Filled Arrays Common Array Algorithms Regression Testing Two-dimensional Arrays

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

Wrappers and Auto-boxing Cont. Primitive TypeWrapper Class byteByte booleanBoolean charCharacter doubleDouble floatFloat intInteger longLong shortShort

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;

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);

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

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; }

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(); }

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

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

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++; }

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

Common Array Algorithms Cont. Counting Matches double atLeast = ; int matches = 0; for (BankAccount a : accounts) { if (a.getBalance() >= atLeast) matches++; } System.out.println(matches);

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; } }

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());

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

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]

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];

Reference: Big Java 4th Edition by Cay Horstmann Wrappers and Auto-boxing (section 7.3 in Big Java) The Enhanced for Loop (section 7.4 in Big Java) Partially Filled Arrays (section 7.5 in Big Java) Common Array Algorithms (section 7.6 in Big Java) Regression Testing (section 7.7 in Big Java) Two-dimensional Arrays (section 7.8 in Big Java)