syde223 tutorial 3: introduction to java annie en-shiun lee january 19, 2010
TRANSCRIPT
![Page 1: SYDE223 Tutorial 3: Introduction to Java Annie En-Shiun Lee January 19, 2010](https://reader036.vdocuments.mx/reader036/viewer/2022062516/56649da75503460f94a928f8/html5/thumbnails/1.jpg)
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](https://reader036.vdocuments.mx/reader036/viewer/2022062516/56649da75503460f94a928f8/html5/thumbnails/2.jpg)
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](https://reader036.vdocuments.mx/reader036/viewer/2022062516/56649da75503460f94a928f8/html5/thumbnails/3.jpg)
1. Java v.s. C++
![Page 4: SYDE223 Tutorial 3: Introduction to Java Annie En-Shiun Lee January 19, 2010](https://reader036.vdocuments.mx/reader036/viewer/2022062516/56649da75503460f94a928f8/html5/thumbnails/4.jpg)
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](https://reader036.vdocuments.mx/reader036/viewer/2022062516/56649da75503460f94a928f8/html5/thumbnails/5.jpg)
Java v.s. C++
• Platform independence
![Page 6: SYDE223 Tutorial 3: Introduction to Java Annie En-Shiun Lee January 19, 2010](https://reader036.vdocuments.mx/reader036/viewer/2022062516/56649da75503460f94a928f8/html5/thumbnails/6.jpg)
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](https://reader036.vdocuments.mx/reader036/viewer/2022062516/56649da75503460f94a928f8/html5/thumbnails/7.jpg)
2. Language Basics
![Page 8: SYDE223 Tutorial 3: Introduction to Java Annie En-Shiun Lee January 19, 2010](https://reader036.vdocuments.mx/reader036/viewer/2022062516/56649da75503460f94a928f8/html5/thumbnails/8.jpg)
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](https://reader036.vdocuments.mx/reader036/viewer/2022062516/56649da75503460f94a928f8/html5/thumbnails/9.jpg)
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](https://reader036.vdocuments.mx/reader036/viewer/2022062516/56649da75503460f94a928f8/html5/thumbnails/10.jpg)
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](https://reader036.vdocuments.mx/reader036/viewer/2022062516/56649da75503460f94a928f8/html5/thumbnails/11.jpg)
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](https://reader036.vdocuments.mx/reader036/viewer/2022062516/56649da75503460f94a928f8/html5/thumbnails/12.jpg)
Operators
• Equality– ==– !=– > – >=– < – <=
• conditional operators– &&– ||
![Page 13: SYDE223 Tutorial 3: Introduction to Java Annie En-Shiun Lee January 19, 2010](https://reader036.vdocuments.mx/reader036/viewer/2022062516/56649da75503460f94a928f8/html5/thumbnails/13.jpg)
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](https://reader036.vdocuments.mx/reader036/viewer/2022062516/56649da75503460f94a928f8/html5/thumbnails/14.jpg)
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](https://reader036.vdocuments.mx/reader036/viewer/2022062516/56649da75503460f94a928f8/html5/thumbnails/15.jpg)
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](https://reader036.vdocuments.mx/reader036/viewer/2022062516/56649da75503460f94a928f8/html5/thumbnails/16.jpg)
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](https://reader036.vdocuments.mx/reader036/viewer/2022062516/56649da75503460f94a928f8/html5/thumbnails/17.jpg)
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](https://reader036.vdocuments.mx/reader036/viewer/2022062516/56649da75503460f94a928f8/html5/thumbnails/18.jpg)
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](https://reader036.vdocuments.mx/reader036/viewer/2022062516/56649da75503460f94a928f8/html5/thumbnails/19.jpg)
3. Hello World Example
![Page 20: SYDE223 Tutorial 3: Introduction to Java Annie En-Shiun Lee January 19, 2010](https://reader036.vdocuments.mx/reader036/viewer/2022062516/56649da75503460f94a928f8/html5/thumbnails/20.jpg)
“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](https://reader036.vdocuments.mx/reader036/viewer/2022062516/56649da75503460f94a928f8/html5/thumbnails/21.jpg)
4. Intro to OOP
![Page 22: SYDE223 Tutorial 3: Introduction to Java Annie En-Shiun Lee January 19, 2010](https://reader036.vdocuments.mx/reader036/viewer/2022062516/56649da75503460f94a928f8/html5/thumbnails/22.jpg)
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](https://reader036.vdocuments.mx/reader036/viewer/2022062516/56649da75503460f94a928f8/html5/thumbnails/23.jpg)
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](https://reader036.vdocuments.mx/reader036/viewer/2022062516/56649da75503460f94a928f8/html5/thumbnails/24.jpg)
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](https://reader036.vdocuments.mx/reader036/viewer/2022062516/56649da75503460f94a928f8/html5/thumbnails/25.jpg)
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](https://reader036.vdocuments.mx/reader036/viewer/2022062516/56649da75503460f94a928f8/html5/thumbnails/26.jpg)
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 }}