Objectives Object Oriented Programming OOP review Black-box - - PDF document

objectives
SMART_READER_LITE
LIVE PREVIEW

Objectives Object Oriented Programming OOP review Black-box - - PDF document

9/4/20 Objectives Object Oriented Programming OOP review Black-box programming Creating classes in Java State Constructor Methods Sep 4, 2020 Sprenkle - CSCI209 1 1 Review True or False: you can call methods on an


slide-1
SLIDE 1

9/4/20 1

Objectives

  • Object Oriented Programming

Ø OOP review Ø Black-box programming Ø Creating classes in Java

  • State
  • Constructor
  • Methods

Sep 4, 2020 Sprenkle - CSCI209 1

1

Review

  • True or False: you can call methods on an array,

e.g., int

int[] [] fibNums fibNums = {1, 1, 2, 3, 5}; = {1, 1, 2, 3, 5};

  • What are some Python à Java Gotchas?
  • What does == mean in Java? (i.e., what question

does == ask?)

  • What does static mean?
  • When should we make a method static?
  • What does a static method have access to?

Sep 4, 2020 Sprenkle - CSCI209 2

2

slide-2
SLIDE 2

9/4/20 2

Assignment 3 Feedback: Remember good programming practices

  • No side effects to methods

Ø If method does not say that it is printing something, don’t print something

  • Printing while debugging is fine; remove print

statements before final submission

Ø Want method to reusable à print statements may not be what others want

  • Leverage existing APIs

Ø StringBuilder has replace and reverse methods

  • Likely more efficient than anything you write

Sep 4, 2020 Sprenkle - CSCI209 3

3

Assignment 3 Discussion

Sep 4, 2020 Sprenkle - CSCI209 4

if if (isPalindrome(potentialPalindrome) == true true) { System.out

  • ut.println(potentialPalindrome + " is a palindrome.");

} else else { System.out

  • ut.println(potentialPalindrome + " is not a palindrome");

}

Move from this (when you were new to programming):

if if (isPalindrome(potentialPalindrome)) { System.out

  • ut.println(potentialPalindrome + " is a palindrome.");

} else else { System.out

  • ut.println(potentialPalindrome + " is not a palindrome");

}

To this (at least your 3rd programming course):

4

slide-3
SLIDE 3

9/4/20 3

Assignment 3 Discussion

Sep 4, 2020 Sprenkle - CSCI209 5

if if (string.equals(string2)) { return return true true; } return return false false; Rewrite the above code in one statement.

5

Assignment 3 Discussion

Sep 4, 2020 Sprenkle - CSCI209 6

if if (string.equals(string2)) { return return true true; } return return false false; return return string.equals(string2));

Much more concise and still understandable

6

slide-4
SLIDE 4

9/4/20 4

Review: Object-Oriented Programming

  • What is OO programming?

Ø What are its components?

  • What are its benefits?

Sep 4, 2020 Sprenkle - CSCI209 7

7

Review: Classes & Objects

  • Classes define template from which objects are

made

Ø “Cookie cutters” Ø Define state (aka fields or attributes) Ø Define behavior

  • Many objects can be created of a class

Ø Object: the cookie! Ø Ex: Many Mustangs created from Ford’s “blueprint” Ø Object is an instance of the class

Sep 4, 2020 Sprenkle - CSCI209 8

8

slide-5
SLIDE 5

9/4/20 5

Sep 4, 2020 Sprenkle - CSCI209 9

Constructors

  • Constructor: a special method that constructs

and initializes an object

Ø After construction, can call methods on object

9

Sep 4, 2020 Sprenkle - CSCI209 10

Black-Box Programming

  • How object does something doesn’t matter

Ø Example: if object sorts, does not matter if uses merge or quick sort

  • What object does matters (its functionality)

Ø What object exposes to other objects Ø Referred to as “black-box programming” or encapsulation

Object

  • Has public interface that others can use
  • Hides state from others

10

slide-6
SLIDE 6

9/4/20 6

Discussion

Sep 4, 2020 Sprenkle - CSCI209 11

Object Object Others can see and manipulate

  • bject’s internals
  • May have unintended

consequences Java’s structure helps us enforce black-box programming

What is the problem with white-box programming?

11

Sep 4, 2020 Sprenkle - CSCI209 12

Access Modifiers

  • A public

public method (or instance field) means that any object of any class can directly access the method (or field)

Ø Least restrictive

  • A private

private method (or instance field) means that any object of the same class can directly access this method (or field)

Ø Most restrictive

  • Additional access modifiers will be discussed

with inheritance

In general, what access modifiers will we use for methods? For instance fields?

12

slide-7
SLIDE 7

9/4/20 7

CREATING CLASSES

Sep 4, 2020 Sprenkle - CSCI209 13

13

Classes and Objects

  • Java is pure object-oriented programming

Ø All data and methods in a program must be contained within a class

  • But, for data, can use objects as well as primitive

types (e.g., int, double, char)

Sep 4, 2020 Sprenkle - CSCI209 14

14

slide-8
SLIDE 8

9/4/20 8

Sep 4, 2020 Sprenkle - CSCI209 15

Example: Chicken class

  • State

Ø Name, weight, height

  • Behavior

Ø Accessor methods

  • getWeight, getHeight, getName
  • Convention: “get” for “getter” methods

Ø Mutator methods

  • feed: adds weight and height when bird eats
  • setName

15

General Java Class Structure

Sep 4, 2020 Sprenkle - CSCI209 16

public public class class ClassName { ClassName { // --------- INSTANCE VARIABLES --------------- // define variables that represent object’s state private int private int inst_var inst_var; // --------- CONSTRUCTORS --------------- public public ClassName() { ClassName() { // initialize data structures } // ----------- METHODS ------------ public public int int getInfo() { getInfo() { return return inst_var inst_var; } }

Note: instance variables are private and methods are public

16

slide-9
SLIDE 9

9/4/20 9

Sep 4, 2020 Sprenkle - CSCI209 17

Example: Chicken class

  • State

Ø Name, weight, height

  • Behavior

Ø Accessor methods

  • getWeight, getHeight, getName
  • Convention: “get” for “getter” methods

Ø Mutator methods

  • feed: adds weight, height
  • setName

Ø Convention: “set” for “setter” methods Discussion: data types for state variables?

17

Instance Variables: Chicken.java

Sep 4, 2020 Sprenkle - CSCI209 18

public public class class Chicken { Chicken { // --------- INSTANCE VARIABLES --------------- private String private String name; name; private int private int height; height; // in cm private double private double weight; weight; // in lbs

Instance variables are declared, with access modifier All instance variables are private

private

18

slide-10
SLIDE 10

9/4/20 10

Constructor: Chicken.java

Sep 4, 2020 Sprenkle - CSCI209 19

public public class class Chicken { Chicken { // --------- INSTANCE VARIABLES --------------- private String private String name; name; private private int int height; height; // in cm private double private double weight; weight; // --------- CONSTRUCTORS --------------- public public Chicken( Chicken(String name, int int h, h, double double weight) { weight) { this this.name name = name; = name; this this.height height = h; = h; this this.weight weight = weight; = weight; } …

Observations? Thoughts? Questions?

19

Constructor: Chicken.java

Sep 4, 2020 Sprenkle - CSCI209 20

public public class class Chicken { Chicken { // --------- INSTANCE VARIABLES --------------- private String private String name; name; private private int int height; height; // in cm private double private double weight; weight; // --------- CONSTRUCTORS --------------- public public Chicken( Chicken(String name, int int h, h, double double weight) { weight) { this this.name name = name; = name; this this.height height = h; = h; this this.weight weight = weight; = weight; } …

this this: Special name for the constructed object, like self in Python (differentiate from parameters) Type and name for each parameter Constructor name same as class’s name Params don’t need to be same names as instance var names

20

slide-11
SLIDE 11

9/4/20 11

Sep 4, 2020 Sprenkle - CSCI209 21

Constructors

  • Constructor: a special method that constructs

and initializes an object

Ø After construction, can call methods on object

  • A constructor has the same name as its class

21

Sep 4, 2020 Sprenkle - CSCI209 22

Example: Chicken class

  • State

Ø Name, weight, height

  • Behavior

Ø Accessor methods

  • getWeight, getHeight, getName
  • Convention: “get” for “getter” methods

Ø Mutator methods

  • feed: adds weight, height
  • setName

Discussion: What are the methods’ input (parameters) and output (what is returned)?

22

slide-12
SLIDE 12

9/4/20 12

Methods: Chicken.java

Sep 4, 2020 Sprenkle - CSCI209 23

… // --------- Getter Methods --------------- public public String getName() { return return this. this.name; } // --------- Mutator Methods --------------- public public void void feed() { weight += .3; height += 1; } … }

Note that you don’t have to use this this when variables are unambiguous Chicken object’s instance variables Type the method returns

23

Sep 4, 2020 Sprenkle - CSCI209 24

Constructing objects

  • Given the Chicken constructor

Chicken( String name, int height, double weight )

create a chicken with the following characteristics

Ø “Fred”, weight: 2.0, height: 38

Chicken chicken = new new Chicken("Fred", 38, 2.0);

24

slide-13
SLIDE 13

9/4/20 13

Object References

  • Variable of type Object: value is memory location

Sep 4, 2020 Sprenkle - CSCI209 25

  • ne =

Chicken weight = height = name = 2.0 38 "Fred"

Memory Location

Chicken one = new new Chicken("Fred", 38, 2.0);

25

Sep 4, 2020 Sprenkle - CSCI209 26

Object References

  • Variable of type Object: value is memory location
  • ne =

two = If I haven’t called the constructor, only declared the variables, e.g., Chicken one; Chicken two; Both one and two are equal to null null This is the case for objects. Primitive types are not null.

26

slide-14
SLIDE 14

9/4/20 14

Sep 4, 2020 Sprenkle - CSCI209 27

Null Object Variables

  • An object variable can be explicitly set to null

Ø Means that the object variable does not currently refer to any object

  • Can test if an object variable is set to null

Chicken chick = null; … … … if (chick == null) { . . . } "

27

Recall This Error Message

Sep 4, 2020 Sprenkle - CSCI209 28

28

slide-15
SLIDE 15

9/4/20 15

Sep 4, 2020 Sprenkle - CSCI209 29

Multiple Object Variables

  • More than one object variable can refer to the

same object

Chicken weight = height = name = 3.0 45 "Sallie Mae" sal =

  • ther =

Chicken sal = new Chicken("Sallie Mae"); Chicken other = sal;

29

Chicken’s main method

  • Where we’ll do testing
  • 1. Create object
  • 2. Call methods
  • 3. Verify methods’ results are what you expect
  • When done testing, can move tests into

separate test method

  • Later: better ways to test

Sep 4, 2020 Sprenkle - CSCI209 30

30

slide-16
SLIDE 16

9/4/20 16

Class Development Process

  • 1. Determine state

Ø Declare state at top of class

  • 2. Write constructor

Ø Test

  • 3. Repeat

Ø Write method or constructor Ø Test

Sep 4, 2020 Sprenkle - CSCI209 31

31

TODO

  • Assignment 4 – due Monday

Ø OO programming

  • Textbook – Read “Defining Classes in Java” up to

but not including Inheritance

Sep 4, 2020 Sprenkle - CSCI209 32

32