Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSC3170 Introduction to Database Systems

Similar presentations


Presentation on theme: "CSC3170 Introduction to Database Systems"— Presentation transcript:

1 CSC3170 Introduction to Database Systems
Tutorial 6 Java CSC3170 Introduction to Database Systems

2 Outline Basic Object-Oriented programming concept (OO Concept)
Java basic Useful API (for project) Compile and Run Useful link and reference

3 OO Concept Object-Oriented programming Object Class
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.

4 OO Concept -- Object Real world entity E.g. A student OOP State
Behavior E.g. A student Student ID, name, major, ……, etc. Self-introduction, ..., etc. OOP instance variable methods Alice Self-introduction CSC state method object SID Major Name

5 OO Concept -- Object Self-introduction
C: Self_Intro(Alice); function OOP: Alice.Self_Intro(); ----method Object = related data + function state method

6 OO Concept -- Class A class is a template of an object Student
Self-introduction state method object SID Major Name

7 OO Concept -- Class They are all instances of class “Student”
Self-introduction SID Major Name Alice CEG James CSC Rose IEE Class “Student” Object “Alice” Object “James” Object “Rose”

8 OO Concept -- Class Instance variables Instance methods
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

9 OO Concept -- Class Class variable Class methods
All instances share a single copy Class methods Cannot access the instance variables and instance methods  Student.name  Student.self_intro()  Student.classMethods()

10 OO Concept – features Inheritance Abstraction Encapsulation
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.

11 Java basic – Hello World
//helloworld.c #include <stdio.h> int main(int argc, char * argv[]){ /* print “Hello World!” */ printf("Hello World!\n"); return 0; } C //HelloWorld.java import java.io.*; class HelloWorld { public static void main(String[] args) { /* print “Hello World!” */ System.out.println("Hello World!"); } JAVA

12 Java basic – Hello World
//helloworld.c #include <stdio.h> int main(int argc, char * argv[]){ /* print “Hello World!” */ printf("Hello World!\n"); return 0; } C //HelloWorld.java import java.io.*; class HelloWorld { public static void main(String[] args) { /* print “Hello World!” */ System.out.println("Hello World!"); } JAVA

13 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”

14 Java basic – Hello World
//import java.io.*; //not required for console output class HelloWorld { /* print “Hello World!” */ public static void main(String[] args) { System.out.println("Hello World!"); } visibility modifiers return type method name parameter list 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”

15 Java basic -- Similar to C
Data Types short, int, long, float, double Operators +, -, *, /, %, ++, --, >, >=, <, <=, ==, !=, &&, ||, !, &, |, >>, <<, ……

16 Java basic -- Similar to C
Control Flows (Selection) if (<expression>) { <statement(s)> } else { } switch (<expression>) { case <constant>: <statement(s)> break; default: <statement(s)> }

17 Java basic -- Similar to C
Control Flows (iteration) while(<expression>) { <statement(s)> } do { <statement(s)> } while (<expression>); for(<initialization>; <termination>; <increment>){ <statement(s)> }

18 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

19 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.

20 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

21 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

22 Java basic -- First Java Program
class 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

23 Java basic -- First Java Program
class 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.

24 Java basic -- First Java Program
//Defining the class Student class 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);

25 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

26 Java basic -- First Java Program
class FirstClass { public static void main(String[] args) { Student alice, james, rose; alice = new Student(“ ", "Alice", “CEG"); james = new Student(“ ", “James", “CSC"); rose = new Student(“ ", “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(); }

27 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); } System.out.println(alice); public String toString() { return “Student ID:”+this.SID+”\n”+ “Name:”+this.Name+”\n”+ “Major:”+this.Major; }

28 Java basic – java IO Standard Output System.out.print[ln]()
can accept boolean, char, char[], double, float, int, long, Object, and String

29 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(), …

30 Java basic – java IO File Input
BufferedReader inFile = new BufferedReader(new FileReader(new File(“filename”)));

31 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>) { } finally { /* this will be executed after normal execution or execution of an exception handler */ }

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 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]); boo and foo /48

33 Useful API public boolean equals(Object anObject) Compare Strings
Example Don’t use == for String Comparison if(str.equals("CSCI3170")) { }

34 Compile and Run 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

35 Compile and Run Add %JAVA_HOME%\bin;%JAVA_HOME% in Path

36 Compile and Run Java Compiler javac Java Interpreter java
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

37 IDE – NetBeans Netbeans is a fully-featured, free and open-source Java IDE written completely in Java You can download from:

38 Useful Reference The Java Tutorial Java APIs
Java APIs CSE Summer Preparatory Course Free Electronic Book: Thinking in Java, 3rd Edition

39 Q&A


Download ppt "CSC3170 Introduction to Database Systems"

Similar presentations


Ads by Google