COT 3530: Data Structures Giri Narasimhan ECS 389; Phone: x3748 giri@cs.fiu.edu www.cs.fiu.edu/~giri/teach/3530spring04.html
Evaluation Midterm & Final Exams Programming Assignments Class Participation 01/13/2004 Lecture 1 2
What is Java? Java programming language object-oriented similar to C++ Java platform Java Virtual Machine (JVM) Application Programming Interface (API) Created and licensed by Sun Microsystems Both compiled and interpreted compiler produces Java Bytecodes bytecodes are executed by Java Virtual Machine programs run slightly slower than native code (such as that produced by C++) 01/13/2004 Lecture 1 3
What is Java? Java Development Kit (JDK) compiler, runtime system, debugger, documentation Java Virtual Machine (JVM) executes Java bytecode programs Java API a large collection of ready-made software components that provide many useful capabilities, such as graphical user interface (GUI) widgets. The Java API is grouped into libraries of related classes and interfaces; these libraries are known as packages. 01/13/2004 Lecture 1 4
How Can Java be Used? Write software on one platform and run it on another. applications Create programs to run within a web browser. applets Develop server-side applications for online forums, stores, polls, processing HTML forms, and more. Write applications for cell phones, two-way pagers, and other consumer devices. 01/13/2004 Lecture 1 5
Programming Environment JDK 1.3.1 JCreator is our editor and Integrated Development Environment (IDE) Filenames case-sensitive Source files:.java Compiled code:.class Project files All java source files must be in same directory 01/13/2004 Lecture 1 6
Workspace file Hello.jcw Project file Hello.jcp Source Code Hello.java List of source files src_hello.txt JCreator Files Compiled code (class file) Hello.class A workspace may contain multiple projects A project may contain multiple java source files 01/13/2004 Lecture 1 7
Simple Java Hello -1 // Hello.java public class Hello public static void main(string[] args) System.out.println("Hello, COP 3337"); 01/13/2004 Lecture 1 8
Simple Java Hello -2 Create the project File / New / Empty project Project name: Hello (Workspace created automatically) Create the source file File / New / Java file File name: Hello Build the project Build / Compile project (F7) Execute the project Build / Execute project (F5) 01/13/2004 Lecture 1 9
Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss 2002 Addison Wesley Figure 1.2 The eight primitive types in Java 01/13/2004 Lecture 1 10
Primitive Types 8 primitive types: byte, short, int, long, float, double, char, boolean Non-primitive types are all reference types reference types are simply pointers assignments to reference types declaring an object creating an object garbage collection reference types as parameters of methods casting String type: concatenation, length, comparison, sub-string, conversion 01/13/2004 Lecture 1 11
Static Declarations Storage allocation for static objects, fields, methods. 2 ways to invoke static fields & methods: using an object or using the class name. 01/13/2004 Lecture 1 12
Arrays Reference types; explicitly created using new statement. Index starts at 0. Arrays have length field. Array assignment array copy. Array copy done using clone() multi-dimensional arrays Dynamic arrays automatic using ArrayList 01/13/2004 Lecture 1 13
import java.io.inputstreamreader; import java.io.bufferedreader; import java.io.ioexception; public class ReadStrings public static void main( String [ ] args ) String [ ] array = getstrings( ); for( int i = 0; i < array.length; i++ ) System.out.println( array[ i ] ); Figure 2.6, 2.7, page 42-43 // Read an unlimited number of String; return a String [ ] public static String [ ] getstrings( ) BufferedReader in = new BufferedReader( new InputStreamReader( System.in ) ); String [ ] array = new String[ 5 ]; int itemsread = 0; String oneline; System.out.println( "Enter any number of strings, one per line; " ); System.out.println( "Terminate with empty line: " ); try while( ( oneline = in.readline( ) )!= null &&!oneline.equals( "" ) ) if( itemsread == array.length ) array = resize( array, array.length * 2 ); array[ itemsread++ ] = oneline; catch( IOException e ) System.out.println( "Unexpected IO Exception has shortened amount read" ); Implementing arrays of Strings System.out.println( "Done reading" ); return resize( array, itemsread ); 01/13/2004 Lecture 1 14
Figure 2.7, page 43 // Resize a String[ ] array; return new array public static String [ ] resize( String [ ] array, int newsize ) String [ ] original = array; int numtocopy = Math.min( original.length, newsize ); array = new String[ newsize ]; for( int i = 0; i < numtocopy; i++ ) array[ i ] = original[ i ]; return array; 01/13/2004 Lecture 1 15
Same program, but using class ArrayList import java.io.inputstreamreader; import java.io.bufferedreader; import java.io.ioexception; import java.util.arraylist; public class ReadStringsWithArrayList public static void main( String [ ] args ) ArrayList array = getstrings( ); for( int i = 0; i < array.size( ); i++ ) System.out.println( array.get( i ) ); Figure 2.8, page 44 // Read an unlimited number of String; return an ArrayList public static ArrayList getstrings( ) BufferedReader in = new BufferedReader( new InputStreamReader( System.in ) ); ArrayList array = new ArrayList( ); String oneline; System.out.println( "Enter any number of strings, one per line; " ); System.out.println( "Terminate with empty line: " ); try while( ( oneline = in.readline( ) )!= null &&!oneline.equals( "" ) ) array.add( oneline ); catch( IOException e ) System.out.println( "Unexpected IO Exception has shortened amount read" ); System.out.println( "Done reading" ); 01/13/2004 return array; Lecture 1 16
Exceptions & Errors An exception is an object that is thrown from the site of an error and can be caught by an appropriate exception handler. Separating the handler from error detection makes the code easier to read and write. Do not use exception as a cheap goto statement. Better to pass it on to calling procedure. More reliable error recovery without simply exiting. User-defined exceptions can be created or thrown. The try region is a guarded region from which errors can be caught by exceptions. Errors are virtual machine problems. OutOfMemoryError, InternalError, UnknownError are examples of errors. Errors are uncrecoverable and should not be caught. 01/13/2004 Lecture 1 17
Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss 2002 Addison Wesley Figure 2.12 Common standard run-time exceptions 01/13/2004 Lecture 1 18
Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss 2002 Addison Wesley Figure 2.13 Common standard checked exceptions 01/13/2004 Lecture 1 19
Input/Output Streams are used for I/O Terminal I/O treated in the same way as File I/O. Predefined streams System.in, System.out, System.err readline and StringTokenizer are useful methods for formatted input; they are part of java.io.bufferedreader and java.util.stringtokenizer respectively. 01/13/2004 Lecture 1 20