Objects First With Java A Practical Introduction Using BlueJ Supplementary Material for Java 5 2.0.

Slides:



Advertisements
Similar presentations
Grouping objects Iterators. Iterator and iterator() Collections have an iterator() method. This returns an Iterator object. Iterator has three methods:
Advertisements

1-May-15 Java 1.5. Reason for changes “The new language features all have one thing in common: they take some common idiom and provide linguistic support.
Objects First With Java A Practical Introduction Using BlueJ Improving structure with inheritance 2.0.
Improving structure with inheritance Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Main concepts.
Collections & Loops Chapter 5 Copyright © 2012 Pearson Education, Inc.
Using interfaces Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling How would you find the maximum.
Improving structure with inheritance (Chapters 8 and 9)
Designing classes How to write classes in a way that they are easily understandable, maintainable and reusable 4.0.
Improving structure with inheritance
More sophisticated behaviour Using library classes to implement some more advanced functionality.
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.
Chapter 101 Dynamic Data Structures and Generics Chapter 10.
1 Java Object Model Part 1. 2 Type Definition: set of values – a set of values and set of operations –a set of operations that can be applied to those.
Understanding class definitions Looking inside classes 3.0.
Java 1.5 & Effective Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Grouping Objects 1 Introduction to Collections.
Make Sure You Know All This!. Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling 2 Objects and Classes.
Grouping objects Collections and iterators. 04/11/2004Lecture 4: Grouping Objects2 Main concepts to be covered Collections Loops Iterators Arrays.
Understanding class definitions – Part II –. Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Main.
More sophisticated behavior Using library classes to implement some more advanced functionality 3.0.
Chapter 101 Dynamic Data Structures and Generics Chapter 10.
Chapter 10 2D Arrays Collection Classes. Topics Arrays with more than one dimension Java Collections API ArrayList Map.
Generic Java 21/ What is generics? To be able to assign type variables to a class These variables are not bound to any specific type until the.
CSE373 Optional Section Java Collections 11/12/2013 Luyi Lu.
Random, Collections & Loops Chapter 5 Copyright © 2012 Pearson Education, Inc.
Grouping objects Collections and iterators. Main concepts to be covered Collections Loops Iterators.
David Streader Computer Science Victoria University of Wellington Copyright: David Streader, Victoria University of Wellington Java Arrays and ArrayLists.
Improving structure with inheritance Main concepts to be covered Inheritance Subtyping Substitution Polymorphic variables Objects First with Java.
CSC 142 J(part 1) 1 CSC 142 The ArrayList class [Reading: chapter 10]
Improving structure with inheritance Main concepts to be covered Inheritance Subtyping Substitution Polymorphic variables Objects First with Java.
Objects First With Java A Practical Introduction Using BlueJ Grouping objects Collections and iterators 2.0.
Grouping objects Arrays, Collections and Iterators 1.0.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
More sophisticated behavior Using library classes to implement some more advanced functionality 5.0.
Java 5 Part 1 CSE301 University of Sunderland Harry Erwin, PhD.
Grouping objects Introduction to collections 5.0.
Designing classes How to write classes in a way that they are easily understandable, maintainable and reusable 5.0.
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.
CompSci 100E 40.1 Java 5 New Features  Generics  Enhanced for loop  Autoboxing/unboxing  Typesafe enums  Other  Varargs  Static Import  Metadata.
Java 1.5 The New Java Mike Orsega Central Carolina CC.
Java 5 Part 2 CSE301 University of Sunderland Harry Erwin, PhD.
Grouping objects Iterators. Iterator type Third variation to iterate over a collection Uses a while loop and Iterator object But NO integer index variable.
Objects First With Java A Practical Introduction Using BlueJ Grouping objects Collections and iterators 1.0.
©SoftMoore ConsultingSlide 1 Generics “Generics constitute the most significant change in the Java programming language since the 1.0 release.” – Cay Horstmann.
Chapter 4 Generic Vector Class. Agenda A systemic problem with Vector of Object – Several approaches at a solution – Generic structures Converting classes.
1 CSC 2053 New from AutoBoxing 3 Before J2SE 5.0, working with primitive types required the repetitive work of converting between the primitive.
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.
Grouping objects Introduction to collections 5.0.
Collections and Iteration Week 13.  Collections  ArrayList objects  Using loops with collections Collections and Iteration CONCEPTS COVERED THIS WEEK.
1 COS 260 DAY 17 Tony Gauvin. 2 Agenda Questions? 7 th Mini quiz –Chapter 7 –Password “GoBengals” –40 min Assignment 4 posted –Due Nov 9 (one week) Capstone.
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(…)
Review. Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Objects and Classes Objects and Classes –State.
New Java Features Advanced Programming Techniques.
Coming up ArrayList ArrayList vs Array – Declaration – Insertion – Access – Removal Wrapper classes Iterator object.
More-Sophisticated Behavior
Objects First with Java CITS1001 week 4
Objects First with Java Introduction to collections
More sophisticated behavior
Collections & Definite Loops
Object Oriented Programming in java
Exercise 1 Declare a constant of type int called SIZE and initialize it to value 10 Declare two int arrays of size “SIZE” Assume that those int arrays.
COS 260 DAY 11 Tony Gauvin.
Collections and iterators
Java 5 New Features 1-May-19.
Improving structure with inheritance
Collections and iterators
Presentation transcript:

Objects First With Java A Practical Introduction Using BlueJ Supplementary Material for Java 5 2.0

2 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Main concepts to be covered Typed collections (Generics) Enhanced For Loop Autoboxing and Unboxing Typesafe Enums Static Imports Input using Scanner

3 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Typed collections (Generics) Collections use polymorphism to store objects of any type. A drawback is type loss on retrieval: Lot selected = (Lot)lots.get(i); Use of untyped collections is considered ‘unsafe’. Typed collections avoid type loss. Runtime checks are simplified because the type is known.

4 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling New syntax The type of object to be stored is indicated on declaration: private ArrayList notes;... and on creation: notes = new ArrayList (); Collection types are parameterized.

5 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Using a typed collection Because the collection is typed, no cast is required on retrieval: Lot selection = lots.get(i);

6 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Typed Iteration A typed iterator can be obtained from a typed collection: Iterator it = lots.iterator(); while(it.hasNext()) { Lot lot = it.next(); System.out.println(lot); }

7 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Typed HashMaps HashMaps operate with (key,value) pairs. A typed HashMap required two type parameters: private HashMap responses;... responses = new HashMap ();

8 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Enhanced For Loop Iteration over arrays and collections is a common operation. It is easy to get the terminating condition wrong. An enhanced for loop simplifies iteration. –Only if the loop variable is not required.

9 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Enhanced For Loop for(int i = 0; i < array.length; i++) { Type element = array[i];... } for(Type element : array) {... } Original style Enhanced style ‘for each element in array’

10 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling For loop with Iterator Iterator it = entries.iterator(); while(it.hasNext()) { System.out.println(it.next()); } Original style for(LogEntry entry : entries) { System.out.println(entry); } Enhanced style

11 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Autoboxing and Unboxing Wrapper classes not required when using primitive types in object contexts. Autoboxing: int i = 18; myCollection.add(i); Unboxing : int i = myCollection.get(0);

12 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Typesafe Enums A new syntax allowing definition of enumerated types. Will typically replace lists of named, related, integer values, such as: public static final int LOW = 0, MEDIUM = 1, HIGH = 2;

13 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling An Enumerated Type Values are referred to like static variables, e.g. Level.LOW Values are distinct from ints and have no associated numeric value. public enum Level { LOW, MEDIUM, HIGH }

14 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Static Imports Allow import of static methods and variables, e.g. import static java.lang.Math.abs; import static java.lang.Math.PI; Imported names can be used without qualification. A relatively minor addition to the language.

15 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Input using Scanner Defined in the java.util package. Provides a way to read and process input. Substitutes for existing combinations of classes, e.g. BufferedReader with StringTokenizer or String.split. Parses input and returns typed values.

16 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Example using Scanner // Scan a line of text for ints. Scanner tokenizer = new Scanner(line); for(int i = 0; i < dataline.length; i++) { dataline[i] = tokenizer.nextInt(); }

17 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Review A range of new language features and classes are available in Java 5. Typed collections (generics) are probably the most important. The enhanced for loop is useful. Enumerated types improve clarity and type security of code.