COS 260 DAY 11 Tony Gauvin.

Slides:



Advertisements
Similar presentations
Grouping objects Arrays and for loops. Fixed-size collections Sometimes the maximum collection size can be pre-determined. Programming languages usually.
Advertisements

Using interfaces Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling How would you find the maximum.
1 Features of the collection It increases its capacity as necessary. It keeps a private count: –size() accessor. It keeps the objects in order. Details.
Grouping objects Introduction to collections 5.0.
Grouping Objects Arrays and for loops. Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Fixed-Size Collections.
Grouping Objects 3 Iterators, collections and the while loop.
Programming with Collections Collections in Java Using Arrays Week 9.
Grouping objects Collections and iterators. 04/11/2004Lecture 4: Grouping Objects2 Main concepts to be covered Collections Loops Iterators Arrays.
Grouping objects Collections and iterators. Main concepts to be covered Collections Loops Iterators.
Objects First With Java A Practical Introduction Using BlueJ Grouping objects Collections and iterators 2.0.
Grouping objects Arrays, Collections and Iterators 1.0.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2009 Pearson Education, Inc., Upper.
JAVA: An Introduction to Problem Solving & Programming, 7 th Ed. By Walter Savitch ISBN © 2015 Pearson Education, Inc., Upper Saddle River,
Grouping objects Collections and iterators Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Main.
Chapter 4 Grouping Objects. Flexible Sized Collections  When writing a program, we often need to be able to group objects into collections  It is typical.
Objects First With Java A Practical Introduction Using BlueJ Supplementary Material for Java
Grouping objects Iterators. Iterator type Third variation to iterate over a collection Uses a while loop and Iterator object But NO integer index variable.
Final Review. From ArrayLists to Arrays The ArrayList : used to organize a list of objects –It is a class in the Java API –the ArrayList class uses an.
Objects First With Java A Practical Introduction Using BlueJ Grouping objects Collections and iterators 1.0.
1 COS 260 DAY 10 Tony Gauvin. 2 Agenda Questions? 4 th Mini quiz Today –Chapter 4 Assignment 2 Due Capstone Discussion Proposals Due Oct 15 No class on.
1 COS 260 DAY 14 Tony Gauvin. 2 Agenda Questions? 6 th Mini quiz graded  Oct 29 –Chapter 6 Assignment 4 will be posted later Today –First two problems.
Grouping objects Arrays. 2 Fixed-size collections The maximum collection size may be pre-determined with an upper limit Array is an fixed-size collection.
1 COS 260 DAY 12 Tony Gauvin. 2 Agenda Questions? 5 th Mini quiz –Chapter 5 40 min Assignment 3 Due Assignment 4 will be posted later (next week) –If.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Grouping objects Introduction to collections 5.0.
1 COS 260 DAY 7 Tony Gauvin. 2 Agenda Questions? 3 rd Mini quiz next class (September 28) –Chap 3 concepts Assignment 1 Corrected Assignment 2 posted.
1 COS 260 DAY 9 Tony Gauvin. 2 Agenda Questions? 3 rd Mini quiz –Good results ->All A’s –Threw out question on name overloading 4 th Mini quiz next class.
1 COS 260 DAY 22 Tony Gauvin. 2 Agenda Questions? 9 th Mini Quiz corrected –Good results Assignment 5 Not corrected yet Assignment 6 Posted (one more)
1 COS 260 DAY 18 Tony Gauvin. 2 Agenda Questions? 7 th Mini quiz Graded –Good results 8 th Mini Quiz –Chap 8  Next class Assignment 4 Due Assignment.
Grouping objects Iterators, collections and the while loop Equality and Equality == and equals(…)
Fixed-sized collections Introduction to arrays 6.0.
Chapter 9 Introduction to Arrays Fundamentals of Java.
Functional Processing of Collections (Advanced) 6.0.
© 2004 Pearson Addison-Wesley. All rights reserved October 5, 2007 Arrays ComS 207: Programming I (in Java) Iowa State University, FALL 2007 Instructor:
for-each PROS CONS easy to use access to ALL items one-by-one
Objects First with Java CITS1001 week 4
Objects First with Java CITS1001
SOFTWARE AND PROGRAMMING 1
Objects First with Java Introduction to collections
Fixed-sized collections
Chapter 7 Part 1 Edited by JJ Shepherd
Loop Structures.
Functional Processing of Collections (Advanced)
COS 260 DAY 17 Tony Gauvin.
COS 260 DAY 23 Tony Gauvin.
COS 260 DAY 9 Tony Gauvin.
COS 260 DAY 13 Tony Gauvin.
COS 260 DAY 6 Tony Gauvin.
COS 260 DAY 19 Tony Gauvin.
COS 260 DAY 11 Tony Gauvin.
COS 260 DAY 11 Tony Gauvin.
COS 260 DAY 10 Tony Gauvin.
COS 260 DAY 2 Tony Gauvin.
COS 260 DAY 18 Tony Gauvin.
COS 260 DAY 8 Tony Gauvin.
COS 260 DAY 3 Tony Gauvin.
COS 260 DAY 8 Tony Gauvin.
Object Oriented Programming in java
Objects First with Java Introduction to collections
COS 260 DAY 16 Tony Gauvin.
COS 260 DAY 19 Tony Gauvin.
Collections and iterators
COS 260 DAY 23 Tony Gauvin.
COS 260 DAY 14 Tony Gauvin.
COS 260 DAY 4 Tony Gauvin.
Arrays October 6, 2006 ComS 207: Programming I (in Java)
COS 260 DAY 23 Tony Gauvin.
COS 260 DAY 6 Tony Gauvin.
Collections and iterators
Arrays.
Presentation transcript:

COS 260 DAY 11 Tony Gauvin

Agenda Mini-quiz Five Today Friday the 13th Fixed Size Collections Questions Assignment 2 corrected Good Results Assignment 3 posted Due Oct 20 Assignment 4 will be posted soon Mini-quiz Five Today Friday the 13th Password “BreakingTheCode” Fixed Size Collections

Capstone Project Capstone Project Description Fall 2017.pdf October 2 Proposal Due E-mailed to Tony in Blackboard October 27 Progress Report E-mailed to Tony in Blackboard November 16 Progress Report E-mailed to Tony in Blackboard November 30 Progress Report E-mailed to Tony in Blackboard December 12@8AM All Deliverables & Presentation Due Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

Fixed-sized collections Objects First with Java Fixed-sized collections Introduction to arrays 6.0 © David J. Barnes and Michael Kölling

Features of arrays Fixed in length, unlike ArrayList, HashSet, HashMap, etc. Use a special syntax. For historical reasons. Objects with no methods. Methods are provided by other classes; e.g., java.util.Arrays. Methods that are static. https://docs.oracle.com/javase/specs/jls/se8/html/jls-10.html © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved.

Fixed-size collections Objects First with Java Fixed-size collections Sometimes the maximum collection size can be pre-determined. A special fixed-size collection type is available: an array. Unlike the flexible List collections, arrays can store object references or primitive-type values (without autoboxing). © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved. © David J. Barnes and Michael Kölling

The weblog-analyzer project Objects First with Java The weblog-analyzer project Web server records details of each access. Supports analysis tasks: Most popular pages. Busiest periods. How much data is being delivered. Broken references. Analyze accesses by hour – there is a fixed number of these in a day! © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved. © David J. Barnes and Michael Kölling

Log file analyzer Parses entries from a log weblog.txt Year month day hour min 2015 06 01 03 31 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

Creating an array object Objects First with Java Creating an array object Array type — does not contain size public class LogAnalyzer { private int[] hourCounts; private LogfileReader reader;   public LogAnalyzer() hourCounts = new int[24]; reader = new LogfileReader(); } ... Array object creation — specifies size © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved. © David J. Barnes and Michael Kölling

Objects First with Java The hourCounts array © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved. © David J. Barnes and Michael Kölling

Objects First with Java Using an array Square-bracket notation is used to access an array element: hourCounts[...] Elements are used like ordinary variables. The target of an assignment: hourCounts[hour] = ...; In an expression: hourCounts[hour]++; if(hourCounts[hour] > 0) ... © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved. © David J. Barnes and Michael Kölling

Objects First with Java Standard array use private int[] hourCounts; private String[] names; ...   hourCounts = new int[24]; hourcounts[i] = 0; hourcounts[i]++; System.out.println(hourcounts[i]); declaration creation use © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved. © David J. Barnes and Michael Kölling

Objects First with Java Array literals The size is inferred from the data. private int[] numbers = { 3, 15, 4, 5 }; declaration, creation and initialization Array literals in this form can only be used in declarations. Related uses require new: numbers = new int[] { 3, 15, 4, 5 }; © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved. © David J. Barnes and Michael Kölling

