comp200 java review - discipline of musichughm/oop/slides/lecture2-review.pdf · comp200 java...

Post on 04-Jun-2020

3 Views

Category:

Documents

1 Downloads

Preview:

Click to see full reader

TRANSCRIPT

1

COMP200 JAVA REVIEW

OOP using Java, from slides by Shayan Javed

2

Introduction to Java

High-level language.

Paradigm: Object-Oriented.

WORA (“write-once, run anywhere”)

3

Introduction to Java

Some languages are compiled (C/C++)

4

Introduction to Java

Some languages are compiled (C/C++)

Some are interpreted (Python, Perl, Ruby, etc.)

5

Introduction to Java

Some languages are compiled (C/C++)

Some are interpreted (Python, Perl, Ruby, etc.)

What about Java?

6

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)

7

Java Syntax

Similar to C/C++. Variables: byte, short, int, long, float, double, char, boolean

Field Modifiers: final (“constant”) , static (applies to classes)

8

Java Operators

Numerical: +, -, *, /, % Boolean: >, <, <=, >=, ==, !=, !, instanceof Others: ++, --, Bitwise operators: & (AND),

^ (XOR), | (OR), <<, >> (shift)

9

if statements

if (boolean-expression) {

}

else if (…) {…

}

else {

}

10

Conditional expression

boolean-expression ? expression1 : expression2

Example:

int x = 3;

int y = (x > 0) ? 1 : 5;

11

switch statement

switch (byte/short/char/int/String/etc.) { case x:

break; case y: …..

break; case …:

break; default: …..

}

12

Loops

while (boolean-expression) {// do something

}

do {// something

} while (boolean-expression);

Difference?

13

Loops

for (expression1; boolean-expression; expression2) {

// do something

}

Example: int i;

for (i = 0; i <= 10; i++)

i++; // Value of i?

14

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

15

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;

}

16

Methods

Program modularity.

Avoid redundant code! Use whenever possible

Methods can be “called”

17

Methods

modifier returnValueType name (list of parameters) {

...

}

public static int max (int num1, int num2) {

if (num1 > num2)

return num1;

else

return num2;

}

18

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.

19

Methods

returnValueType = Can be primitive, class, etc. Even void (nothing to return)

list of parameters = a list of primitives, classes, etc. (or nothing)

20

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

21

Recursion

public int fibonacci (int n) {

if (n == 0 || n == 1) // base case(s)

return n;

else

return fibonacci (n-1) + fibonacci (n-2);

}

22

Recursion

Later on we will look at recursion for other algorithms (searching/sorting)

23

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;

else

return num2;

}

24

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.

25

Object-Oriented Programming

Paradigm which uses “objects” and “classes”.

26

Object-Oriented Programming

Paradigm which uses “objects” and “classes”.

Used to represent real-life objects or concepts that can be distinctly identified.

27

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.

28

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.

29

Object-Oriented Programming

Most modern languages support OOP

30

Object-Oriented Programming

Most modern languages support OOP

Alternatives: ! Procedural/Imperative ( C ) ! Functional (Lisp/PROLOG)

31

Classes in Java

A template for objects of the same type.

32

Classes in Java

A template for objects of the same type.

You create “objects” (or “instances”) of a class.

33

Objects in Java

Unique identity, state and behavior.

34

Objects in Java

Unique identity, state and behavior.

state (properties/attributes): Data fields and their current values.

35

Objects in Java

Unique identity, state and behavior.

state (properties/attributes): Data fields and their current values.

behavior: The methods for that class

36

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

}

37

Properties

// Propertiesprivate double radius;

private = only accessible by that class directly.

38

Properties

// Propertiesprivate double radius;

private = only accessible by that class directly.

Not a good idea to have public properties (for security reasons).

39

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?

40

Properties

Add get/set methods:

41

Properties

Add get/set methods:

public double getRadius() {

return radius;

}

42

Properties

Add get/set methods:

public double getRadius() {return radius;

}

public void setRadius(double radius) {this.radius = radius;

}

43

this keyword

Refers to the property of this specific class

Used to distinguish between similar-named variables

44

Constructors

// Constructors// default constructorpublic Circle() {}

public Circle(double radius) {this.radius = radius;

}

45

Constructors

Special kind of method

Same name as the class

No return type (even void)

Used to initialize objects (using the new keyword)

46

Constructors

Initialization example:

Circle circle1 = new Circle();

Circle circle2 = new Circle(4.5);

47

Constructors

Should always provide a default constructor. ! Does not take in any properties

Good idea to have multiple constructors and default values

48

Reference Variables

Objects accessed via reference variables.

Example from before:

Circle circle2 = new Circle(4.5);

circle2 = Reference variable used to access the object.

49

Reference Variables

Can declare without initializing

Circle circle2; // What’s the value?

Initialize later:

circle2 = new Circle(4.5);

50

Accessing properties/methods

referenceName.property // only if public!

referenceName.method(...)

Example:

double radius = circle1.radius;

double area = circle2.getArea();

51

Revisiting static

Variables in classes can be static

Associated with the class, rather than a specific object.

Every object shares that variable

52

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

}

53

Revisiting static

Example:

Student student1 = new Student(“John”);

Student student2 = new Student(“Smith”);

54

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?

55

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

56

Summary

Creating classes

specifying properties, methods

reference variables, initialization

57

Next lecture

Arrays (single and multi-dimensional)

Strings

Inheritance

top related