introduction to java - clark science center · introduction to java csc212 lecture 7 d. thiebaut,...

45
Introduction to Java CSC212 Lecture 7 D. Thiebaut, Fall 2014

Upload: others

Post on 16-Jul-2020

12 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Introduction to Java - Clark Science Center · Introduction to Java CSC212 Lecture 7 D. Thiebaut, Fall 2014. More on

Introduction to JavaCSC212 Lecture 7

D. Thiebaut, Fall 2014

Page 2: Introduction to Java - Clark Science Center · Introduction to Java CSC212 Lecture 7 D. Thiebaut, Fall 2014. More on

More on Exceptions

Page 3: Introduction to Java - Clark Science Center · Introduction to Java CSC212 Lecture 7 D. Thiebaut, Fall 2014. More on

A Simple Classimport java.util.Scanner;!!public class Bomb {! ! public static void main(String[] args) {! Scanner input = new Scanner( System.in );! ! while ( true ) {! System.out.println( "Please enter 2 integers"! +" (-1 to stop): ");! int n1 = input.nextInt();! if ( n1==-1 ) ! break;! int n2 = input.nextInt();! System.out.println( n1 + " / " + n2 + " is " + (n1/n2) );! }! }!}

Page 4: Introduction to Java - Clark Science Center · Introduction to Java CSC212 Lecture 7 D. Thiebaut, Fall 2014. More on

What could go wrong?import java.util.Scanner;!!public class Bomb {! ! public static void main(String[] args) {! Scanner input = new Scanner( System.in );! ! while ( true ) {! System.out.println( "Please enter 2 integers"! +" (-1 to stop): ");! int n1 = input.nextInt();! if ( n1==-1 ) ! break;! int n2 = input.nextInt();! System.out.println( n1 + " / " + n2 + " is " + (n1/n2) );! }! }!}

Page 5: Introduction to Java - Clark Science Center · Introduction to Java CSC212 Lecture 7 D. Thiebaut, Fall 2014. More on

First Place to Crash:import java.util.Scanner;!!public class Bomb {! ! public static void main(String[] args) {! Scanner input = new Scanner( System.in );! ! while ( true ) {! System.out.println( "Please enter 2 integers"! +" (-1 to stop): ");! int n1 = input.nextInt();! if ( n1==-1 ) ! break;! int n2 = input.nextInt();! System.out.println( n1 + " / " + n2 + " is " + (n1/n2) );! }! }!}

Page 6: Introduction to Java - Clark Science Center · Introduction to Java CSC212 Lecture 7 D. Thiebaut, Fall 2014. More on

Fix #1while ( true ) {! System.out.println( "Please enter 2 integers (-1 to stop): ");! int n1 = input.nextInt();! int n2 = input.nextInt();! if ( n1==-1 ) ! break;! try {! System.out.println( n1 + " / " + n2 + " is " + (n1/n2) );! }! catch ( ArithmeticException e ) {! System.out.println("Exception: " + e.getMessage() + ! "\nReenter numbers." );! }!}!

Page 7: Introduction to Java - Clark Science Center · Introduction to Java CSC212 Lecture 7 D. Thiebaut, Fall 2014. More on

What Else Could Go Wrong?import java.util.Scanner;!!public class Bomb {! ! public static void main(String[] args) {! Scanner input = new Scanner( System.in );! ! while ( true ) {! System.out.println( "Please enter 2 integers"! +" (-1 to stop): ");! int n1 = input.nextInt();! if ( n1==-1 ) ! break;! int n2 = input.nextInt();! System.out.println( n1 + " / " + n2 + " is " + (n1/n2) );! }! }!}

Page 8: Introduction to Java - Clark Science Center · Introduction to Java CSC212 Lecture 7 D. Thiebaut, Fall 2014. More on

Badly Formatted Ints:import java.util.Scanner;!!public class Bomb {! ! public static void main(String[] args) {! Scanner input = new Scanner( System.in );! ! while ( true ) {! System.out.println( "Please enter 2 integers"! +" (-1 to stop): ");! int n1 = input.nextInt();! if ( n1==-1 ) ! break;! int n2 = input.nextInt();! System.out.println( n1 + " / " + n2 + " is " + (n1/n2) );! }! }!}

Page 9: Introduction to Java - Clark Science Center · Introduction to Java CSC212 Lecture 7 D. Thiebaut, Fall 2014. More on