Array Literals 3 15 4 5 1 private int[] numbers = { 3, 15, 4, 5 }; Declare numbers Create Initialize 3 15 4 5 1 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

Objects First with Java Array length private int[] numbers = { 3, 15, 4, 5 }; int n = numbers.length; not a method call! NB: length is a field rather than a method. It’s value cannot be changed – ‘fixed size’. © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved. © David J. Barnes and Michael Kölling

Objects First with Java The for loop There are two variations of the for loop, for-each and for. The for loop is often used to iterate a fixed number of times. Often used with a variable that changes a fixed amount on each iteration. © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved. © David J. Barnes and Michael Kölling

Objects First with Java For loop pseudo-code General form of the for loop for(initialization; condition; post-body action) { statements to be repeated } Equivalent while-loop version initialization; while(condition) { statements to be repeated post-body action } © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved. © David J. Barnes and Michael Kölling

Objects First with Java Array iteration for loop version for(int hour = 0; hour < hourCounts.length; hour++) { System.out.println(hour + ": " + hourCounts[hour]); } while loop version int hour = 0; while(hour < hourCounts.length) { System.out.println(hour + ": " + hourCounts[hour]); hour++; } © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved. © David J. Barnes and Michael Kölling

Array-related methods System has static arraycopy. java.util.Arrays contains static utility methods for processing arrays: binarySearch fill sort ArrayList has toArray. © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved.

Objects First with Java Practice Given an array of numbers, print out all the numbers in the array, using a for loop. int[] numbers = { 4, 1, 22, 9, 14, 3, 9}; for ... © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved. © David J. Barnes and Michael Kölling

Objects First with Java Practice Fill an array with the Fibonacci sequence. 0 1 1 2 3 5 8 13 21 34 ... int[] fib = new int[howMany]; fib[0] = 0; fib[1] = 1; for(...) ... © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved. © David J. Barnes and Michael Kölling

for loop with bigger step Objects First with Java for loop with bigger step // Print multiples of 3 that are below 40. for(int num = 3; num < 40; num = num + 3) { System.out.println(num); } © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved. © David J. Barnes and Michael Kölling

for loop and Iterator No post-body action required. for(Iterator<Track> it = tracks.iterator(); it.hasNext(); ) { Track track = it.next(); if(track.getArtist().equals(artist)) { it.remove(); } © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved.

Objects First with Java Review Arrays are appropriate where a fixed-size collection is required. Arrays use a special syntax. For loops are used when an index variable is required. For loops offer an alternative to while loops when the number of repetitions is known. Used with a regular step size. © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved. © David J. Barnes and Michael Kölling

The automaton project An array of ‘cells’. Each cell maintains a simple state. Usually a small numerical value. E.g., on/off or alive/dead. The states change according to simple rules. Changes affected by neighboring states. © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved.

Cell states – blank cells are in state 0 A simple automaton nextState[i] = (state[i-1] + state[i] + state[i+1]) % 2; Step Cell states – blank cells are in state 0 + 1 2 3 4 5 6 © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved.

The conditional operator Choose between two values: condition ? value1 : value1 for(int cellValue : state) { System.out.print(cellValue == 1 ? '+' : ' '); } System.out.println(); © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved.

Further advanced material

Arrays of more than one dimension Array syntax supports multiple dimensions. E.g., 2D array to represent a game board, or a grid of cells. Can be thought of as an array of arrays. © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved.

The brain project Cell[][] cells; ... cells = new Cell[numRows][numCols]; for(int row = 0; row < numRows; row++) { for(int col = 0; col < numCols; col++) { cells[row][col] = new Cell(); } © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved.

Alternative iteration for(int row = 0; row < cells.length; row++) { Cell[] nextRow = cells[row]; for(int col = 0; col < nextRow.length; col++) { nextRow[col] = new Cell(); } ‘Array of array’ style. Requires no access to numRows and numCols. Works with irregular shape arrays, which are supported in Java. © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved.

Arrays and Streams java.util.Arrays has several stream() methods that return a Stream based on an array. IntStream is a Stream of int values. Instream.range() creates a sequential IntStream. toArray returns an array from a Stream. © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved.