39
CSC3170 INTRODUCTION TO DATABASE SYSTEMS Tutorial 6 Java

Tutorial 6 Java. Basic Object-Oriented programming concept (OO Concept) Java basic Useful API (for project) Compile and Run Useful link and

Embed Size (px)

Citation preview

Page 1: Tutorial 6 Java.  Basic Object-Oriented programming concept (OO Concept)  Java basic  Useful API (for project)  Compile and Run  Useful link and

CSC3170 INTRODUCTION TO DATABASE SYSTEMS

Tutorial 6Java

Page 2: Tutorial 6 Java.  Basic Object-Oriented programming concept (OO Concept)  Java basic  Useful API (for project)  Compile and Run  Useful link and

OUTLINE

Basic Object-Oriented programming concept (OO Concept)

Java basic Useful API (for project) Compile and Run Useful link and reference

Page 3: Tutorial 6 Java.  Basic Object-Oriented programming concept (OO Concept)  Java basic  Useful API (for project)  Compile and Run  Useful link and

OO CONCEPT

Object-Oriented programming It uses objects and their interactions to design

computer program. Object

Object is used to describe an entity in the real world.

Class A description of a set of objects that share the

same attributes, operations, relationships, and semantics.

Page 4: Tutorial 6 Java.  Basic Object-Oriented programming concept (OO Concept)  Java basic  Useful API (for project)  Compile and Run  Useful link and

OO CONCEPT -- OBJECT

Real world entity State Behavior

E.g. A student Student ID, name,

major, ……, etc. Self-introduction, ...,

etc.

OOP instance variable methods

Alice

Self-introduction

08123456

Alice

CSC

state

method

object

SID

Major

Name

Page 5: Tutorial 6 Java.  Basic Object-Oriented programming concept (OO Concept)  Java basic  Useful API (for project)  Compile and Run  Useful link and

OO CONCEPT -- OBJECT

Self-introduction C: Self_Intro(Alice); ---- function OOP:Alice.Self_Intro(); ----method

Object = related data + function

state method

Page 6: Tutorial 6 Java.  Basic Object-Oriented programming concept (OO Concept)  Java basic  Useful API (for project)  Compile and Run  Useful link and

OO CONCEPT -- CLASS

A class is a template of an object

Student

Self-introduction

state

method

object

SID

Major

Name

Page 7: Tutorial 6 Java.  Basic Object-Oriented programming concept (OO Concept)  Java basic  Useful API (for project)  Compile and Run  Useful link and

They are all instances of class “Student”

OO CONCEPT -- CLASS

Student

Self-introduction

SID

Major

Name

Alice

Self-introduction

08987654

Alice

CEG

SID

Major

Name

James

Self-introduction

07123456

James

CSC

SID

Major

Name

Rose

Self-introduction

09321654

Rose

IEE

SID

Major

Name

Object “Alice” Object “Rose”

Object “James”

Class “Student”

Page 8: Tutorial 6 Java.  Basic Object-Oriented programming concept (OO Concept)  Java basic  Useful API (for project)  Compile and Run  Useful link and

Instance variables values are independent of instances

Instance methods Can access only after created the instance

the output may vary due to the differences of instance variables

OO CONCEPT -- CLASS

Page 9: Tutorial 6 Java.  Basic Object-Oriented programming concept (OO Concept)  Java basic  Useful API (for project)  Compile and Run  Useful link and

OO CONCEPT -- CLASS

Class variable All instances share a single copy

Class methods Cannot access the instance variables and

instance methods Student.name Student.self_intro() Student.classMethods()

Page 10: Tutorial 6 Java.  Basic Object-Oriented programming concept (OO Concept)  Java basic  Useful API (for project)  Compile and Run  Useful link and

OO CONCEPT – FEATURES

Inheritance Each subclass inherits all variables and methods

of its superclass. Abstraction

Simply complex reality. Encapsulation

The values of variables of an object are private. Polymorphism

Allows the objects of different types respond to the method call of the same name.

Page 11: Tutorial 6 Java.  Basic Object-Oriented programming concept (OO Concept)  Java basic  Useful API (for project)  Compile and Run  Useful link and

JAVA BASIC – HELLO WORLD//helloworld.c#include <stdio.h>int main(int argc, char * argv[]){ /* print “Hello World!” */ printf("Hello World!\n"); return 0;}

//HelloWorld.javaimport java.io.*; class HelloWorld { public static void main(String[] args) { /* print “Hello World!” */ System.out.println("Hello World!"); }}

JAVA

C

Page 12: Tutorial 6 Java.  Basic Object-Oriented programming concept (OO Concept)  Java basic  Useful API (for project)  Compile and Run  Useful link and

JAVA BASIC – HELLO WORLD//helloworld.c#include <stdio.h>int main(int argc, char * argv[]){ /* print “Hello World!” */ printf("Hello World!\n"); return 0;}

//HelloWorld.javaimport java.io.*; class HelloWorld { public static void main(String[] args) { /* print “Hello World!” */ System.out.println("Hello World!"); }}

JAVA

C

Page 13: Tutorial 6 Java.  Basic Object-Oriented programming concept (OO Concept)  Java basic  Useful API (for project)  Compile and Run  Useful link and

JAVA BASIC – HELLO WORLD

Comment: the same with C (“//” and “/*…*/”)

#include <stdio.h> → import java.io.*; a huge number of (standard) packages

ready for use Define a new class called “HelloWorld” Define the “main” method in class

“HelloWorld”

Page 14: Tutorial 6 Java.  Basic Object-Oriented programming concept (OO Concept)  Java basic  Useful API (for project)  Compile and Run  Useful link and

JAVA BASIC – HELLO WORLD

Visibility modifier public, protected, private

“static” modifier static class variable/method

Return type void, int, double, char, boolean, <Array>, <Object>,

<Interface> Method name Parameter list

Primitives: pass-by-value Reference Types: “pass-by-reference”

//import java.io.*; //not required for console outputclass HelloWorld { /* print “Hello World!” */ public static void main(String[] args) { System.out.println("Hello World!"); }}

visibility

modifiers

return type

method name

parameter list

Page 15: Tutorial 6 Java.  Basic Object-Oriented programming concept (OO Concept)  Java basic  Useful API (for project)  Compile and Run  Useful link and

JAVA BASIC -- SIMILAR TO C

Data Types short, int, long, float, double

Operators +, -, *, /, %, ++, --, >, >=, <, <=, ==, !=,

&&, ||, !, &, |, >>, <<, ……

Page 16: Tutorial 6 Java.  Basic Object-Oriented programming concept (OO Concept)  Java basic  Useful API (for project)  Compile and Run  Useful link and

JAVA BASIC -- SIMILAR TO C

Control Flows (Selection)if (<expression>) { <statement(s)>} else { <statement(s)>}switch (<expression>) { case <constant>: <statement(s)> break; case <constant>: <statement(s)> break; default: <statement(s)>}

Page 17: Tutorial 6 Java.  Basic Object-Oriented programming concept (OO Concept)  Java basic  Useful API (for project)  Compile and Run  Useful link and

JAVA BASIC -- SIMILAR TO C

Control Flows (iteration)while(<expression>) { <statement(s)>}

do { <statement(s)>} while (<expression>);

for(<initialization>; <termination>; <increment>){ <statement(s)>}

Page 18: Tutorial 6 Java.  Basic Object-Oriented programming concept (OO Concept)  Java basic  Useful API (for project)  Compile and Run  Useful link and

JAVA BASIC -- DIFFERENT FROM C

External Dependencies #include <header.h> import package.*;

Data Types char byte (an 8-bit integer)

char char (a character, e.g. ‘c’) int boolean (e.g. true / false)

int int (a 32-bit integer) char string[20] = “C Program”; /* an array */

String string = “Java”; //an object int array[8];

int[] array = new int[8];//an object

Page 19: Tutorial 6 Java.  Basic Object-Oriented programming concept (OO Concept)  Java basic  Useful API (for project)  Compile and Run  Useful link and

JAVA BASIC -- DIFFERENT FROM C

Operators new

Object obj = new Object(); Student student = new Student();

instanceof (student instanceof Student) = true (obj instanceof Student) = false (anything instanceof Object) = true

There is no pointers, no “alloc” in java. System will manage the memory. We need not delete

instances.

Page 20: Tutorial 6 Java.  Basic Object-Oriented programming concept (OO Concept)  Java basic  Useful API (for project)  Compile and Run  Useful link and

JAVA BASIC -- DIFFERENT FROM C To invoke the Java

Program:

Then the argument array: args[0] = “Peter” args[1] = “Paul” args[2] = “Mary” args.length = 3

For C program:

Then, the argument array: argv[0] = “hello_word” argv[1] = “Peter” argv[2] = “Paul” argv[3] = “Mary” argc = 4

java HelloWorld Peter Paul Mary

hello_world Peter Paul Mary

Page 21: Tutorial 6 Java.  Basic Object-Oriented programming concept (OO Concept)  Java basic  Useful API (for project)  Compile and Run  Useful link and

JAVA BASIC -- FIRST JAVA PROGRAM

Model your application with objects Design the states and behaviors of

each object Define a class for each type of object

Implement the states (with instance variables) and behaviors (with methods)

Write the main method to create objects and start the object interactions

Page 22: Tutorial 6 Java.  Basic Object-Oriented programming concept (OO Concept)  Java basic  Useful API (for project)  Compile and Run  Useful link and

JAVA BASIC -- FIRST JAVA PROGRAMclass Student { public static int NoOfStudents = 0; public String SID; public String Name; public String Major;

//constructor (create an object / instance) public Student(String SID, String Name, String Major){ NoOfStudents++; this.SID = SID; this.Name = Name; this.Major = Major; }

public void Self_Intro() { System.out.println(“Student ID:” + this.SID); System.out.println(“Name:”+this.Name); System.out.println(“Major:”+this.Major); }}

Student

Self_Intro

SID

Major

Name

Page 23: Tutorial 6 Java.  Basic Object-Oriented programming concept (OO Concept)  Java basic  Useful API (for project)  Compile and Run  Useful link and

JAVA BASIC -- FIRST JAVA PROGRAMclass Student { public static int NoOfStudents = 0; public String SID; public String Name; public String Major;

//constructor (create an object / instance) public Student(String SID, String Name, String Major){ NoOfStudents++; this.SID = SID; this.Name = Name; this.Major = Major; }

public void Self_Intro() { System.out.println(“Student ID:”+this.SID); System.out.println(“Name:”+this.Name); System.out.println(“Major:”+this.Major); }}

If you make everything “public”, everyone can change your “state”, So …

private String SID;public String getStudentID() { return SID;}

Now, only you can change your status and others can just see it but cannot modify it.

Page 24: Tutorial 6 Java.  Basic Object-Oriented programming concept (OO Concept)  Java basic  Useful API (for project)  Compile and Run  Useful link and

JAVA BASIC -- FIRST JAVA PROGRAM//Defining the class Studentclass Student { //class variable private static int NoOfStudents = 0; //instance variables private String SID; private String Name; private String Major;

//constructor public Student(String SID, String

Name, String Major) { NoOfStudents++; this. SID = SID; this. Name = Name; this. Major = Major; }

