pengantar oop class-java. 2 software development tools using sun java sdk alone source file(s)...

21
Pengantar OOP Class-Java

Upload: michael-francis

Post on 17-Jan-2016

234 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Pengantar OOP Class-Java. 2 Software Development Tools Using Sun Java SDK alone Source File(s) (.java) Programmer Compiler (javac) Class File(s) (.class)

Pengantar OOP

Class-Java

Page 2: Pengantar OOP Class-Java. 2 Software Development Tools Using Sun Java SDK alone Source File(s) (.java) Programmer Compiler (javac) Class File(s) (.class)

2

Software Development Tools• Using Sun Java SDK alone

SourceFile(s)(.java)

Programmer

Compiler(javac)

ClassFile(s)(.class)

VirtualMachine

(java)

Editor

Programexecutes

Parts of Sun Java SDK

Command Line Interface

Page 3: Pengantar OOP Class-Java. 2 Software Development Tools Using Sun Java SDK alone Source File(s) (.java) Programmer Compiler (javac) Class File(s) (.class)

3

Using Sun Java SDK Alone

• Example DOS Commands and ParametersC:\ > edit HelloWorld.java (Create/edit “source file” in an external window)C:\ > javac HelloWorld.java (creates .class file)C:\ > java -classpath … HelloWorldHello WorldC:\ > exit

Page 4: Pengantar OOP Class-Java. 2 Software Development Tools Using Sun Java SDK alone Source File(s) (.java) Programmer Compiler (javac) Class File(s) (.class)

4

Software Development Tools• We will use a combination of the Dr Java IDE and

the Sun Java SDK

SourceFile(s)(.java)

Programmer

Dr Java IDE

Compiler(javac)

ClassFile(s)(.class)

VirtualMachine

(java)

Edit Build Run

Programexecutes

Parts of Sun Java SDK

Graphical User Interface

Page 5: Pengantar OOP Class-Java. 2 Software Development Tools Using Sun Java SDK alone Source File(s) (.java) Programmer Compiler (javac) Class File(s) (.class)

5

On-line Demonstration: Dr Java

Page 6: Pengantar OOP Class-Java. 2 Software Development Tools Using Sun Java SDK alone Source File(s) (.java) Programmer Compiler (javac) Class File(s) (.class)

6

Software Development Tools

• Download/install the software development tools on your own PC if you wish to do your project assignments at home– Sun Software Development Kit (SDK)– Dr Java Integrated Development Environment (IDE)

• Use the PCs in the Healey Library labs as they already have these tools installed

Page 7: Pengantar OOP Class-Java. 2 Software Development Tools Using Sun Java SDK alone Source File(s) (.java) Programmer Compiler (javac) Class File(s) (.class)

7

Review of Java• Object Oriented Programming Concepts– Objects and Classes– Encapsulation, Constructors, and Methods– References and Aliases– Interfaces and Inheritance– Class Hierarchies and Polymorphism– Generic Types (ArrayList Class)– Exceptions

• Reading: L&C Appendix B

Page 8: Pengantar OOP Class-Java. 2 Software Development Tools Using Sun Java SDK alone Source File(s) (.java) Programmer Compiler (javac) Class File(s) (.class)

8

Objects and Classes

