Barb Ericson Georgia Institute of Technology June 2006

Slides:



Advertisements
Similar presentations
ManipulatingPictures-Mod6-part21 Manipulating Pictures, Arrays, and Loops part 2 Barb Ericson Georgia Institute of Technology.
Advertisements

Intro-Sound-part31 Introduction to Processing Digital Sounds part 3 Barb Ericson Georgia Institute of Technology Oct 2009.
Georgia Institute of Technology Movies part 3 Barb Ericson Georgia Institute of Technology April 2006.
Georgia Institute of Technology Manipulating Pictures, Arrays, and Loops part 1.
15-Jul-15 Generics. ArrayList s and arrays A ArrayList is like an array of Object s, but... Arrays use [ ] syntax; ArrayList s use object syntax An ArrayList.
Georgia Institute of Technology Making Text for the Web part 4 Barb Ericson Georgia Institute of Technology March 2006.
CSE373 Optional Section Java Collections 11/12/2013 Luyi Lu.
Intro-Sound-part21 Introduction to Processing Digital Sounds part 2 Barb Ericson Georgia Institute of Technology Oct 2009.
GENERIC COLLECTIONS. Type-Wrapper Classes  Each primitive type has a corresponding type- wrapper class (in package java.lang).  These classes are called.
Georgia Institute of Technology Movies part 5 Barb Ericson Georgia Institute of Technology April 2006.
Some Other Collections: Bags, Sets, Queues and Maps COMP T2 Lecture 4 School of Engineering and Computer Science, Victoria University of Wellington.
Java 5 Part 1 CSE301 University of Sunderland Harry Erwin, PhD.
Georgia Institute of Technology Manipulating Pictures, Arrays, and Loops Barb Ericson Georgia Institute of Technology August 2005.
TOPIC 6 MODIFYING PICTURES USING LOOPS 1 Notes adapted from Introduction to Computing and Programming with Java: A Multimedia Approach by M. Guzdial and.
ManipulatingPictures-Mod6-part61 Manipulating Pictures, Arrays, and Loops: Eliminating color, Inversion, grey scale and adjusting for luminance Barb Ericson.
Java 1.5 The New Java Mike Orsega Central Carolina CC.
CPSC1301 Computer Science 1 Chapter 8 Introduction to Processing Digital Sounds part 3.
CPSC1301 Computer Science 1 Chapter 4 Manipulating Pictures, Arrays, and Loops part 5.
Georgia Institute of Technology What is new in Java 5.0 (1.5)? Barb Ericson Georgia Institute of Technology June 2006.
Recitation 5 Enums and The Java Collections classes/interfaces 1.
Some Other Collections: Bags, Sets, Queues and Maps COMP T2 Lecture 4 School of Engineering and Computer Science, Victoria University of Wellington.
SourceAnatomy1 Java Source Anatomy Barb Ericson Georgia Institute of Technology July 2008.
Intro-Sound-Mod10-part31 Introduction to Processing Digital Sounds part 3 while loop, tracing, for loop, parameters Barb Ericson Georgia Institute of Technology.
ManipulatingPictures-part31 Manipulating Pictures, Arrays, and Loops part 3 Barb Ericson Georgia Institute of Technology Nov 2009.
04-ManipulatingPictures-part21 Manipulating Pictures, Arrays, and Loops part 2 Barb Ericson Georgia Institute of Technology June 2008.
Barbara Ericson Georgia Tech Sept 2005
CMSC 202 ArrayList Aug 9, 2007.
Sixth Lecture ArrayList Abstract Class and Interface
Lecture 20: Wrapper Classes and More Loops
Array Lists and the Color Sensor
Manipulating Pictures, Arrays, and Loops part 2
Topic 6 Modifying Pictures Using Loops
Fundamentals of Java: AP Computer Science Essentials, 4th Edition
Software Development Java Collections
Barb Ericson Georgia Institute of Technology Dec 2009
Manipulating Pictures, Arrays, and Loops part 3
Week 2: 10/1-10/5 Monday Tuesday Wednesday Thursday Friday
Manipulating Pictures, Arrays, and Loops part 2
Manipulating Pictures, Arrays, and Loops
Barb Ericson Georgia Institute of Technology August 2005
Introduction to Processing Digital Sounds part 2
Manipulating Pictures, Arrays, and Loops part 5
Generics 27-Nov-18.
Words exercise Write code to read a file and display its words in reverse order. A solution that uses an array: String[] allWords = new String[1000]; int.
Arrays versus ArrayList
CMSC 202 ArrayList Aug 9, 2007.
Processing Sound Ranges part 1
Arrays and Collections
Manipulating Pictures, Arrays, and Loops part 3
CMSC 202 ArrayList Aug 9, 2007.
CSE 113 A February , 2009.
Introduction to Processing Digital Sounds part 3
int [] scores = new int [10];
Manipulating Pictures, Arrays, and Loops
Manipulating Pictures, Arrays, and Loops
Georgia Institute of Technology
Manipulating Pictures, Arrays, and Loops
ArrayLists 22-Feb-19.
February , 2009 CSE 113 B.
Introduction to Processing Digital Sounds
Manipulating Pictures, Arrays, and Loops part 6
Manipulating Pictures, Arrays, and Loops
slides created by Alyssa Harding
Creating and Modifying Text part 3
Java 5 New Features 1-May-19.
Review: libraries and packages
Generics 2-May-19.
Barb Ericson Georgia Institute of Technology April 2006
Manipulating Pictures, Arrays, and Loops part 6
Presentation transcript:

