1 java b object oriented b powerful b interpreted b portable b platform independent b filename.java...

33
1 Java Java Object Oriented Object Oriented Powerful Powerful Interpreted Interpreted Portable Portable Platform independent Platform independent filename.java filename.java filename.class filename.class (bytecode) (bytecode)

Upload: albert-harvey

Post on 14-Jan-2016

214 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 1 Java b Object Oriented b Powerful b Interpreted b Portable b Platform independent b filename.java b filename.class(bytecode)

1

JavaJava

Object OrientedObject Oriented PowerfulPowerful InterpretedInterpreted PortablePortable Platform independentPlatform independent filename.javafilename.java filename.classfilename.class (bytecode)(bytecode)

Page 2: 1 Java b Object Oriented b Powerful b Interpreted b Portable b Platform independent b filename.java b filename.class(bytecode)

2

JavaJava

Multi-threadedMulti-threaded Easy to work with multimediaEasy to work with multimedia

• imagesimages• animationanimation• audioaudio• videovideo

Graphics capabilities (and still portable)Graphics capabilities (and still portable) Case sensitiveCase sensitive

Page 3: 1 Java b Object Oriented b Powerful b Interpreted b Portable b Platform independent b filename.java b filename.class(bytecode)

3

JavaJava

ApplicationsApplications AppletsApplets

• for the WWWfor the WWW

Page 4: 1 Java b Object Oriented b Powerful b Interpreted b Portable b Platform independent b filename.java b filename.class(bytecode)

4

ApplicationApplication

//Java application//Java application

public class HelloWorld {public class HelloWorld {

public static void main(String args[])public static void main(String args[])

{{

System.out.println(“Hello World!…”);System.out.println(“Hello World!…”);

}}

}}

Page 5: 1 Java b Object Oriented b Powerful b Interpreted b Portable b Platform independent b filename.java b filename.class(bytecode)

5

ApplicationApplication

Public:Public:• If the class is not If the class is not publicpublic, it can be used only by , it can be used only by

other classes in the same other classes in the same packagepackage A A packagepackage is a collection of other packages is a collection of other packages

and classesand classes

Page 6: 1 Java b Object Oriented b Powerful b Interpreted b Portable b Platform independent b filename.java b filename.class(bytecode)

6

ApplicationsApplications

To display a dialog box containing To display a dialog box containing information use the class information use the class JOptionPaneJOptionPane

Class Class JOptionPaneJOptionPane is defined in the is defined in the package called package called javax.swingjavax.swing

e.g.e.g.

import javax.swing.*;import javax.swing.*;

Page 7: 1 Java b Object Oriented b Powerful b Interpreted b Portable b Platform independent b filename.java b filename.class(bytecode)

7

ApplicationApplication

import javax.swing.JOptionPane;import javax.swing.JOptionPane;

public class HelloWorld {public class HelloWorld {

public static void main(String args[])public static void main(String args[])

{{

JOptionPane.showMessageDialog(JOptionPane.showMessageDialog(

null, “Hello\nWorld!…”);null, “Hello\nWorld!…”);

}}

}}

Page 8: 1 Java b Object Oriented b Powerful b Interpreted b Portable b Platform independent b filename.java b filename.class(bytecode)

8

The The concatination operator “+”concatination operator “+”

int i =5;int i =5;

int j = 6;int j = 6;

JOptionPane.showMessageDialog(JOptionPane.showMessageDialog(

null, “i = ” + i + “j = “ + j);null, “i = ” + i + “j = “ + j);

System.out.println(“x = “ + i + “j = “ + j); System.out.println(“x = “ + i + “j = “ + j);

Page 9: 1 Java b Object Oriented b Powerful b Interpreted b Portable b Platform independent b filename.java b filename.class(bytecode)

9

Input from keyboardInput from keyboard

int i;int i;

int j;int j;

i = System.in.read();i = System.in.read();

oror

read a string and convert it to an integerread a string and convert it to an integer

Page 10: 1 Java b Object Oriented b Powerful b Interpreted b Portable b Platform independent b filename.java b filename.class(bytecode)

10

Input from keyboardInput from keyboard

import javax.swing.JOptionPane;import javax.swing.JOptionPane;

public class MyInputpublic class MyInput

{public static void main(String Args[]){{public static void main(String Args[]){

String firstNumber;String firstNumber;

int number1;int number1;

firstNumber = firstNumber = JOptionPane.showInputDialog("Enter an integer: ");JOptionPane.showInputDialog("Enter an integer: ");

number1 = Integer.parseInt(firstNumber);number1 = Integer.parseInt(firstNumber);

System.exit(0);}}System.exit(0);}}

Page 11: 1 Java b Object Oriented b Powerful b Interpreted b Portable b Platform independent b filename.java b filename.class(bytecode)

11

Primitive data typesPrimitive data types

booleanboolean - 8 bits - true/false - 8 bits - true/false char 16 bits ‘\u0000’ to ‘\uFFFF’ (unicode)char 16 bits ‘\u0000’ to ‘\uFFFF’ (unicode) byte - 8 bitsbyte - 8 bits short - 6 bitsshort - 6 bits int - 32 bitsint - 32 bits long - 64 bitslong - 64 bits float - 32 bitsfloat - 32 bits double - 64 bitsdouble - 64 bits

Page 12: 1 Java b Object Oriented b Powerful b Interpreted b Portable b Platform independent b filename.java b filename.class(bytecode)

12

OperatorsOperators

Arithmetic operators: +, -, *, /, %Arithmetic operators: +, -, *, /, % Equality and relational operators: ==, !=, >, Equality and relational operators: ==, !=, >,

<, >=, <=<, >=, <= Increment operator: i++, ++iIncrement operator: i++, ++i Decrement operator: i--, --IDecrement operator: i--, --I Assignment operators: =, +=, -=, *=, /=, %=Assignment operators: =, +=, -=, *=, /=, %=

Page 13: 1 Java b Object Oriented b Powerful b Interpreted b Portable b Platform independent b filename.java b filename.class(bytecode)

13

Control StructuresControl Structures

if(grade >= 16)system.out.println( "passed");

else

system.out.println( "failed");

Page 14: 1 Java b Object Oriented b Powerful b Interpreted b Portable b Platform independent b filename.java b filename.class(bytecode)

14

Control StructuresControl Structures