• Class Definitionpublic class ClassName{// attributes

// methods}

• Instantiating Objects using ClassesClassName myClassName = new ClassName();

Page 9: Pengantar OOP Class-Java. 2 Software Development Tools Using Sun Java SDK alone Source File(s) (.java) Programmer Compiler (javac) Class File(s) (.class)

9

Encapsulation

• Encapsulation of Attributespublic class ClassName{// constantspublic static final int MAX_SIZE = 20;private static final int DEFAULT_SIZE = 10;

// class variablesprivate static int largestSizeOfAll;

// instance variablesprivate int mySize;

}

Page 10: Pengantar OOP Class-Java. 2 Software Development Tools Using Sun Java SDK alone Source File(s) (.java) Programmer Compiler (javac) Class File(s) (.class)

10

Constructors and Methods

• Constructor (ClassName with no return type)public class ClassName{public ClassName (parameter list if any){

statements;}

• Method (Method name with a return type)public type methodName(parameter list if any){

statements;}

}

Page 11: Pengantar OOP Class-Java. 2 Software Development Tools Using Sun Java SDK alone Source File(s) (.java) Programmer Compiler (javac) Class File(s) (.class)

11

Using References

• Using a class constantif (size <= ClassName.MAX_SIZE) statement;

• Using a class methodtype returnValue = ClassName.methodName(...);

• Using an instance method via a referencetype returnValue = myClassName.methodName(...);

Page 12: Pengantar OOP Class-Java. 2 Software Development Tools Using Sun Java SDK alone Source File(s) (.java) Programmer Compiler (javac) Class File(s) (.class)

12

Aliases and Garbage Collection

• Creating an alias of a referenceClassName yourClassName = myClassName;

• Making object eligible for garbage collectionmyClassName = yourClassName = null;

Object of classClassName

myClassName

yourClassName

null

null

Object of classClassName

myClassName

yourClassName“Garbage”

Page 13: Pengantar OOP Class-Java. 2 Software Development Tools Using Sun Java SDK alone Source File(s) (.java) Programmer Compiler (javac) Class File(s) (.class)

13

Interfaces

• A class that implements an interfacepublic class ClassName implements InterfaceName{. . .

}<<interface>>

InterfaceName

ClassName Class must define codefor all methods definedin InterfaceName

Interface defines themethod signature forall required methods

Page 14: Pengantar OOP Class-Java. 2 Software Development Tools Using Sun Java SDK alone Source File(s) (.java) Programmer Compiler (javac) Class File(s) (.class)

14

Inheritance

• A class that extends another classpublic class ClassName extends SuperClassName{. . .

}

SuperClassName

ClassName SubClass may define codeto override some or all ofthe SuperClass methods

SuperClass defines all themethods for all SubClasses

Page 15: Pengantar OOP Class-Java. 2 Software Development Tools Using Sun Java SDK alone Source File(s) (.java) Programmer Compiler (javac) Class File(s) (.class)

15

Inheritance

• A class that extends an abstract classpublic class ClassName extends SuperClassName{. . .

}<<abstract>>

SuperClassName

ClassName SubClass may define code to overridesome or all of the SuperClass methods,but must define code for the signatures

SuperClass defines some of themethods for all SubClasses, but only defines method signaturesfor the rest of the methods

Page 16: Pengantar OOP Class-Java. 2 Software Development Tools Using Sun Java SDK alone Source File(s) (.java) Programmer Compiler (javac) Class File(s) (.class)

16

Class Hierarchies and Polymorphism

• An object reference variable may hold a reference to any compatible type of object

• Compatibility may be via implementing an interface or inheritance from another classClassName a = new ClassName();

InterfaceName b = new ClassName();

SuperClassName c = new ClassName();

• Object behaves as class it was “born as” (i.e. class used with the new operator)

Page 17: Pengantar OOP Class-Java. 2 Software Development Tools Using Sun Java SDK alone Source File(s) (.java) Programmer Compiler (javac) Class File(s) (.class)

17

Generic Types

• Collection classes like the ArrayList class can be defined to hold a specific type of object via a generic type designation <T>ArrayList<String> myList = new ArrayList<String>();

• We will use generics often in CS210 with many other types of “collection” classes

Page 18: Pengantar OOP Class-Java. 2 Software Development Tools Using Sun Java SDK alone Source File(s) (.java) Programmer Compiler (javac) Class File(s) (.class)

18

Exceptions

• When code encounters a situation that is impossible for it to resolve, it may throw an Exception object, e.g. NameOfException instead of executing its normal return

• If a method may throw an exception, it should indicate that in its method headerpublic void methodName() throws NameOfException{if (boolean condition of impossible situation) throw new NameOfException();

}

Page 19: Pengantar OOP Class-Java. 2 Software Development Tools Using Sun Java SDK alone Source File(s) (.java) Programmer Compiler (javac) Class File(s) (.class)

19

Exception Handling

• Code that calls a method that may throw a checked exception must use try-catch or indicate that it throws that exception in its own method headertry{

statements with call to methodName();}catch (NameOfException e) // may be multiple catch clauses{

statements to recover from occurrence of exception}finally // optional finally clause{ statements always performed, e.g. clean up actions}

Page 20: Pengantar OOP Class-Java. 2 Software Development Tools Using Sun Java SDK alone Source File(s) (.java) Programmer Compiler (javac) Class File(s) (.class)

20

File Input: Exampleimport java.util.Scanner;import java.io.*;

public class FileDisplay{ public static void main (String [] args)

throws IOException { Scanner scan = new Scanner(System.in); System.out.println("Enter name of file to display"); File file = new File(scan.nextLine()); Scanner fileScan = new Scanner (file); while (fileScan.hasNext()) System.out.println(fileScan.nextLine()); }}

Page 21: Pengantar OOP Class-Java. 2 Software Development Tools Using Sun Java SDK alone Source File(s) (.java) Programmer Compiler (javac) Class File(s) (.class)

21

File Output: Exampleimport java.util.Scanner;import java.io.*;

public class FileWrite{ public static void main (String [] args) throws IOException { // Get filename and instantiate File object as before

PrintStream out = new PrintStream(file); while (scan.hasNext()) { String line = scan.nextLine(); if (line.equals("END")) // A sentinel String value break; else out.println(line); } out.close(); }}