ISE 582 Information Technology for Industrial Engineering Instructor

  • Slides: 36
Download presentation
ISE 582: Information Technology for Industrial Engineering Instructor: Elaine Chew University of Southern California

ISE 582: Information Technology for Industrial Engineering Instructor: Elaine Chew University of Southern California Department of Industrial and Systems Engineering Lecture 6 JAVA Cup Five: Data Structures Winston & Narasimhan: Chapt 27 -31, 33 2 October 2003 Web Technology for IE

JAVA Cup 5 • File Access – Input file streams – Output file streams

JAVA Cup 5 • File Access – Input file streams – Output file streams • Arrays and Vectors – How to create and access arrays – Expandable vectors • Characters and Strings 2 October 2003 Web Technology for IE 2

File Input Streams • • • Reading files one-byte-at-a-time Taking bigger bites Java’s input-output

File Input Streams • • • Reading files one-byte-at-a-time Taking bigger bites Java’s input-output package Traditional string handling Updating to tokens: number / words 2 October 2003 Web Technology for IE 3

File input streams • A stream is a sequence of values. • To read

File input streams • A stream is a sequence of values. • To read bytes from a file: – File. Input. Stream stream = new File. Input. Stream(“input. data”) – File. Input. Stream stream = new File. Input. Stream(“~username/public_html/ise 582/input. data”); • When you are done, it is good to: – stream. close() • Reads ONE BYTE AT A TIME… 2 October 2003 Web Technology for IE 4

To take bigger bites than bytes • To read characters from your file: –

To take bigger bites than bytes • To read characters from your file: – Input. Stream. Reader reader = new Input. Stream. Reader(stream); • To read lines from your file: – Buffered. Reader buffer = new Buffered. Reader(reader); – buffer. read. Line() stream 2 October 2003 reader Web Technology for IE buffer 5

Input-output package • Notify JAVA that you want to work with input or output

Input-output package • Notify JAVA that you want to work with input or output streams: – import java. io. File. Input. Stream – import java. io. * • In the event of error – use try-catch statements – throw an exception, throws IOException 2 October 2003 Web Technology for IE 6