switch(grade) {case 'A':

gradeA++;

break;

case 'B': gradeB++;

break;

Page 15: 1 Java b Object Oriented b Powerful b Interpreted b Portable b Platform independent b filename.java b filename.class(bytecode)

15

Control StructuresControl Structures

case 'C':gradeC++;

break;

case 'D': gradeD++;

break;

default:showStatus("invalid grade. Enter new grade.");

Page 16: 1 Java b Object Oriented b Powerful b Interpreted b Portable b Platform independent b filename.java b filename.class(bytecode)

16

Repetition StructuresRepetition Structures

Whiledo/while for

Page 17: 1 Java b Object Oriented b Powerful b Interpreted b Portable b Platform independent b filename.java b filename.class(bytecode)

17

Repetition StructuresRepetition Structures

int i = 2;

while(i <=100)

i = i + 1;

Page 18: 1 Java b Object Oriented b Powerful b Interpreted b Portable b Platform independent b filename.java b filename.class(bytecode)

18

Repetition StructuresRepetition Structures

for(int i = 2; i <= 100; i++)

j = j + 1;

Page 19: 1 Java b Object Oriented b Powerful b Interpreted b Portable b Platform independent b filename.java b filename.class(bytecode)

19

Repetition StructuresRepetition Structures

int i = 2;

do { i = i + 1;

} while(i <=100)

Page 20: 1 Java b Object Oriented b Powerful b Interpreted b Portable b Platform independent b filename.java b filename.class(bytecode)

20

ArraysArrays

Creating an array:

MyObject arrayofobjs[] = new MyObject[10];

add actual MyObject objects to that array:

for (int i=0;

i < arrayofobjs.length; i++) arrayofobjs[i] = new MyObject;

Page 21: 1 Java b Object Oriented b Powerful b Interpreted b Portable b Platform independent b filename.java b filename.class(bytecode)

21

Creating Arrays - examplesCreating Arrays - examples

String[] stringArray;String[] stringArray;

int[] intArray;int[] intArray;

boolean[] booleanArray;boolean[] booleanArray;

int[] myIntArray = new int[20];int[] myIntArray = new int[20];

int[] numbers = {2, 5, 8, 10};int[] numbers = {2, 5, 8, 10};

Page 22: 1 Java b Object Oriented b Powerful b Interpreted b Portable b Platform independent b filename.java b filename.class(bytecode)

22

Input/outputInput/output

Files and StreamsFiles and Streams• System.in System.in - - standard input stream objectstandard input stream object• System.out - System.out - standard output stream objectstandard output stream object• System.err - System.err - standard input stream objectstandard input stream object

are three stream object created when a Java are three stream object created when a Java program is executed.program is executed.

Page 23: 1 Java b Object Oriented b Powerful b Interpreted b Portable b Platform independent b filename.java b filename.class(bytecode)

23

Input/outputInput/output

To perform file processing in JavaTo perform file processing in Java

java.iojava.io

package must be imported.package must be imported. Stream ClassesStream Classes

• FileInputStreamFileInputStream - for input from a file - for input from a file

• FileOutputStreamFileOutputStream - for output to a file- for output to a file

Page 24: 1 Java b Object Oriented b Powerful b Interpreted b Portable b Platform independent b filename.java b filename.class(bytecode)

24

Object

InputStream OutputStream

FileInputStream FileOutputStream

PipedInputStream PipedOutputStream

Class Hierarchy

Page 25: 1 Java b Object Oriented b Powerful b Interpreted b Portable b Platform independent b filename.java b filename.class(bytecode)

25

More on Class HierarchyMore on Class Hierarchy

ObjectObject• FileFile• FileDescriptorFileDescriptor• StreamTokenizerStreamTokenizer• InputStreamInputStream• OutputStreamOutputStream• RandomAccessFileRandomAccessFile

Page 26: 1 Java b Object Oriented b Powerful b Interpreted b Portable b Platform independent b filename.java b filename.class(bytecode)

26

More on Class HierarchyMore on Class Hierarchy

InputStreamInputStream• ByteArrayInputStreamByteArrayInputStream• SequenceInputStreamSequenceInputStream• StringBufferInputStreamStringBufferInputStream• PipedInputStreamPipedInputStream• FileInputStreamFileInputStream• FilterInputStreamFilterInputStream

– DataInputStreamDataInputStream– BufferedInputStreamBufferedInputStream– PushBackInputStreamPushBackInputStream– LineNumberInputStreamLineNumberInputStream

Page 27: 1 Java b Object Oriented b Powerful b Interpreted b Portable b Platform independent b filename.java b filename.class(bytecode)

27

More on Class HierarchyMore on Class Hierarchy

OutputStreamOutputStream• ByteArrayOutputStreamByteArrayOutputStream• PipedOutputStreamPipedOutputStream• FileOutputStreamFileOutputStream• FilterOutputStreamFilterOutputStream

– DataOutputStreamDataOutputStream– BufferedOutputStreamBufferedOutputStream– PrintStreamPrintStream

Page 28: 1 Java b Object Oriented b Powerful b Interpreted b Portable b Platform independent b filename.java b filename.class(bytecode)

28

Input/outputInput/output

PipesPipes• synchronised communication synchronised communication

channels between threadschannels between threads• One thread send data to another by One thread send data to another by

writing to a writing to a PipedOutputStream.PipedOutputStream.• The target thread reads information The target thread reads information

from the pipe via a from the pipe via a PipedInputStreamPipedInputStream

Page 29: 1 Java b Object Oriented b Powerful b Interpreted b Portable b Platform independent b filename.java b filename.class(bytecode)

29

Creating a FileOutputStream Creating a FileOutputStream object and attempting to open a object and attempting to open a file:file:

try {try {

output = new DataOutputStream(output = new DataOutputStream(

new FileOutputStream(“afile.dat”));new FileOutputStream(“afile.dat”));

}}

catch ( IOException e ) {catch ( IOException e ) {

System.err.println(“File not opened System.err.println(“File not opened properly\n” + e.toString());properly\n” + e.toString());

System.exit( 1 );}System.exit( 1 );}

Page 30: 1 Java b Object Oriented b Powerful b Interpreted b Portable b Platform independent b filename.java b filename.class(bytecode)

30

Writing to a fileWriting to a file

output.writeInt ( accountNumber);output.writeInt ( accountNumber);

output.writeDouble(doubleValue);output.writeDouble(doubleValue); Closing the fileClosing the file

output.close();output.close();

Page 31: 1 Java b Object Oriented b Powerful b Interpreted b Portable b Platform independent b filename.java b filename.class(bytecode)

31

Opening a file to readOpening a file to read

try {try {

input = new DataInputStream(input = new DataInputStream(

new FileInputStream(“afile.dat”));new FileInputStream(“afile.dat”));

}}

catch ( IOException e ) {catch ( IOException e ) {

System.err.println(“File not opened System.err.println(“File not opened properly\n” + e.toString());properly\n” + e.toString());

System.exit( 1 );}System.exit( 1 );}

Page 32: 1 Java b Object Oriented b Powerful b Interpreted b Portable b Platform independent b filename.java b filename.class(bytecode)

32

Reading from a fileReading from a file

Account = input.readInt();Account = input.readInt();

balance = input.readDouble();balance = input.readDouble();

input.close();input.close();

Page 33: 1 Java b Object Oriented b Powerful b Interpreted b Portable b Platform independent b filename.java b filename.class(bytecode)

33

Read/WriteRead/Write

And of courseAnd of course

System.out.print(“Enter grade:”);System.out.print(“Enter grade:”);

grade = System.in.read();grade = System.in.read();