Fix #2import java.util.Scanner;!import java.util.InputMismatchException;!!public class Bomb {! ! public static void main(String[] args) {! Scanner input = new Scanner( System.in );! ! while ( true ) {! System.out.println( "Please enter 2 integers (-1 to stop): ");! int n1, n2;! try {! n1 = input.nextInt();! if ( n1==-1 ) break;! n2 = input.nextInt();! } catch ( InputMismatchException e ) {! // add your code here...! }! ! //... some code missing...

Page 10: Introduction to Java - Clark Science Center · Introduction to Java CSC212 Lecture 7 D. Thiebaut, Fall 2014. More on

Fix #2import java.util.Scanner;!import java.util.InputMismatchException;!!public class Bomb {! ! public static void main(String[] args) {! Scanner input = new Scanner( System.in );! ! while ( true ) {! System.out.println( "Please enter 2 integers (-1 to stop): ");! int n1, n2;! try {! n1 = input.nextInt();! if ( n1==-1 ) break;! n2 = input.nextInt();! } catch ( InputMismatchException e ) {! // add your code here…!! ! ! ! ! ! continue;! }! ! //... some code missing...

Both Input!

Statements!Protected!

Page 11: Introduction to Java - Clark Science Center · Introduction to Java CSC212 Lecture 7 D. Thiebaut, Fall 2014. More on

But There's a Problem… !

Infinite Loop… !

It Has to Do With The Input Buffer…

Page 12: Introduction to Java - Clark Science Center · Introduction to Java CSC212 Lecture 7 D. Thiebaut, Fall 2014. More on

1 2 3 _ 3 7 \n

input.nextInt() —-> 123

input.nextInt() —-> 37

Input Buffer

Page 13: Introduction to Java - Clark Science Center · Introduction to Java CSC212 Lecture 7 D. Thiebaut, Fall 2014. More on

1 2 A _ 3 7 \n

input.nextInt() —->

input.nextInt() —->

Input Buffer

Page 14: Introduction to Java - Clark Science Center · Introduction to Java CSC212 Lecture 7 D. Thiebaut, Fall 2014. More on

Solution: input.nextLine();

1 2 A _ 3 7 \n Input Buffer

Page 15: Introduction to Java - Clark Science Center · Introduction to Java CSC212 Lecture 7 D. Thiebaut, Fall 2014. More on

