objectives
play

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


  1. 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 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 1

  2. 9/4/20 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 Move from this (when you were new to programming): if if (isPalindrome(potentialPalindrome) == true true ) { System. out out .println(potentialPalindrome + " is a palindrome."); } else else { System. out out .println(potentialPalindrome + " is not a palindrome"); } To this (at least your 3 rd programming course): if if (isPalindrome(potentialPalindrome)) { System. out out .println(potentialPalindrome + " is a palindrome."); } else else { System. out out .println(potentialPalindrome + " is not a palindrome"); } Sep 4, 2020 Sprenkle - CSCI209 4 4 2

  3. 9/4/20 Assignment 3 Discussion if if (string.equals(string2)) { return return true true; } return return false false; Rewrite the above code in one statement. Sep 4, 2020 Sprenkle - CSCI209 5 5 Assignment 3 Discussion if if (string.equals(string2)) { return return true true; } return return false false; Much more concise and still understandable return return string.equals(string2)); Sep 4, 2020 Sprenkle - CSCI209 6 6 3

  4. 9/4/20 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 4

  5. 9/4/20 Constructors • Constructor: a special method that constructs and initializes an object Ø After construction, can call methods on object Sep 4, 2020 Sprenkle - CSCI209 9 9 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 Sep 4, 2020 Sprenkle - CSCI209 10 10 5

  6. 9/4/20 Discussion What is the problem with white-box programming? Object Object Others can see and manipulate Java’s structure helps us object’s internals enforce black-box • May have unintended programming consequences Sep 4, 2020 Sprenkle - CSCI209 11 11 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? Sep 4, 2020 Sprenkle - CSCI209 12 12 6

  7. 9/4/20 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 7

  8. 9/4/20 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 Sep 4, 2020 Sprenkle - CSCI209 15 15 General Java Class Structure 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 Sep 4, 2020 Sprenkle - CSCI209 16 16 8

  9. 9/4/20 Example: Chicken class Discussion : data types for • State state variables? Ø 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 Sep 4, 2020 Sprenkle - CSCI209 17 17 Instance Variables: Chicken.java public class public 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 Sep 4, 2020 Sprenkle - CSCI209 18 18 9

  10. 9/4/20 Constructor: Chicken.java public public class class Chicken { Chicken { // --------- INSTANCE VARIABLES --------------- private String private String name; name; private private int int height; height; // in cm private double weight; private double weight; // --------- CONSTRUCTORS --------------- public Chicken( public Chicken( String name, int int h, h, double weight) { double weight) { this this.name name = name; = name; this this.height height = h; = h; this this.weight weight = weight; = weight; } … Observations? Thoughts? Questions? Sep 4, 2020 Sprenkle - CSCI209 19 19 Constructor: Chicken.java public public class class Chicken { Chicken { // --------- INSTANCE VARIABLES --------------- private String private String name; name; private int private int height; height; // in cm Type and name for private double private double weight; weight; Constructor name same as class’s name each parameter // --------- CONSTRUCTORS --------------- public public Chicken( Chicken( String name, int int h, h, double double weight) { weight) { this this.name name = name; = name; Params don’t need to be same this this.height height = h; = h; this this.weight weight = weight; = weight; names as instance var names } this this : Special name for the constructed object, … like self in Python (differentiate from parameters) Sep 4, 2020 Sprenkle - CSCI209 20 20 10

  11. 9/4/20 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 Sep 4, 2020 Sprenkle - CSCI209 21 21 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)? Sep 4, 2020 Sprenkle - CSCI209 22 22 11

  12. 9/4/20 Methods: Chicken.java Type the method returns … // --------- Getter Methods --------------- public String getName() { public return this. return this. name ; Chicken object’s } instance variables // --------- Mutator Methods --------------- void feed() { public public void weight += .3; height += 1; } … } Note that you don’t have to use this this when variables are unambiguous Sep 4, 2020 Sprenkle - CSCI209 23 23 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); Sep 4, 2020 Sprenkle - CSCI209 24 24 12

  13. 9/4/20 Object References • Variable of type Object: value is memory location new Chicken("Fred", 38, 2.0); Chicken one = new Chicken weight = 2.0 Memory Location 38 height = one = name = "Fred" Sep 4, 2020 Sprenkle - CSCI209 25 25 Object References • Variable of type Object: value is memory location If I haven’t called the constructor, only declared the variables, e.g., one = Chicken one; Chicken two; two = Both one and two are equal to null null This is the case for objects. Primitive types are not null . Sep 4, 2020 Sprenkle - CSCI209 26 26 13

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend