cop 3503 fall 2012 shayan javed lecture 2
DESCRIPTION
COP 3503 FALL 2012 Shayan Javed Lecture 2. Programming Fundamentals using Java. Introduction to Java. High-level language. Paradigm: Object-Oriented. WORA (“write-once, run anywhere”). Introduction to Java. Some languages are compiled (C/C++). Introduction to Java. - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: COP 3503 FALL 2012 Shayan Javed Lecture 2](https://reader035.vdocuments.us/reader035/viewer/2022062410/56815f01550346895dcdbdba/html5/thumbnails/1.jpg)
1 / 58
COP 3503 FALL 2012SHAYAN JAVED
LECTURE 2
Programming Fundamentals using Java
![Page 2: COP 3503 FALL 2012 Shayan Javed Lecture 2](https://reader035.vdocuments.us/reader035/viewer/2022062410/56815f01550346895dcdbdba/html5/thumbnails/2.jpg)
2 / 58
Introduction to Java
High-level language.
Paradigm: Object-Oriented.
WORA (“write-once, run anywhere”)
![Page 3: COP 3503 FALL 2012 Shayan Javed Lecture 2](https://reader035.vdocuments.us/reader035/viewer/2022062410/56815f01550346895dcdbdba/html5/thumbnails/3.jpg)
3 / 58
Introduction to Java
Some languages are compiled (C/C++)
![Page 4: COP 3503 FALL 2012 Shayan Javed Lecture 2](https://reader035.vdocuments.us/reader035/viewer/2022062410/56815f01550346895dcdbdba/html5/thumbnails/4.jpg)
4 / 58
Introduction to Java
Some languages are compiled (C/C++)
Some are interpreted (Python, Perl, Ruby, etc.)
![Page 5: COP 3503 FALL 2012 Shayan Javed Lecture 2](https://reader035.vdocuments.us/reader035/viewer/2022062410/56815f01550346895dcdbdba/html5/thumbnails/5.jpg)
5 / 58
Introduction to Java
Some languages are compiled (C/C++)
Some are interpreted (Python, Perl, Ruby, etc.)
What about Java?
![Page 6: COP 3503 FALL 2012 Shayan Javed Lecture 2](https://reader035.vdocuments.us/reader035/viewer/2022062410/56815f01550346895dcdbdba/html5/thumbnails/6.jpg)
6 / 58
Introduction to Java
Source code (.java)
Bytecode (.class)
Interpret and run
Compilation and Interpretation
.java = compiled to .class
.class = interpeted by Java Virtual Machine (JVM)
![Page 7: COP 3503 FALL 2012 Shayan Javed Lecture 2](https://reader035.vdocuments.us/reader035/viewer/2022062410/56815f01550346895dcdbdba/html5/thumbnails/7.jpg)
7 / 58
Java Syntax
Similar to C/C++. Variables: byte, short, int, long, float, double, char,
boolean
Field Modifiers: final (“constant”) ,
static (applies to classes)
![Page 8: COP 3503 FALL 2012 Shayan Javed Lecture 2](https://reader035.vdocuments.us/reader035/viewer/2022062410/56815f01550346895dcdbdba/html5/thumbnails/8.jpg)
8 / 58
Java Operators
Numerical: +, -, *, /, % Boolean: >, <, <=, >=, ==, !=, !, instanceof Others: ++, --, Bitwise operators: & (AND),
^ (XOR), | (OR), <<, >> (shift)
![Page 9: COP 3503 FALL 2012 Shayan Javed Lecture 2](https://reader035.vdocuments.us/reader035/viewer/2022062410/56815f01550346895dcdbdba/html5/thumbnails/9.jpg)
9 / 58
if statements
if (boolean-expression) {…
}else if (…) {
…
}else { …}
![Page 10: COP 3503 FALL 2012 Shayan Javed Lecture 2](https://reader035.vdocuments.us/reader035/viewer/2022062410/56815f01550346895dcdbdba/html5/thumbnails/10.jpg)
10 / 58
Conditional expression
boolean-expression ? expression1 : expression2
Example:
int x = 3; int y = (x > 0) ? 1 : 5;
![Page 11: COP 3503 FALL 2012 Shayan Javed Lecture 2](https://reader035.vdocuments.us/reader035/viewer/2022062410/56815f01550346895dcdbdba/html5/thumbnails/11.jpg)
11 / 58
switch statement
switch (byte/short/char/int/String/etc.) {
case x: break;
case y: …..break;
case …:break;
default: …..}
![Page 12: COP 3503 FALL 2012 Shayan Javed Lecture 2](https://reader035.vdocuments.us/reader035/viewer/2022062410/56815f01550346895dcdbdba/html5/thumbnails/12.jpg)
12 / 58
Loops
while (boolean-expression) {// do something
}
do {// something
} while (boolean-expression);
Difference?
![Page 13: COP 3503 FALL 2012 Shayan Javed Lecture 2](https://reader035.vdocuments.us/reader035/viewer/2022062410/56815f01550346895dcdbdba/html5/thumbnails/13.jpg)
13 / 58
Loops for (expression1; boolean-expression;
expression2) {// do something
}
Example:int i;
for (i = 0; i <= 10; i++)i++; // Value of i?
![Page 14: COP 3503 FALL 2012 Shayan Javed Lecture 2](https://reader035.vdocuments.us/reader035/viewer/2022062410/56815f01550346895dcdbdba/html5/thumbnails/14.jpg)
14 / 58
keyword break
break = used to “break out” of a loop. Rest of the code is not executed.
int sum = 0;int number = 0;while (number < 20) {number++;sum += number;if (sum > 100) break;}
![Page 15: COP 3503 FALL 2012 Shayan Javed Lecture 2](https://reader035.vdocuments.us/reader035/viewer/2022062410/56815f01550346895dcdbdba/html5/thumbnails/15.jpg)
15 / 58
keyword continue
continue = used in loops. Break out of current statement, but continue with the rest of the
loop.
int sum = 0;int number = 0;while (number < 20) {number++;if (number == 10)continue;sum += number;}
![Page 16: COP 3503 FALL 2012 Shayan Javed Lecture 2](https://reader035.vdocuments.us/reader035/viewer/2022062410/56815f01550346895dcdbdba/html5/thumbnails/16.jpg)
16 / 58
Methods
Program modularity.
Avoid redundant code! Use whenever possible
Methods can be “called”
![Page 17: COP 3503 FALL 2012 Shayan Javed Lecture 2](https://reader035.vdocuments.us/reader035/viewer/2022062410/56815f01550346895dcdbdba/html5/thumbnails/17.jpg)
17 / 58
Methods modifier returnValueType name (list of parameters) {
...}
public static int max (int num1, int num2) {if (num1 > num2)return num1;elsereturn num2;}
![Page 18: COP 3503 FALL 2012 Shayan Javed Lecture 2](https://reader035.vdocuments.us/reader035/viewer/2022062410/56815f01550346895dcdbdba/html5/thumbnails/18.jpg)
18 / 58
Methods - modifiers
public = can be called by any class private = can only be called by the class Also protected (will look at it later)
static = don’t require an “instance” of the class to call the method.
ClassName.method(...)The Math class – Math.sin(), Math.acos(), etc.
![Page 19: COP 3503 FALL 2012 Shayan Javed Lecture 2](https://reader035.vdocuments.us/reader035/viewer/2022062410/56815f01550346895dcdbdba/html5/thumbnails/19.jpg)
19 / 58
Methods
returnValueType = Can be primitive, class, etc. Even void (nothing to return)
list of parameters = a list of primitives, classes, etc. (or nothing)
![Page 20: COP 3503 FALL 2012 Shayan Javed Lecture 2](https://reader035.vdocuments.us/reader035/viewer/2022062410/56815f01550346895dcdbdba/html5/thumbnails/20.jpg)
20 / 58
Recursion
Methods calling themselves
Write base case first! Otherwise might be stuck forever.
Classic example: Fibonacci numbers
Integer sequence: 0, 1, 1, 2, 3, 5, 8, 13, 21...
F(n) = F(n-1) + F(n-2) F(0) = 0, F(1) = 1
![Page 21: COP 3503 FALL 2012 Shayan Javed Lecture 2](https://reader035.vdocuments.us/reader035/viewer/2022062410/56815f01550346895dcdbdba/html5/thumbnails/21.jpg)
21 / 58
Recursion
public int fibonacci (int n) {if (n == 0 || n == 1) // base case(s)
return n;else
return fibonacci (n-1) + fibonacci (n-2);}
![Page 22: COP 3503 FALL 2012 Shayan Javed Lecture 2](https://reader035.vdocuments.us/reader035/viewer/2022062410/56815f01550346895dcdbdba/html5/thumbnails/22.jpg)
22 / 58
Recursion
Later on we will look at recursion for other algorithms (searching/sorting)
![Page 23: COP 3503 FALL 2012 Shayan Javed Lecture 2](https://reader035.vdocuments.us/reader035/viewer/2022062410/56815f01550346895dcdbdba/html5/thumbnails/23.jpg)
23 / 58
Method overloading
Can have multiple methods with the same name. Showed “max” method with ints Write one with double:
public static double max (double num1, double num2) {
if (num1 > num2)return num1;
elsereturn num2;
}
![Page 24: COP 3503 FALL 2012 Shayan Javed Lecture 2](https://reader035.vdocuments.us/reader035/viewer/2022062410/56815f01550346895dcdbdba/html5/thumbnails/24.jpg)
24 / 58
Commenting
Single-line:// This is a single-line comment
Multi-line:/* This is going to be on multiple lines */
Comment your code properly!Very helpful – to you and others.
![Page 25: COP 3503 FALL 2012 Shayan Javed Lecture 2](https://reader035.vdocuments.us/reader035/viewer/2022062410/56815f01550346895dcdbdba/html5/thumbnails/25.jpg)
25 / 58
Object-Oriented Programming
Paradigm which uses “objects” and “classes”.
![Page 26: COP 3503 FALL 2012 Shayan Javed Lecture 2](https://reader035.vdocuments.us/reader035/viewer/2022062410/56815f01550346895dcdbdba/html5/thumbnails/26.jpg)
26 / 58
Object-Oriented Programming
Paradigm which uses “objects” and “classes”.
Used to represent real-life objects or concepts that can be distinctly identified.
![Page 27: COP 3503 FALL 2012 Shayan Javed Lecture 2](https://reader035.vdocuments.us/reader035/viewer/2022062410/56815f01550346895dcdbdba/html5/thumbnails/27.jpg)
27 / 58
Object-Oriented Programming
Paradigm which uses “objects” and “classes”.
Used to represent real-life objects or concepts that can be distinctly identified.
Objects have properties, methods.
![Page 28: COP 3503 FALL 2012 Shayan Javed Lecture 2](https://reader035.vdocuments.us/reader035/viewer/2022062410/56815f01550346895dcdbdba/html5/thumbnails/28.jpg)
28 / 58
Object-Oriented Programming
Paradigm which uses “objects” and “classes”.
Used to represent real-life objects or concepts that can be distinctly identified.
Objects have properties, methods.
Interaction between objects.
![Page 29: COP 3503 FALL 2012 Shayan Javed Lecture 2](https://reader035.vdocuments.us/reader035/viewer/2022062410/56815f01550346895dcdbdba/html5/thumbnails/29.jpg)
29 / 58
Object-Oriented Programming
Most modern languages support OOP
![Page 30: COP 3503 FALL 2012 Shayan Javed Lecture 2](https://reader035.vdocuments.us/reader035/viewer/2022062410/56815f01550346895dcdbdba/html5/thumbnails/30.jpg)
30 / 58
Object-Oriented Programming
Most modern languages support OOP
Alternatives: Procedural/Imperative ( C ) Functional (Lisp/PROLOG)
![Page 31: COP 3503 FALL 2012 Shayan Javed Lecture 2](https://reader035.vdocuments.us/reader035/viewer/2022062410/56815f01550346895dcdbdba/html5/thumbnails/31.jpg)
31 / 58
Classes in Java
A template for objects of the same type.
![Page 32: COP 3503 FALL 2012 Shayan Javed Lecture 2](https://reader035.vdocuments.us/reader035/viewer/2022062410/56815f01550346895dcdbdba/html5/thumbnails/32.jpg)
32 / 58
Classes in Java
A template for objects of the same type.
You create “objects” (or “instances”) of a class.
![Page 33: COP 3503 FALL 2012 Shayan Javed Lecture 2](https://reader035.vdocuments.us/reader035/viewer/2022062410/56815f01550346895dcdbdba/html5/thumbnails/33.jpg)
33 / 58
Objects in Java
Unique identity, state and behavior.
![Page 34: COP 3503 FALL 2012 Shayan Javed Lecture 2](https://reader035.vdocuments.us/reader035/viewer/2022062410/56815f01550346895dcdbdba/html5/thumbnails/34.jpg)
34 / 58
Objects in Java
Unique identity, state and behavior.
state (properties/attributes): Data fields and their current values.
![Page 35: COP 3503 FALL 2012 Shayan Javed Lecture 2](https://reader035.vdocuments.us/reader035/viewer/2022062410/56815f01550346895dcdbdba/html5/thumbnails/35.jpg)
35 / 58
Objects in Java
Unique identity, state and behavior.
state (properties/attributes): Data fields and their current values.
behavior: The methods for that class
![Page 36: COP 3503 FALL 2012 Shayan Javed Lecture 2](https://reader035.vdocuments.us/reader035/viewer/2022062410/56815f01550346895dcdbdba/html5/thumbnails/36.jpg)
36 / 58
Examplepublic Circle {
// Propertiesprivate double radius;
// Constructorspublic Circle() {
radius = 0.0;}
public Circle(double radius) {this.radius = radius;
}
// Methodspublic double getArea() {
return radius * radius * Math.PI;}
}
![Page 37: COP 3503 FALL 2012 Shayan Javed Lecture 2](https://reader035.vdocuments.us/reader035/viewer/2022062410/56815f01550346895dcdbdba/html5/thumbnails/37.jpg)
37 / 58
Properties
// Propertiesprivate double radius;
private = only accessible by that class directly.
![Page 38: COP 3503 FALL 2012 Shayan Javed Lecture 2](https://reader035.vdocuments.us/reader035/viewer/2022062410/56815f01550346895dcdbdba/html5/thumbnails/38.jpg)
38 / 58
Properties
// Propertiesprivate double radius;
private = only accessible by that class directly.
Not a good idea to have public properties (for security reasons).
![Page 39: COP 3503 FALL 2012 Shayan Javed Lecture 2](https://reader035.vdocuments.us/reader035/viewer/2022062410/56815f01550346895dcdbdba/html5/thumbnails/39.jpg)
39 / 58
Properties
// Propertiesprivate double radius;
private = only accessible by that class directly.
Not a good idea to have public properties (for security reasons).
What if another class needs to access/modify the property?
![Page 40: COP 3503 FALL 2012 Shayan Javed Lecture 2](https://reader035.vdocuments.us/reader035/viewer/2022062410/56815f01550346895dcdbdba/html5/thumbnails/40.jpg)
40 / 58
Properties
Add get/set methods:
![Page 41: COP 3503 FALL 2012 Shayan Javed Lecture 2](https://reader035.vdocuments.us/reader035/viewer/2022062410/56815f01550346895dcdbdba/html5/thumbnails/41.jpg)
41 / 58
Properties
Add get/set methods:
public getRadius() {return radius;
}
![Page 42: COP 3503 FALL 2012 Shayan Javed Lecture 2](https://reader035.vdocuments.us/reader035/viewer/2022062410/56815f01550346895dcdbdba/html5/thumbnails/42.jpg)
42 / 58
Properties
Add get/set methods:
public getRadius() {return radius;
}
public void setRadius(double radius) {this.radius = radius;
}
![Page 43: COP 3503 FALL 2012 Shayan Javed Lecture 2](https://reader035.vdocuments.us/reader035/viewer/2022062410/56815f01550346895dcdbdba/html5/thumbnails/43.jpg)
43 / 58
Properties
Add get/set methods:
public getRadius() {return radius;
}
public void setRadius(double radius) {this.radius = radius;
}POINT OUT THE MISTAKE
![Page 44: COP 3503 FALL 2012 Shayan Javed Lecture 2](https://reader035.vdocuments.us/reader035/viewer/2022062410/56815f01550346895dcdbdba/html5/thumbnails/44.jpg)
44 / 58
this keyword
Refers to the property of this specific class
Used to distinguish between similar-named variables
![Page 45: COP 3503 FALL 2012 Shayan Javed Lecture 2](https://reader035.vdocuments.us/reader035/viewer/2022062410/56815f01550346895dcdbdba/html5/thumbnails/45.jpg)
45 / 58
Constructors
// Constructors// default constructorpublic Circle() {}
public Circle(double radius) {this.radius = radius;
}
![Page 46: COP 3503 FALL 2012 Shayan Javed Lecture 2](https://reader035.vdocuments.us/reader035/viewer/2022062410/56815f01550346895dcdbdba/html5/thumbnails/46.jpg)
46 / 58
Constructors
Special kind of method
Same name as the class
No return type (even void)
Used to initialize objects (using the new keyword)
![Page 47: COP 3503 FALL 2012 Shayan Javed Lecture 2](https://reader035.vdocuments.us/reader035/viewer/2022062410/56815f01550346895dcdbdba/html5/thumbnails/47.jpg)
47 / 58
Constructors
Initialization example:
Circle circle1 = new Circle();
Circle circle2 = new Circle(4.5);
![Page 48: COP 3503 FALL 2012 Shayan Javed Lecture 2](https://reader035.vdocuments.us/reader035/viewer/2022062410/56815f01550346895dcdbdba/html5/thumbnails/48.jpg)
48 / 58
Constructors
Should always provide a default constructor. Does not take in any properties
Good idea to have multiple constructors and default values
![Page 49: COP 3503 FALL 2012 Shayan Javed Lecture 2](https://reader035.vdocuments.us/reader035/viewer/2022062410/56815f01550346895dcdbdba/html5/thumbnails/49.jpg)
49 / 58
Reference Variables
Objects accessed via reference variables.
Example from before:
Circle circle2 = new Circle(4.5);
circle2 = Reference variable used to access the object.
![Page 50: COP 3503 FALL 2012 Shayan Javed Lecture 2](https://reader035.vdocuments.us/reader035/viewer/2022062410/56815f01550346895dcdbdba/html5/thumbnails/50.jpg)
50 / 58
Reference Variables
Can declare without initializing
Circle circle2; // What’s the value?
Initialize later:
circle2 = new Circle(4.5);
![Page 51: COP 3503 FALL 2012 Shayan Javed Lecture 2](https://reader035.vdocuments.us/reader035/viewer/2022062410/56815f01550346895dcdbdba/html5/thumbnails/51.jpg)
51 / 58
Accessing properties/methods
referenceName.property // only if public!
referenceName.method(...)
Example:
double radius = circle1.radius;
double area = circle2.getArea();
![Page 52: COP 3503 FALL 2012 Shayan Javed Lecture 2](https://reader035.vdocuments.us/reader035/viewer/2022062410/56815f01550346895dcdbdba/html5/thumbnails/52.jpg)
52 / 58
Revisiting static
Variables in classes can be static
Associated with the class, rather than a specific object.
Every object shares that variable
![Page 53: COP 3503 FALL 2012 Shayan Javed Lecture 2](https://reader035.vdocuments.us/reader035/viewer/2022062410/56815f01550346895dcdbdba/html5/thumbnails/53.jpg)
53 / 58
Revisiting static
Example:public class Student {
private String name;public int ID;
private static int numberOfStudents = 0;
public Student(String name) {this.name = name;
this.ID = ++numberOfStudents;}
}
![Page 54: COP 3503 FALL 2012 Shayan Javed Lecture 2](https://reader035.vdocuments.us/reader035/viewer/2022062410/56815f01550346895dcdbdba/html5/thumbnails/54.jpg)
54 / 58
Revisiting static
Example:
Student student1 = new Student(“John”);Student student2 = new Student(“Smith”);
![Page 55: COP 3503 FALL 2012 Shayan Javed Lecture 2](https://reader035.vdocuments.us/reader035/viewer/2022062410/56815f01550346895dcdbdba/html5/thumbnails/55.jpg)
55 / 58
Revisiting static
Example:
Student student1 = new Student(“John”);Student student2 = new Student(“Smith”);
System.out.println(student1.ID); // Output?
System.out.println(student2.ID); // Output?
![Page 56: COP 3503 FALL 2012 Shayan Javed Lecture 2](https://reader035.vdocuments.us/reader035/viewer/2022062410/56815f01550346895dcdbdba/html5/thumbnails/56.jpg)
56 / 58
Revisiting static
Example:
Student student1 = new Student(“John”);Student student2 = new Student(“Smith”);
System.out.println(student1.ID); // Prints 1
System.out.println(student2.ID); // Prints 2
![Page 57: COP 3503 FALL 2012 Shayan Javed Lecture 2](https://reader035.vdocuments.us/reader035/viewer/2022062410/56815f01550346895dcdbdba/html5/thumbnails/57.jpg)
57 / 58
Summary
Creating classes
specifying properties, methods
reference variables, initialization
![Page 58: COP 3503 FALL 2012 Shayan Javed Lecture 2](https://reader035.vdocuments.us/reader035/viewer/2022062410/56815f01550346895dcdbdba/html5/thumbnails/58.jpg)
58 / 58
Next lecture
Arrays (single and multi-dimensional)
Strings
Inheritance