DOC PREVIEW
UMD CMSC 132 - Object-Oriented Programming II

This preview shows page 1-2-19-20 out of 20 pages.

Save
View full document
Premium Document
Do you want full access? Go Premium and unlock all 20 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

CMSC 132 Object Oriented Programming II Nelson Padua Perez William Pugh Department of Computer Science University of Maryland College Park Overview Autoboxing Enumerated Types Iterator Interface Enhanced for loop Scanner class Exceptions Streams Autoboxing Unboxing Automatically convert primitive data types Data value Object of matching class Data types classes converted Boolean Byte Double Short Integer Long Float Example See SortValues java Enumerated Types New type of variable with set of fixed values Establishes all possible values by listing them Supports values valueOf name compareTo Can add fields and methods to enums Example public enum Color Black White new enumeration Color myC Color Black for Color c Color values System out println c When to use enums Natural enumerated types days of week phases of the moon seasons Sets where you know all possible values Enumerated Types The following example is from the presentation Taming the Tiger by Joshua Bloch and Neal Gafter that took place at Sun s 2004 Worldwide Java Developer Conference Example public class Card implements Serializable public enum Rank DEUCE THREE FOUR FIVE SIX SEVEN EIGHT NINE TEN JACK QUEEN KING ACE public enum Suit CLUBS DIAMONDS HEARTS SPADES private final Rank rank private final Suit suit private Card Rank rank Suit suit this rank rank this suit suit public Rank rank return rank public Suit suit return suit public String toString return rank of suit Iterator Interface Iterator Common interface for all Collection classes Used to examine all elements in collection Properties Can remove current element during iteration Works for any collection Iterator Interface Interface public interface Iterator boolean hasNext Object next void remove optional called once per next Example usage Iterator i myCollection iterator while i hasNext myCollectionElem x myCollectionElem i next Enhanced For Loop Works for arrays and any class that implements the Iterable interface For loop handles Iterator automatically Test hasNext then get cast next Example 1 Iterating over a String array String roster John Mary Peter Jackie Mark for String student roster System out println student Enhanced For Loop Example 2 ArrayList String roster new ArrayList String roster add John roster add Mary Iterator it roster iterator using an iterator while it hasNext System out println it next for String student roster using for loop System out println student Standard Input Output Standard I O Provided in System class in java lang System in An instance of InputStream System out An instance of PrintStream System err An instance of PrintStream Scanner class Scanner Allow us to read primitive type and strings from the standard input Example See ScannerExample java In the example notice the use of printf Exception Handling Performing action in response to exception Example actions Ignore exception Print error message Request new data Retry action Approaches 1 Exit program 2 Exit method returning error code 3 Throw exception Representing Exceptions Java Exception class hierarchy Two types of exceptions checked unchecked Representing Exceptions Java Exception class hierarchy ClassNotFoundException Exception CloneNotSupportedException IOException AWTException RuntimeException Object Throwable ArithmeticException NullPointerException IndexOutOfBoundsException NoSuchElementException LinkageError VirtualMachoneError Error AWTError Checked Unchecked Unchecked Exceptions Class Error RunTimeException Serious errors not handled by typical program Usually indicate logic errors Example NullPointerException IndexOutOfBoundsException Catching unchecked exceptions is optional Handled by Java Virtual Machine if not caught Checked Exceptions Class Exception except RunTimeException Errors typical program should handle Used for operations prone to error Example IOException ClassNotFoundException Compiler requires catch or declare Catch and handle exception in method OR Declare method can throw exception force calling function to catch or declare exception in turn Example void A throws ExceptionType Exceptions Java Primitives Java primitives Try Forms try block Encloses all statements that may throw exception Throw Actually throw exception Catch Catches exception matching type Code in catch block exception handler Finally Forms finally block Always executed follows try block catch code Exceptions Syntax try try block encloses throws throw new eType1 throw jumps to catch catch eType1 e catch block 1 action run if type match catch eType2 e catch block 2 action run if type match finally final block action always executes Stream Input Output Stream A connection carrying a sequence of data Bytes InputStream OutputStream Characters FileReader PrintWriter From a source to a destination Keyboard File Network Memory Basis for modern I O Using Streams Opening a stream Connects program to external data Location of stream specified at opening Only need to refer to stream Usage 1 import java io 2 Open stream connection 3 Use stream read and or write Catch exceptions if needed 4 Close stream Examples See fileExamples package in lect2 jar


View Full Document

UMD CMSC 132 - Object-Oriented Programming II

Documents in this Course
Notes

Notes

8 pages

Recursion

Recursion

12 pages

Sorting

Sorting

31 pages

HTML

HTML

7 pages

Trees

Trees

19 pages

HTML

HTML

18 pages

Trees

Trees

19 pages

Honors

Honors

19 pages

Lecture 1

Lecture 1

11 pages

Quiz #3

Quiz #3

2 pages

Hashing

Hashing

21 pages

Load more
Loading Unlocking...
Login

Join to view Object-Oriented Programming II and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Object-Oriented Programming II and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?