Traditional Approach Example import java. io. *; public class Demonstrate { public static void

Traditional Approach Example import java. io. *; public class Demonstrate { public static void main(String argv[]) throws IOException { File. Input. Stream stream = new File. Input. Stream(“input. data”); Input. Stream. Reader reader = new Input. Stream. Reader(stream); Buffered. Reader buffer = new Buffered. Reader(reader); String line; while ((line=buffer. read. Line())!=null && !line. equals(“”)) { System. out. println(“Line read: “ + line); } stream. close(); return; }} 2 October 2003 Web Technology for IE 7

String Methods • line. trim() – removes white space • line. index. Of(“ “)

String Methods • line. trim() – removes white space • line. index. Of(“ “) – index of first occurrence, starts from 0 • line. substring(2) – returns rest of line after index 2 • line. substring(0, 1) • Integer. parse. Int(“ 4”) – converts string to integer 2 October 2003 Web Technology for IE 8

Example Continued line = line. trim(); int next. Space = line. index. Of(" ");

Example Continued line = line. trim(); int next. Space = line. index. Of(" "); int x = Integer. parse. Int(line. substring(0, next. Space)); line = line. substring(next. Space). trim(); next. Space=line. index. Of(" "); int y = Integer. parse. Int(line. substring(0, next. Space)); line = line. substring(next. Space). trim(); int z = Integer. parse. Int(line); System. out. println("Numbers read: " + x + ", " + y + ", " + z); 2 October 2003 Web Technology for IE 9

The Token Approach • The stream tokenizer: – Stream. Tokenizer tokens = new Stream.

The Token Approach • The stream tokenizer: – Stream. Tokenizer tokens = new Stream. Tokenizer(reader); – Do away with the Buffered. Reader – Divides character sequences into tokens, delimited by white space • Token Methods: – – tokens. next. Token() assigns value to nval, tokens. nval always a double, if need recasting: (int) tokens. nval end of token string indicated by TT_EOF: tokens. TT_EOF 2 October 2003 Web Technology for IE 10

Token Example import java. io. *; public class Demonstrate { public static void main

Token Example import java. io. *; public class Demonstrate { public static void main (String argv[]) throws IOException { File. Input. Stream stream = new File. Input. Stream("input. data"); Input. Stream. Reader reader = new Input. Stream. Reader(stream); Stream. Tokenizer tokens = new Stream. Tokenizer(reader); while (tokens. next. Token()!= tokens. TT_EOF) { int x = (int) tokens. nval; tokens. next. Token(); int y = (int) tokens. nval; tokens. next. Token(); int z = (int) tokens. nval; Movie m = new Movie(x, y, z); System. out. println("Rating: " + m. rating()); } stream. close(); }} 2 October 2003 Web Technology for IE 11

Words vs. Numbers • If the token is a number – next. Token returns

Words vs. Numbers • If the token is a number – next. Token returns a TT_NUMBER instance – the number is assigned to nval • If the token is a word – next. Token returns a TT_WORD instance – the number is assigned to sval 2 October 2003 Web Technology for IE 12

Word / Number Example int next=0; while ((next=tokens. next. Token())!= tokens. TT_EOF) { switch

Word / Number Example int next=0; while ((next=tokens. next. Token())!= tokens. TT_EOF) { switch (next) { case tokens. TT_WORD: break; case tokens. TT_NUMBER: int x = (int) tokens. nval; tokens. next. Token(); int y = (int) tokens. nval; tokens. next. Token(); int z = (int) tokens. nval; Movie m = new Movie(x, y, z); System. out. println("Rating: " + m. rating()); break; } } 2 October 2003 Web Technology for IE 13

Arrays and Vectors • • Creating / assigning values to arrays Passing arrays to

Arrays and Vectors • • Creating / assigning values to arrays Passing arrays to methods Command-line arguments Creating vectors Vector methods Vectors as targets Using vector iterators 2 October 2003 Web Technology for IE 14

Creating Arrays • Creating an array – <elt_type> <array_name> [] = new <elt_type> [

Creating Arrays • Creating an array – <elt_type> <array_name> [] = new <elt_type> [ <size> ]; – int durations [] = new int [4]; • What you can do with an array – call / assign an elt: <array_name> [<index>] – find its length: <array_name>. length • Arrays of class instances – <Class> <array_name> [] = new <Class> [ <size> ]; – Movie movies [] = new Movie [4]; 2 October 2003 Web Technology for IE 15

Instance Array Elements • Field-selection operator still works – movies[3]. script = 6 •

Instance Array Elements • Field-selection operator still works – movies[3]. script = 6 • Checking to see if element is assigned – movies[2] == null • Using instance as target for method – movies[1]. rating() 2 October 2003 Web Technology for IE 16

Mixing Creation and Elt Insertion public class Demonstrate { public static void main (String

Mixing Creation and Elt Insertion public class Demonstrate { public static void main (String argv[]) { Movie movies[] = { new Movie(5, 6, 3), new Movie(8, 7, 7), new Movie(7, 2, 2), new Movie(7, 5, 5)}; int sum = 0; for (int counter=0; counter < movies. length; ++counter) { sum += movies[counter]. rating(); } System. out. print("The average rating of the " + movies. length); System. out. println(" movies is " + sum / movies. length); }} 2 October 2003 Web Technology for IE 17

Arrays and Memory • Arrays are reference type variables • Integer arrays contain a

Arrays and Memory • Arrays are reference type variables • Integer arrays contain a length variable and 4 bytes of memory per instance • Class instance arrays contain a length variable and several bytes for the address of each instance 2 October 2003 Web Technology for IE 18

Higher Dimension Arrays • You can easily define arrays of higher dimension – double

Higher Dimension Arrays • You can easily define arrays of higher dimension – double 2 DArray [] [] = new double[2][100]; 2 October 2003 Web Technology for IE 19

Passing an Array to a Method • To designate that a parameter is array

Passing an Array to a Method • To designate that a parameter is array – public static Movie[] read. Data(Movie movies []) throws IOExc. . – public static Movie[] read. Data(Movie[] movies) throws IOExc. . • E. g. : put file-reads into Auxiliary Class • Some ways to define read. Data: – Create array, pass array address to method, return address – Create array, pass array address to method, return nothing – Create array variable, pass filename to method, return array 2 October 2003 Web Technology for IE 20

Command-Line Arguments • The main methods has one parameter – an array of Strings,

Command-Line Arguments • The main methods has one parameter – an array of Strings, argv[] – argv. length is # command-line arguments 2 October 2003 Web Technology for IE 21

Example public class Command { public static void main(String argv[]) { Movie m =

Example public class Command { public static void main(String argv[]) { Movie m = new Movie(Integer. parse. Int(argv[0]), Integer. parse. Int(argv[1]), Integer. parse. Int(argv[2])); System. out. println("The rating is " + m. rating()); } } 2 October 2003 Web Technology for IE 22

Vectors • Vectors vs. Arrays – variable in length – insertions can occur at

Vectors • Vectors vs. Arrays – variable in length – insertions can occur at any point – elements can only be class instances • Provided by Java’s utility package – import java. util. * – Vector v = new Vector(); 2 October 2003 Web Technology for IE 23

Vector Methods • Insertions and deletions – – v. add. Element(m) … adds to

Vector Methods • Insertions and deletions – – v. add. Element(m) … adds to back end of vector v. insert. Element. At(m, 0) … adds to front of vector v. remove. Element. At(0) … removes first element v. set. Element. At(m, 4) … replaces element • Access to elements – v. first. Element() – v. last. Element() – v. element. At(2) 2 October 2003 • Size of Vector – v. size() Web Technology for IE 24

A Little Quiz • Which methods do you need to represent FIFO queues? •

A Little Quiz • Which methods do you need to represent FIFO queues? • Which methods do you need to represent LIFO queues (stacks)? 2 October 2003 Web Technology for IE 25

Vectors as Targets • All vector elements are instances of the Object class •

Vectors as Targets • All vector elements are instances of the Object class • All vector methods work with instances of the Object class • You can work with an element of the vector class by casting the element: – ( (Movie) (v. first. Element()) ). rating() 2 October 2003 Web Technology for IE 26

Example import java. io. *; import java. util. *; public class Auxiliaries 2 {

Example import java. io. *; import java. util. *; public class Auxiliaries 2 { public static Vector read. Data(String file. Name) throws IOException { File. Input. Stream stream = new File. Input. Stream(file. Name); Input. Stream. Reader reader = new Input. Stream. Reader(stream); Stream. Tokenizer tokens = new Stream. Tokenizer(reader); Vector v = new Vector(); while (tokens. next. Token() != tokens. TT_EOF) { int x = (int) tokens. nval; tokens. next. Token(); int y = (int) tokens. nval; tokens. next. Token(); int z = (int) tokens. nval; v. add. Element(new Movie(x, y, z)); } stream. close(); return v; }} 2 October 2003 Web Technology for IE 27

Iterators • An iterator maintains a pointer to a place on the parent vector

Iterators • An iterator maintains a pointer to a place on the parent vector • To create an iterator – Iterator i = v. iterator() • Some Iterator methods – i. next() … returns element, advances pointer – i. has. Next() 2 October 2003 Web Technology for IE 28

Example import java. io. *; import java. util. *; public class Demonstrate { public

Example import java. io. *; import java. util. *; public class Demonstrate { public static void main(String argv[]) throws IOException { Vector main. Vector = Auxiliaries 2. read. Data("input. data"); int size = main. Vector. size(); for ( Iterator i = main. Vector. iterator(); i. has. Next(); ) { System. out. println(( (Movie) i. next() ). rating()); } } } 2 October 2003 Web Technology for IE 29

Working with Char & Strings • What you can do with Strings • Specifying

Working with Char & Strings • What you can do with Strings • Specifying delimiters in File-reads 2 October 2003 Web Technology for IE 30

The String s • • s. length() … compare this to arrays + concatenates

The String s • • s. length() … compare this to arrays + concatenates two strings s. char. At(0) extracts first character s. char. At(0)==‘M’ … note single quotes switch(s) … use in switch statements char c = s. char. At(0); default character is ‘u 000’ 2 October 2003 Web Technology for IE 31

Example (excerpt) while (tokens. next. Token() != tokens. TT_EOF) { String code. String =

Example (excerpt) while (tokens. next. Token() != tokens. TT_EOF) { String code. String = tokens. sval; tokens. next. Token(); int x = (int) tokens. nval; tokens. next. Token(); int y = (int) tokens. nval; tokens. next. Token(); int z = (int) tokens. nval; switch (code. String. char. At(0)) { case 'M': v. add. Element(new Movie(x, y, z)); break; case 'S': v. add. Element(new Symphony(x, y, z)); break; } } 2 October 2003 Web Technology for IE 32

Specifying Delimiters • To advise the tokens tokenizer to use double quotation marks to

Specifying Delimiters • To advise the tokens tokenizer to use double quotation marks to delimit strings: – tokens. quote. Char((int) ‘”’) • To tell the tokenizer to recognize carriage returns: – tokens. eol. Is. Significant(true); 2 October 2003 Web Technology for IE 33

Example (excerpt) tokens. quote. Char((int) '"'); tokens. eol. Is. Significant(true); Vector v = new

Example (excerpt) tokens. quote. Char((int) '"'); tokens. eol. Is. Significant(true); Vector v = new Vector(); while (tokens. next. Token() != tokens. TT_EOF) { String name. String = tokens. sval; tokens. next. Token(); int x = (int) tokens. nval; tokens. next. Token(); int y = (int) tokens. nval; tokens. next. Token(); int z = (int) tokens. nval; Movie m = new Movie(x, y, z); m. title = name. String; if (tokens. next. Token() == tokens. TT_EOL) {} else { m. poster = tokens. sval; tokens. next. Token(); } v. add. Element(m); } 2 October 2003 Web Technology for IE 34

Working with O/p File Streams • To connect to output file – File. Output.

Working with O/p File Streams • To connect to output file – File. Output. Stream stream = new File. Output. Stream(“output. data”); • To write more than 1 -byte-at-a-time – Print. Writer writer = new Print. Writer(stream); • It’s good to flush out buffered characters – writer. flush() • Close the file – stream. close() 2 October 2003 Web Technology for IE 35

Example (part) import java. io. *; import java. util. *; public class Demonstrate {

Example (part) import java. io. *; import java. util. *; public class Demonstrate { public static void main(String argv[]) throws IOException { File. Output. Stream stream = new File. Output. Stream("output. data"); Print. Writer writer = new Print. Writer(stream); Vector main. Vector = Auxiliaries 4. read. Data("input 3. data"); int size = main. Vector. size(); for (Iterator i = main. Vector. iterator(); i. has. Next(); ) { Movie m = (Movie) i. next(); m. write. To. File(writer); writer. println(m. rating()); } writer. flush(); stream. close(); System. out. println("File written"); }} 2 October 2003 Web Technology for IE 36