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

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

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

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

SYDE223 Tutorial 3:Introduction to Java

Annie En-Shiun LeeJanuary 19, 2010

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

Outline

1. Java v.s. C++2. Language Basics3. HelloWorld example4. Introduction to OOP

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

1. Java v.s. C++

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

Java v.s. C++

• Smaller/easier• Object Oriented Programming (OOP)• Single inheritance • Platform independence

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

Java v.s. C++

• Platform independence

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

Java v.s. C++

• Java Virtual Machine• No Pointers or Templates• Extensive Library• Simpler String and Data Structures

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

2. Language Basics

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

Variables

• Scope & Types– Instance variable (non static)– Class variable (static fields)• static int numGears = 6;

– Local variable– Parameter• public static void main(String[] args)

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

Variables

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

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

Operators

• Assignment– Operand = value– Ex. int i = 0

• Arithmetic: +, -, *, /, % (remainder)– Math– 2 operands

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

Operators

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

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

Operators

• Equality– ==– !=– > – >=– < – <=

• conditional operators– &&– ||

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

Operatorsint 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");

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

If-then, if-else-thenint 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);}

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

Switchint 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;

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

While

while (expression) {statement(s)

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

}

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

Do-While

do{statement(s)

} while (expression)  int count = 1;do {System.out.println("Count is: " + count);count++;

} while (count < 11);

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

For

for (initialization; termination; increment){statement(s)

}  for(int i=1; i<11; i++){{System.out.println("Count is: " + count);

}

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

3. Hello World Example

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

“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

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

4. Intro to OOP

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

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

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

Object Oriented Programming

• Advantages– Modularity– Information-hiding– Code re-use– Pluggability and debugging ease

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

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”

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

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 

}

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

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 }}