//class method public static int getNoOfStudents()

{ return NoOfStudents; }

//instance methods public String getStudentID() { return SID; }

public String getName() { return Name; }

public String getMajor() { return Major; }

public void Self_Intro() { System.out.println(“Student

ID:”+this.SID);

System.out.println(“Name:”+this.Name);

System.out.println(“Major:”+this.Major);

}}

Page 25: Tutorial 6 Java.  Basic Object-Oriented programming concept (OO Concept)  Java basic  Useful API (for project)  Compile and Run  Useful link and

JAVA BASIC -- FIRST JAVA PROGRAM

Model your application with objects Design the states and behaviors of

each object Define a class for each type of object

Implement the states (with instance variables) and behaviors (with methods)

Write the main method to create objects and start the object interactions

Page 26: Tutorial 6 Java.  Basic Object-Oriented programming concept (OO Concept)  Java basic  Useful API (for project)  Compile and Run  Useful link and

JAVA BASIC -- FIRST JAVA PROGRAMclass FirstClass { public static void main(String[] args) { Student alice, james, rose;

alice = new Student(“08987654", "Alice", “CEG");

james = new Student(“07123456", “James", “CSC");

rose = new Student(“09321654", “Rose", “IEE");

System.out.println(“Prof. Wong:”+alice.getStudentID());

alice.Self_Intro();

System.out.println (“Prof. Wong :”+ james.getStudentID());

james.Self_Intro();

System.out.println (“Prof. Wong :”+ rose.getStudentID());

rose.Self_Intro();}}

Page 27: Tutorial 6 Java.  Basic Object-Oriented programming concept (OO Concept)  Java basic  Useful API (for project)  Compile and Run  Useful link and

JAVA BASIC – TOSTRING()public void Self_Intro() {

System.out.println(“Student ID:”+this.SID);

System.out.println(“Name:”+this.Name);

System.out.println(“Major:”+this.Major);

}

public String toString() { return “Student ID:”+this.SID+”\n”+ “Name:”+this.Name+”\

n”+ “Major:”+this.Major; }

System.out.println(alice);

Page 28: Tutorial 6 Java.  Basic Object-Oriented programming concept (OO Concept)  Java basic  Useful API (for project)  Compile and Run  Useful link and

JAVA BASIC – JAVA IO

Standard Output System.out.print[ln]()

can accept boolean, char, char[], double, float, int, long, Object, and String

Page 29: Tutorial 6 Java.  Basic Object-Oriented programming concept (OO Concept)  Java basic  Useful API (for project)  Compile and Run  Useful link and

JAVA BASIC – JAVA IO

Standard Input import java.io.*; BufferedReader in = new BufferedReader(new

InputStreamReader(System.in)); To read string

str = in.readLine(); //Read a line char c = in.read(); //Read a char

To read numbers read String then convert to appropriate type using

Integer.parseInt(), Double.parseDouble(), …

Page 30: Tutorial 6 Java.  Basic Object-Oriented programming concept (OO Concept)  Java basic  Useful API (for project)  Compile and Run  Useful link and

File Input BufferedReader inFile = new

BufferedReader(new FileReader(new File(“filename”)));

JAVA BASIC – JAVA IO

Page 31: Tutorial 6 Java.  Basic Object-Oriented programming concept (OO Concept)  Java basic  Useful API (for project)  Compile and Run  Useful link and

JAVA BASIC –EXCEPTION HANDLING

Exception provides a smart way to handle “exceptional event”

An appropriate “exception handler” takes over when exception is thrown

You can also throw or re-throw an exception if you don’t know how to handle.

try { <statement(s)>} catch (<exception type> <name>) { <statement(s)>} finally { /* this will be executed after normal execution or execution of an exception handler */ <statement(s)>}

Page 32: Tutorial 6 Java.  Basic Object-Oriented programming concept (OO Concept)  Java basic  Useful API (for project)  Compile and Run  Useful link and

32

USEFUL API

public String[] split(String regex) Splits this string (i.e. the string that

invokes the split() method) around matches of the given regular expression

Example

/48

String str = "boo:and:foo";String[] result;result = str.split(":");System.out.println("(1)" + result[0]);System.out.println("(2)" + result[1]);System.out.println("(3)" + result[2]);

(1) boo(2) and(3) foo

Page 33: Tutorial 6 Java.  Basic Object-Oriented programming concept (OO Concept)  Java basic  Useful API (for project)  Compile and Run  Useful link and

public boolean equals(Object anObject) Compare Strings Example

Don’t use == for String Comparison

USEFUL API

if(str.equals("CSCI3170")) {…

}

Page 34: Tutorial 6 Java.  Basic Object-Oriented programming concept (OO Concept)  Java basic  Useful API (for project)  Compile and Run  Useful link and

Java uses an interpreter to run the program Add JAVA_HOME to Environment Variables

with the path of JDK6 like C:\Program Files\Java\jdk1.6.0_24

COMPILE AND RUN

Page 35: Tutorial 6 Java.  Basic Object-Oriented programming concept (OO Concept)  Java basic  Useful API (for project)  Compile and Run  Useful link and

Add %JAVA_HOME%\bin;%JAVA_HOME% in Path

COMPILE AND RUN

Page 36: Tutorial 6 Java.  Basic Object-Oriented programming concept (OO Concept)  Java basic  Useful API (for project)  Compile and Run  Useful link and

COMPILE AND RUN

Java Compiler javac Compile your source file(s) Usage: javac <source file(s)>

E.g. javac Student.java, javac *.java

Java Interpreter java Run your Java program Usage: java <class file>

E.g. java Student No “.java” / “.class” at the end

Page 37: Tutorial 6 Java.  Basic Object-Oriented programming concept (OO Concept)  Java basic  Useful API (for project)  Compile and Run  Useful link and

IDE – NETBEANS

Netbeans is a fully-featured, free and open-source Java IDE written completely in Java

You can download from: http://netbeans.org/

Page 38: Tutorial 6 Java.  Basic Object-Oriented programming concept (OO Concept)  Java basic  Useful API (for project)  Compile and Run  Useful link and

USEFUL REFERENCE

The Java Tutorial http://download.oracle.com/javase/tutorial/

Java APIs http://download.oracle.com/javase/6/docs/a

pii/ CSE Summer Preparatory Course

http://www.cse.cuhk.edu.hk/~csesc/ Free Electronic Book: Thinking in Java,

3rd Edition http://www.mindviewinc.com/Books/downlo

ads.html

Page 39: Tutorial 6 Java.  Basic Object-Oriented programming concept (OO Concept)  Java basic  Useful API (for project)  Compile and Run  Useful link and

Q&A