Docsity

Java Programming: Lecture 1 - Data Structures with Java - Prof. Jainendra Navlakha, Study notes of Computer Science

Partial preview of the text

Download Java Programming: Lecture 1 - Data Structures with Java - Prof. Jainendra Navlakha and more Study notes Computer Science in PDF only on Docsity!

COT 3530: Data Structures

Giri NarasimhanECS 389; Phone: x

[email protected]

www.cs.fiu.edu/~giri/teach/3530Spring04.html

01/13/

Lecture 1

Evaluation

•^

Midterm & Final Exams

-^

Programming Assignments

-^

Class Participation

01/13/

Lecture 1

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 usefulcapabilities, such as graphical user interface(GUI) widgets. The Java API is grouped intolibraries of related classes and interfaces;these libraries are known as

packages.

01/13/

Lecture 1

How Can Java be Used?

-^

Write software on one platform and run it onanother.–

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-waypagers, and other consumer devices.

01/13/

Lecture 1

JCreator Files

-^

Workspace file–

Hello.jcw

-^

Project file–

Hello.jcp

-^

Source Code–

Hello.java

-^

List of source files–

src_hello.txt

-^

Compiled code (class file)–

Hello.class

-^

A workspace maycontain multipleprojects

-^

A project may containmultiple java sourcefiles

01/13/

Lecture 1

Simple Java Hello

  • 1

Hello.java public

class

Hello

public

static

void

main(String[]

args)

System.out.println("Hello,

COP

01/13/

Lecture 1

Figure 1.2 The eight primitive types in Java

Data Structures & Problem Solving using JAVA/2E

Mark Allen Weiss

© 2002 Addison Wesley

01/13/

Lecture 1

Primitive Types

•^

8 primitive types: byte

, short

, int

, long

, float

, double

, char

boolean

-^

Non-primitive types are all reference types

reference types are simply pointersassignments to reference typesdeclaring an object

creating an object

garbage collectionreference types as parameters of methodscasting

•^

String

type: concatenation, length, comparison, sub-string,

conversion

01/13/

Lecture 1

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/

Lecture 1

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 ] );} // 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" );} System.out.println( "Done reading" );return resize( array, itemsRead );}

Figure 2.6, 2.7, page 42-

Implementing

arrays ofStrings

01/13/

Lecture 1

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 ) );} // Read an unlimited number of String; return an ArrayListpublic 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" );return array;} }

Figure 2.8, page 44

Same

program, butusing class

ArrayList

01/13/

Lecture 1

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 codeeasier 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/

Lecture 1

Figure 2.13 Common standard checked exceptions

Data Structures & Problem Solving using JAVA/2E

Mark Allen Weiss

© 2002 Addison Wesley

01/13/

Lecture 1

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 ofjava.io.BufferedReader

and

java.util.StringTokenizer

respectively.