Barb Ericson Georgia Institute of Technology June 2006 What is new in Java 5.0 (1.5)? Barb Ericson Georgia Institute of Technology June 2006 Georgia Institute of Technology

Georgia Institute of Technology Learning Goals Understand the new features in Java 5.0 For-each loop Generics Automatic Boxing and Unboxing New collection interfaces Not covered on exam Enumerated Types Static Imports Formatted Input and Output For more information see http://apcentral.collegeboard.com/members/article/1,3046,151-165-0-49154,00.html Georgia Institute of Technology

Georgia Institute of Technology For Each Loop Used to loop through all items of a collection (Array, List, Map, Set, etc) Syntax for (Type varName : collectionName) Example for (String name : nameList) Each time through the loop the varName will refer to a different item in the collection until all the items in the collection have been processed Use whenever you want to process each item in a collection But not when you might want to remove an item from the collection in the loop Georgia Institute of Technology

Before the For-Each Loop /** * Method to set all the sample values to * the maximum positive * value if they were positive (including 0) * and the minimum * negative value if they were negative. */ public void forceToExtremes() { SoundSample[] sampleArray = this.getSamples(); SoundSample sample = null; // loop through the sample values for (int i = 0; i < sampleArray.length; i++) // get the current sample sample = sampleArray[i]; /* if the value was >= 0 set to the * maximum positive value */ if (sample.getValue() >= 0) sample.setValue(32767); /* else (must be less than 0) so set it to * the highest negative value else sample.setValue(-32768); } Georgia Institute of Technology

Using for-each instead Replace the following SoundSample sample = null; // loop through the sample values for (int i = 0; i < sampleArray.length; i++) { // get the current sample sample = sampleArray[i]; With for (SoundSample sample : sampleArray) Georgia Institute of Technology

Georgia Institute of Technology Exercise Copy the code for forceToExtremes and put it in Sound.java in the bookClasses directory Test it with the following in the main: String file = FileChooser.pickAFile(); Sound s = new Sound(file); s.explore(); s.play(); s.forceToExtremes() Change the code in forceToExtremes to use a for-each loop instead and test again Georgia Institute of Technology

Georgia Institute of Technology Exercise Modify a method in the Picture class that modifies all pixels in a picture to use a for-each loop public void negate() { Pixel[] pixelArray = this.getPixels(); Pixel pixel = null; int redValue, blueValue, greenValue = 0; // loop through all the pixels for (int i = 0; i < pixelArray.length; i++) // get the current pixel pixel = pixelArray[i]; // get the current red, green, and blue values redValue = pixel.getRed(); greenValue = pixel.getGreen(); blueValue = pixel.getBlue(); // set the pixel's color to the new color pixel.setColor(new Color(255 - redValue, 255 - greenValue, 255 - blueValue)); } Georgia Institute of Technology

Georgia Institute of Technology Generics Allow you to specify the type of objects in a collection, both when you declare it and create it Eliminates the need to downcast Syntax: CollectionType<Type> name = new CollectionType<Type>; List<String> nameList = new ArrayList<String>(); CollectionType<Type,Type> name = new CollectionType<Type,Type>; Map<String,String> phoneMap = new HashMap<String,String>(); Georgia Institute of Technology

Georgia Institute of Technology Without Generics You have to cast back to the original class when you get an object back from a collection Downcast from Object name = (String) iterator.next(); With generics you don't have to downcast Just give the type on declarations and creation name = iterator.next(); Georgia Institute of Technology

Georgia Institute of Technology Exercise Modify the SlideShow class in examples/SlideShow-List Use generics instead of casting Specify the type on all declarations of the List Including parameters Specify the type on the creating of the List Remove the cast to Picture when you show the pictures or get the picture You can use a for-each loop instead of an iterator when showing the pictures Georgia Institute of Technology

Georgia Institute of Technology Exercise Modify the PhoneBook class in examples/PhoneBook to use generics for the phoneMap Copy the code to change first Then comment out the old code Add new code for generics Be sure to specify the type when you declare the phoneMap and when you create it Remove the downcast on getting items from the map Georgia Institute of Technology

Automatic Boxing and Unboxing Private types are not objects: int, double, char, boolean They can not be added to collections You can have arrays of primitive types But, they can be wrapped and added to collections (Integer, Double, Character, Boolean) And then you have to unwrap to get the value With automatic boxing and unboxing you don't have to worry about the wrapping and unwrapping It is done for you Georgia Institute of Technology

Example of Boxing and Unboxing Add a primitive type to a collection Old way: wrapping the primitive type List intList = new ArrayList(); intList.add(new Integer(5)); intList.add(new Integer(6)); Integer integerValue = (Integer) intList.get(0); int value = integerValue.intValue(); New: auto wrapping (boxing) intList.add(5); intList.add(6); int value = (Integer) intList.get(0); Georgia Institute of Technology

Georgia Institute of Technology New Queue Interface Java 5.0 adds a Queue interface Implemented by the LinkedList class Implemented by the PriorityQueue class There are no more special AP interfaces for AB data structures Only using the Java interfaces and classes Stack (class), Queue (interface), PriorityQueue (class) Using new method names for queues peek, add, and remove Be careful not to use methods that are valid in Java for Stacks, Queues, and PriorityQueues but not normally part of these data structures. Georgia Institute of Technology

Georgia Institute of Technology Not Covered on Exam Enumerated Types Old: declare constants using numbers public static final int MALE = 0; public static final int FEMALE = 1; New: use enumerated types public enum Gender { MALE, FEMALE }; Static Imports Old: Use class name to access constants Color.BLACK New: import static java.awt.*; BLACK Formatted Input and Output Use java.io.Scanner class to read from a file or System.in See System.out.printf() to format output Georgia Institute of Technology

Georgia Institute of Technology Summary Java 5.0 adds many new features Tested on the Exam For-each loop for (Type varName : collectionName) Generics Give the type when you declare and create collection objects List<String> nameList = new ArrayList<String>(); Eliminates the need to downcast from Object Auto-boxing and unboxing Don't need to explicitly add primitive values to wrapper classes and get the value back out Georgia Institute of Technology