Announcements Lab 03 inBase late submissions Midterm #1 Friday - - PowerPoint PPT Presentation

announcements
SMART_READER_LITE
LIVE PREVIEW

Announcements Lab 03 inBase late submissions Midterm #1 Friday - - PowerPoint PPT Presentation

Announcements Lab 03 inBase late submissions Midterm #1 Friday Covers everything through basics of OOP Project 2 is posted Due next Wednesday All about nested loops good exam practice Coding style is part of the


slide-1
SLIDE 1

Announcements

  • Lab 03 inBase – late submissions
  • Midterm #1 Friday

– Covers everything through basics of OOP

  • Project 2 is posted

– Due next Wednesday – All about nested loops – good exam practice – Coding style is part of the grade

  • Evan Golub’s slides
  • Avoiding Redundant code
slide-2
SLIDE 2

Basics of Object Oriented Programming

Cont’d

slide-3
SLIDE 3

The keyword

Memory

Object 01 x … …

3

Object 02 x … …

  • 1

Object 03 x … …

101

Class

Refers to whichever object is currently executing the instruction

slide-4
SLIDE 4

Execution flow

public class MyObj { //A sample class definition int data; //instance field public MyObj(int dat) { //Constructor this.data = dat; } //Instance methods public void grow(int more) { this.data = this.data + more; } public void gift(MyObj other) {

  • ther.data = other.data + this.data;

this.data = 0; } }

slide-5
SLIDE 5

Execution flow

public class MyObj { //A sample class definition int data; //instance field public MyObj(int dat) { //Constructor this.data = dat; } //Instance methods public void grow(int more) { this.data = this.data + more; } public void gift(MyObj other) {

  • ther.data = other.data + this.data;

this.data = 0; } }

slide-6
SLIDE 6

Execution flow

//A main method somewhere else public static void main(String[] args) { MyObj obj1 = new MyObj(1); MyObj obj2 = new MyObj(2); MyObj obj3 = new MyObj(3);

  • bj1.grow(3);
  • bj2.grow(3);
  • bj1.gift(obj2);

}

slide-7
SLIDE 7

Execution flow

//A main method somewhere else public static void main(String[] args) { MyObj obj1 = new MyObj(1); MyObj obj2 = new MyObj(2); … }

  • bj1

data

slide-8
SLIDE 8

Execution flow

//A main method somewhere else public static void main(String[] args) { MyObj obj1 = new MyObj(1); MyObj obj2 = new MyObj(2); … }

  • bj1

data dat 1

slide-9
SLIDE 9

Execution flow

public class MyObj { //A sample class definition … public MyObj(int dat) { //Constructor this.data = dat; } … } dat 1

  • bj1

data

slide-10
SLIDE 10

Execution flow

public class MyObj { //A sample class definition … public MyObj(int dat) { //Constructor this.data = dat; } … }

  • bj1

data 1 dat 1

this.data

slide-11
SLIDE 11

Execution flow

//A main method somewhere else public static void main(String[] args) { … MyObj obj1 = new MyObj(1); MyObj obj2 = new MyObj(2); … }

  • bj1

data 1

slide-12
SLIDE 12

Execution flow

//A main method somewhere else public static void main(String[] args) { … MyObj obj1 = new MyObj(1); MyObj obj2 = new MyObj(2); … }

  • bj1

data 1

  • bj2

data

slide-13
SLIDE 13

Execution flow

//A main method somewhere else public static void main(String[] args) { … MyObj obj1 = new MyObj(1); MyObj obj2 = new MyObj(2); … }

  • bj1

data 1

  • bj2

data dat 2

slide-14
SLIDE 14

Execution flow

public class MyObj { //A sample class definition … public MyObj(int dat) { //Constructor this.data = dat; } … } dat 2

  • bj1

data

  • bj2

data

1

slide-15
SLIDE 15

Execution flow

public class MyObj { //A sample class definition … public MyObj(int dat) { //Constructor this.data = dat; } … }

  • bj1

data 1 dat 2

  • bj2

data 2

this.data

slide-16
SLIDE 16

Execution flow

//A main method somewhere else public static void main(String[] args) { … MyObj obj1 = new MyObj(1); MyObj obj2 = new MyObj(2); … }

  • bj1

data 1

  • bj2

data 2

slide-17
SLIDE 17

Execution flow

//A main method somewhere else public static void main(String[] args) { … MyObj obj2 = new MyObj(2); MyObj obj3 = new MyObj(3); … }

  • bj1

data 1

  • bj2

data 2

slide-18
SLIDE 18

Execution flow

//A main method somewhere else public static void main(String[] args) { … MyObj obj2 = new MyObj(2); MyObj obj3 = new MyObj(3); … }

  • bj1

data 1

  • bj2

data 2

  • bj3

data 3

slide-19
SLIDE 19

Execution flow

//A main method somewhere else public static void main(String[] args) { … MyObj obj3 = new MyObj(3);

  • bj1.grow(3);

… }

  • bj1

data 1

  • bj2

data 2

  • bj3

data 3 more 3

slide-20
SLIDE 20

Execution flow

//A main method somewhere else public static void main(String[] args) { … MyObj obj3 = new MyObj(3);

  • bj1.grow(3);

… }

  • bj1

data 1

  • bj2

data 2

  • bj3

data 3 more 3

slide-21
SLIDE 21

Execution flow

public class MyObj { … public void grow(int more) { this.data = this.data + more; } … }

  • bj1

data 1

  • bj2

data 2

  • bj3

data 3 more 3

slide-22
SLIDE 22

Execution flow

public class MyObj { … public void grow(int more) { this.data = this.data + more; } … }

  • bj1

data 1

this.data

  • bj2

data 2

  • bj3

data 3 more 3

slide-23
SLIDE 23

Execution flow

public class MyObj { … public void grow(int more) { this.data = this.data + more; } … }

  • bj1

data 4

this.data

  • bj2

data 2

  • bj3

data 3 more 3

slide-24
SLIDE 24

Execution flow

//A main method somewhere else public static void main(String[] args) { … MyObj obj3 = new MyObj(3);

  • bj1.grow(3);

… }

  • bj1

data 4

  • bj2

data 2

  • bj3

data 3

slide-25
SLIDE 25

Execution flow

//A main method somewhere else public static void main(String[] args) { …

  • bj1.grow(3);
  • bj2.grow(3);

… }

  • bj1

data 4

  • bj2

data 2

  • bj3

data 3 more 3

slide-26
SLIDE 26

Execution flow

//A main method somewhere else public static void main(String[] args) { …

  • bj1.grow(3);
  • bj2.grow(3);

… }

  • bj1

data 4

  • bj2

data 2

  • bj3

data 3 more 3

slide-27
SLIDE 27

Execution flow

public class MyObj { … public void grow(int more) { this.data = this.data + more; } … }

  • bj1

data 4

  • bj2

data 2

  • bj3

data 3 more 3

slide-28
SLIDE 28

Execution flow

public class MyObj { … public void grow(int more) { this.data = this.data + more; } … }

  • bj1

data 4

  • bj2

data 2

  • bj3

data 3 more 3

this.data

slide-29
SLIDE 29

Execution flow

public class MyObj { … public void grow(int more) { this.data = this.data + more; } … }

  • bj1

data 4

  • bj2

data 5

  • bj3

data 3 more 3

this.data

slide-30
SLIDE 30

Execution flow

//A main method somewhere else public static void main(String[] args) { …

  • bj1.grow(3);
  • bj2.grow(3);

… }

  • bj1

data 4

  • bj2

data 5

  • bj3

data 3

slide-31
SLIDE 31

Execution flow

//A main method somewhere else public static void main(String[] args) { …

  • bj2.grow(3);
  • bj1.gift(obj2);

… }

  • bj1

data 4

  • bj2

data 5

  • bj3

data 3

  • ther obj2
slide-32
SLIDE 32

Execution flow

//A main method somewhere else public static void main(String[] args) { …

  • bj2.grow(3);
  • bj1.gift(obj2);

… }

  • bj1

data 4

  • bj2

data 5

  • bj3

data 3

  • ther obj2
slide-33
SLIDE 33

Execution flow

public class MyObj { … public void gift(MyObj other) {

  • ther.data = other.data + this.data;

this.data = 0; } }

  • bj1

data 4

  • bj2

data 5

  • bj3

data 3

  • ther obj2
slide-34
SLIDE 34

Execution flow

public class MyObj { … public void gift(MyObj other) {

  • ther.data = other.data + this.data;

this.data = 0; } }

  • bj1

data 4

  • bj2

data 5

  • bj3

data 3

  • ther obj2
slide-35
SLIDE 35

Execution flow

public class MyObj { … public void gift(MyObj other) {

  • ther.data = other.data + this.data;

this.data = 0; } }

  • bj1

data 4

  • bj2

data 5

  • bj3

data 3

  • ther obj2
  • ther.data
slide-36
SLIDE 36

Execution flow

public class MyObj { … public void gift(MyObj other) {

  • ther.data = other.data + this.data;

this.data = 0; } }

  • bj1

data 4

  • bj2

data 5

  • bj3

data 3

  • ther obj2

this.data

slide-37
SLIDE 37

Execution flow

public class MyObj { … public void gift(MyObj other) {

  • ther.data = other.data + this.data;

this.data = 0; } }

  • bj1

data 4

  • bj2

data 9

  • bj3

data 3

  • ther obj2

this.data

slide-38
SLIDE 38

Execution flow

public class MyObj { … public void gift(MyObj other) {

  • ther.data = other.data + this.data;

this.data = 0; } }

  • bj1

data 4

  • bj2

data 9

  • bj3

data 3

  • ther obj2

this.data

slide-39
SLIDE 39

Execution flow

public class MyObj { … public void gift(MyObj other) {

  • ther.data = other.data + this.data;

this.data = 0; } }

  • bj1

data 0

  • bj2

data 9

  • bj3

data 3

  • ther obj2

this.data

slide-40
SLIDE 40

Execution flow

//A main method somewhere else public static void main(String[] args) { …

  • bj2.grow(3);
  • bj1.gift(obj2);

… }

  • bj1

data 0

  • bj2

data 9

  • bj3

data 3

slide-41
SLIDE 41

Omit the this keyword?

  • If you reference an instance field or

method, and omit the this keyword, Java assumes you are referring to the current object.

  • Standard practice is to omit this

in all but a few special cases.

slide-42
SLIDE 42

Omit the this keyword?

public class MyObj { //A sample class definition int data; //instance field public MyObj(int dat) { //Constructor this.data = dat; } //Instance methods public void grow(int more) { this.data = this.data + more; } public void gift(MyObj other) {

  • ther.data = other.data + this.data;

this.data = 0; } }

slide-43
SLIDE 43

Omit the this keyword?

public class MyObj { //A sample class definition int data; //instance field public MyObj(int dat) { //Constructor data = dat; } //Instance methods public void grow(int more) { data = data + more; } public void gift(MyObj other) {

  • ther.data = other.data + data;

data = 0; } }

slide-44
SLIDE 44

When to use this

  • In a constructor:

public MyObj(int dat) { //Constructor data = dat; } VS public MyObj(int data) { //Constructor this.data = data; }

  • Review shadowing (Lec04 –

ArgsAndParams and example)

slide-45
SLIDE 45

When to use this

  • To return the object at the end.

Compare:

public void grow(int more) { data = data + more; } … //Somewhere else

  • bj1.grow(3)

System.out.println(obj1);

slide-46
SLIDE 46

When to use this

  • To return the object at the end.

With:

public MyObj grow(int more) { data = data + more; return this; } … //Somewhere else System.out.println(obj1.grow(3));

slide-47
SLIDE 47

A brief glance at access modifiers

  • Control whether objects of one type can

access members in objects of another type.

– public: Any object can access – private: Only objects of the same type can access

  • Gamble example

– If bias is a private field in Coin, Gambler objects can’t access it.

slide-48
SLIDE 48

Real-world Objects

  • Integer

– compareTo, intValue – Static elements:

  • parseInt
  • BigInteger

– Factorial without overflow – Static elements:

  • ONE, TEN, ZERO
  • valueOf, probablePrime
  • String

– charAt, indexOf, substring – Static elements:

  • valueOf, format