import java.util.Scanner;!import java.util.InputMismatchException;!!public class Bomb {! ! public static void main(String[] args) {! Scanner input = new Scanner( System.in );! ! while ( true ) {! System.out.println( "Please enter 2 integers (-1 to stop): ");! int n1, n2;! try {! n1 = input.nextInt();! if ( n1==-1 ) break;! n2 = input.nextInt();! } catch ( InputMismatchException e ) {! // add your code here…!! ! ! ! ! ! input.nextLine();!! ! ! ! ! ! continue;! }! ! //... some code missing...

Get rid!of the rest of!the unread!information!

Fix #2 Fixed

Page 16: Introduction to Java - Clark Science Center · Introduction to Java CSC212 Lecture 7 D. Thiebaut, Fall 2014. More on

Reading Files… (and dealing with more exceptions)

Page 17: Introduction to Java - Clark Science Center · Introduction to Java CSC212 Lecture 7 D. Thiebaut, Fall 2014. More on

import java.io.File;!import java.util.Scanner;!!public class Lab5ReadFile {! public static void main( String[] args ) {! ! String fileName;! int n1, n2;! Scanner inFile = null;! ! // if user forgot the enter file name on command line, remind her! if ( args.length == 0 ) {! System.out.println( "Syntax: java progName textFileName" );! System.out.println( " where textFileName is a name of a data file.");! return;! }! ! // set file name to first argument on command line! fileName = args[0]; ! inFile = new Scanner( new File( fileName ) );! ! // read the file! while ( inFile.hasNext() ) {! n1 = inFile.nextInt();! n2 = inFile.nextInt();! System.out.println( n1 + " / " + n2 + " is " + n1/n2 );! }! }!}

Example

Page 18: Introduction to Java - Clark Science Center · Introduction to Java CSC212 Lecture 7 D. Thiebaut, Fall 2014. More on

When we compile the program, the compiler stops

and requires the use of a try/catch statement!

$: javac Lab5ReadFile.java Lab5ReadFile.java:23: error: unreported exception FileNotFoundException; must be caught or declared to be thrown inFile = new Scanner( new File( fileName ) ); ^ 1 error

Page 19: Introduction to Java - Clark Science Center · Introduction to Java CSC212 Lecture 7 D. Thiebaut, Fall 2014. More on

Search the Error on the Web

Page 20: Introduction to Java - Clark Science Center · Introduction to Java CSC212 Lecture 7 D. Thiebaut, Fall 2014. More on

import java.io.File;!import java.util.Scanner;!import java.io.FileNotFoundException;!!public class Lab5ReadFile {! public static void main( String[] args ) {! ! String fileName;! int n1, n2;! Scanner inFile = null;! …! ! // set file name to first argument on command line! fileName = args[0]; !! try { ! inFile = new Scanner( new File( fileName ) );! }! catch ( FileNotFoundException e ) {! System.out.println( "File not found." );! return;! }! ! …! }!}

Page 21: Introduction to Java - Clark Science Center · Introduction to Java CSC212 Lecture 7 D. Thiebaut, Fall 2014. More on

(Lab5ReadFile.java)

Page 22: Introduction to Java - Clark Science Center · Introduction to Java CSC212 Lecture 7 D. Thiebaut, Fall 2014. More on

Throwing Exceptions

Page 23: Introduction to Java - Clark Science Center · Introduction to Java CSC212 Lecture 7 D. Thiebaut, Fall 2014. More on

Why?

• Sometimes the method in which the exception occurs is too low level to be able to fix the problem.

Page 24: Introduction to Java - Clark Science Center · Introduction to Java CSC212 Lecture 7 D. Thiebaut, Fall 2014. More on

private static int[] get2Ints( Scanner inFile ) {! int n1 = inFile.nextInt(); ! int n2 = inFile.nextInt();! return new int[] { n1, n2 };! }

Page 25: Introduction to Java - Clark Science Center · Introduction to Java CSC212 Lecture 7 D. Thiebaut, Fall 2014. More on

private static int[] get2Ints( Scanner inFile ) !! ! ! ! ! ! ! ! ! ! throws InputMismatchException{! int n1 = inFile.nextInt(); ! int n2 = inFile.nextInt();! return new int[] { n1, n2 };! }

while (inFile.hasNext()) {! int[] a;! try {! a = get2Ints( inFile );! } catch ( InputMismatchException e ) {! System.err.println( "Wrong number format. Please reenter!" );! inFile.nextLine();! continue; }! System.out.println( a[0] + " / " + a[1] + " is " + a[0] / a[1] );! }

Page 26: Introduction to Java - Clark Science Center · Introduction to Java CSC212 Lecture 7 D. Thiebaut, Fall 2014. More on

private static int[] get2Ints( Scanner inFile ) !! ! ! ! ! ! ! ! ! ! throws InputMismatchException{! int n1 = inFile.nextInt(); ! int n2 = inFile.nextInt();! return new int[] { n1, n2 };! }

while (inFile.hasNext()) {! int[] a;! try {! a = get2Ints( inFile );! } catch ( InputMismatchException e ) {! System.err.println( "Wrong number format. Please reenter!" );! inFile.nextLine();! continue; }! System.out.println( a[0] + " / " + a[1] + " is " + a[0] / a[1] );! }

Page 27: Introduction to Java - Clark Science Center · Introduction to Java CSC212 Lecture 7 D. Thiebaut, Fall 2014. More on

We Create Our Own Exception Object

Page 28: Introduction to Java - Clark Science Center · Introduction to Java CSC212 Lecture 7 D. Thiebaut, Fall 2014. More on

class MyException extends Exception {! MyException( String message ) {! super( message );! }!}

Page 29: Introduction to Java - Clark Science Center · Introduction to Java CSC212 Lecture 7 D. Thiebaut, Fall 2014. More on

private static int[] get2Ints( Scanner inFile ) !! ! ! throws InputMismatchException, MyException {! int n1 = inFile.nextInt(); ! int n2 = inFile.nextInt();! if ( n2 == 0 ) {! throw new MyException( "Invalid 0\n" );! }! return new int[] { n1, n2 };!}!

Page 30: Introduction to Java - Clark Science Center · Introduction to Java CSC212 Lecture 7 D. Thiebaut, Fall 2014. More on

(Lab5ReadFileThrow3.java)

Page 31: Introduction to Java - Clark Science Center · Introduction to Java CSC212 Lecture 7 D. Thiebaut, Fall 2014. More on

DATA STRUCTURES!

Page 32: Introduction to Java - Clark Science Center · Introduction to Java CSC212 Lecture 7 D. Thiebaut, Fall 2014. More on

–en.wikipedia.org/wiki/Data_structureWikipedia

“ In computer science, a data structure is a particular way of organizing data in

a computer so that it can be used efficiently. Different kinds of data

structures are suited to different kinds of applications, and some are highly

specialized to specific tasks.”

Page 33: Introduction to Java - Clark Science Center · Introduction to Java CSC212 Lecture 7 D. Thiebaut, Fall 2014. More on

Efficiency

• SPEED!

• MEMORY

Page 34: Introduction to Java - Clark Science Center · Introduction to Java CSC212 Lecture 7 D. Thiebaut, Fall 2014. More on

Java’s Vector Data Structure

(Chapter 3)

Page 35: Introduction to Java - Clark Science Center · Introduction to Java CSC212 Lecture 7 D. Thiebaut, Fall 2014. More on

Where Do I Find Information?http://docs.oracle.com/javase/7/docs/api/java/util/Vector.html

Page 36: Introduction to Java - Clark Science Center · Introduction to Java CSC212 Lecture 7 D. Thiebaut, Fall 2014. More on

Some Useful Vector Methods

• Vector(), Vector(initialCapacity)

• add( )

• contains( Object )

• get( index )

• indexOf( Object )

• size()

Page 37: Introduction to Java - Clark Science Center · Introduction to Java CSC212 Lecture 7 D. Thiebaut, Fall 2014. More on

Build a Vector…import java.util.Iterator;import java.util.Vector;!!public class Vector1 {! static public void main( String[] args ) { Vector list = new Vector( ); for ( int i=0; i<10; i++ ) list.add( (Integer) (i * 3) ); … }}

Page 38: Introduction to Java - Clark Science Center · Introduction to Java CSC212 Lecture 7 D. Thiebaut, Fall 2014. More on

Build a Vector…import java.util.Iterator;import java.util.Vector;!!public class Vector1 {! static public void main( String[] args ) { Vector list = new Vector( ); for ( int i=0; i<10; i++ ) list.add( (Integer) (i * 3) ); … }} Type Cast!

Page 39: Introduction to Java - Clark Science Center · Introduction to Java CSC212 Lecture 7 D. Thiebaut, Fall 2014. More on

try { System.out.println( "Element at 4 = " + list.get( 4 ) ); } catch (ArrayIndexOutOfBoundsException e ) { // do nothing }! try { System.out.println( "Element at 20 = " + list.get( 20 ) ); } catch (ArrayIndexOutOfBoundsException e ) { // do nothing }

Access Element At Index

Page 40: Introduction to Java - Clark Science Center · Introduction to Java CSC212 Lecture 7 D. Thiebaut, Fall 2014. More on

for ( int i=0; i<list.size(); i++ ) { int x = list.get( i ); System.out.println( x ); }

Iterating Through Vector

Iterator<Integer> it = list.iterator(); while ( it.hasNext() ) { int x = it.next(); System.out.println( x ); }

Page 41: Introduction to Java - Clark Science Center · Introduction to Java CSC212 Lecture 7 D. Thiebaut, Fall 2014. More on

import java.util.Iterator;import java.util.Vector;!public class Vector1 {! static public void main( String[] args ) { Vector list = new Vector( ); for ( int i=0; i<10; i++ ) list.add( (Integer) (i * 3) ); try { System.out.println( "Element at 4 = " + list.get( 4 ) ); } catch (ArrayIndexOutOfBoundsException e ) { // do nothing }! try { System.out.println( "Element at 20 = " + list.get( 20 ) ); } catch (ArrayIndexOutOfBoundsException e ) { // do nothing } Iterator<Integer> it = list.iterator(); while ( it.hasNext() ) { int x = it.next(); System.out.println( x ); } }}

Page 42: Introduction to Java - Clark Science Center · Introduction to Java CSC212 Lecture 7 D. Thiebaut, Fall 2014. More on
Page 43: Introduction to Java - Clark Science Center · Introduction to Java CSC212 Lecture 7 D. Thiebaut, Fall 2014. More on

import java.util.Iterator;import java.util.Vector;!public class Vector2 { static public void main( String[] args ) { Vector<Integer> list = new Vector<Integer>( ); for ( int i=0; i<10; i++ ) list.add( i * 3 ); try { System.out.println( "Element at 4 = " + list.get( 4 ) ); } catch (ArrayIndexOutOfBoundsException e ) { // do nothing } try { System.out.println( "Element at 20 = " + list.get( 20 ) ); } catch (ArrayIndexOutOfBoundsException e ) { // do nothing } Iterator<Integer> it = list.iterator(); while ( it.hasNext() ) { int x = it.next(); System.out.println( x ); } }}

Page 44: Introduction to Java - Clark Science Center · Introduction to Java CSC212 Lecture 7 D. Thiebaut, Fall 2014. More on
Page 45: Introduction to Java - Clark Science Center · Introduction to Java CSC212 Lecture 7 D. Thiebaut, Fall 2014. More on