Presentation is loading. Please wait.

Presentation is loading. Please wait.

SYDE223 Tutorial 3: Introduction to Java Annie En-Shiun Lee January 19, 2010.

Similar presentations


Presentation on theme: "SYDE223 Tutorial 3: Introduction to Java Annie En-Shiun Lee January 19, 2010."— Presentation transcript:

1 SYDE223 Tutorial 3: Introduction to Java Annie En-Shiun Lee January 19, 2010

2 Outline 1.Java v.s. C++ 2.Language Basics 3.HelloWorld example 4.Introduction to OOP

3 1. Java v.s. C++

4 Java v.s. C++ Smaller/easier Object Oriented Programming (OOP) Single inheritance Platform independence

5 Java v.s. C++ Platform independence

6 Java v.s. C++ Java Virtual Machine No Pointers or Templates Extensive Library Simpler String and Data Structures

7 2. Language Basics

8 Variables Scope & Types – Instance variable (non static) – Class variable (static fields) static int numGears = 6; – Local variable – Parameter public static void main(String[] args)

9 Variables Primitive data – Byte – Short – Int – Long – Float – Double – Boolean – Char – NOTE: String is an “object”

10 Operators Assignment – Operand = value – Ex. int i = 0 Arithmetic: +, -, *, /, % (remainder) – Math – 2 operands

11 Operators Unary: +, -, ++, --, ! – One operand act on one variable – Ex. Int i = 1 i = -i i++;

12 Operators Equality – == – != – > – >= – < – <= conditional operators – && – ||

13 Operators int value1 = 1; int value2 = 2; if(value1 == value2) System.out.println("value1 == value2"); if(value1 != value2) System.out.println("value1 != value2"); if(value1 > value2) System.out.println("value1 > value2"); if(value1 < value2) System.out.println("value1 < value2"); if(value1 <= value2) System.out.println("value1 <= value2"); if((value1 == 1) && (value2 == 2)) System.out.println("value1 is 1 AND value2 is 2"); if((value1 == 1) || (value2 == 1)) System.out.println("value1 is 1 OR value2 is 1");

14 If-then, if-else-then int testscore = 76; char grade; if (testscore >= 90) { grade = 'A'; } else if (testscore >= 80) { grade = 'B'; } else if (testscore >= 70) { grade = 'C'; } else if (testscore >= 60) { grade = 'D'; } else { grade = 'F'; } System.out.println("Grade = " + grade); }

15 Switch int month = 8; switch (month) { case 1: System.out.println("January"); break; case 2: System.out.println("February"); break; case 3: System.out.println("March"); break; case 4: System.out.println("April"); break; case 5: System.out.println("May"); break; case 6: System.out.println("June"); break; case 7: System.out.println("July"); break; case 8: System.out.println("August"); break; case 9: System.out.println("September"); break; case 10: System.out.println("October"); break; case 11: System.out.println("November"); break; case 12: System.out.println("December"); break; default: System.out.println("Invalid month.");break;

16 While while (expression) { statement(s) } int count = 1; while (count < 11) { System.out.println("Count is: " + count); count++; }

17 Do-While do{ statement(s) } while (expression) int count = 1; do { System.out.println("Count is: " + count); count++; } while (count < 11);

18 For for (initialization; termination; increment){ statement(s) } for(int i=1; i<11; i++){{ System.out.println("Count is: " + count); }

19 3. Hello World Example

20 “Main” method Each executable Java application has a “main” method. Using the keyword “private” one can restrict the access to methods to the classes within the same directory “void” means the method returns no object

21 4. Intro to OOP

22 Object Oriented Programming object has 2 characteristics – state, – behaviour Ex. Dog – State – name, color, breed, mood Mood = sad, happy, angry – Behaviour – barking, wagging tail, whining Ex. Java – State – Object store fields – Behaviour – methods

23 Object Oriented Programming Advantages – Modularity – Information-hiding – Code re-use – Pluggability and debugging ease

24 Objects Instance of the class of objects Ex. Professor Tizhoosh’s dog is named “alpha” Ex. Annie the TA’s dog is named “beta” Ex. Student A’s dog is named “delta”

25 Inheritance A class inherit commonly used state and behaviour from other class – Ex. Dog  small dog, big dog class SmallDog extends Dog { // new fields and methods defining a Dog would go here }

26 Inheritance A class inherit commonly used state and behaviour from other class – Ex. Dog  small dog, big dog class SmallDog extends Dog { // new fields and methods defining a Dog would go here class SmallDog extends Dog { // new fields and methods defining a Dog would go here } }


Download ppt "SYDE223 Tutorial 3: Introduction to Java Annie En-Shiun Lee January 19, 2010."

Similar presentations


Ads by Google