Creating an object Instance variables

Similar documents
CS 251 Intermediate Programming Methods and Classes

Object-Oriented Programming (OOP) Basics. CSCI 161 Introduction to Programming I

CS 251 Intermediate Programming Methods and More

Example: Fibonacci Numbers

Chapter 4: Writing Classes

Inheritance (Extends) Overriding methods IS-A Vs. HAS-A Polymorphism. superclass. is-a. subclass

Anatomy of a Class Encapsulation Anatomy of a Method

Chapter 6 Introduction to Defining Classes

Handout 7. Defining Classes part 1. Instance variables and instance methods.

Encapsulation. Administrative Stuff. September 12, Writing Classes. Quick review of last lecture. Classes. Classes and Objects

1 Epic Test Review 2 Epic Test Review 3 Epic Test Review 4. Epic Test Review 5 Epic Test Review 6 Epic Test Review 7 Epic Test Review 8

Express Yourself. Writing Your Own Classes

AP Computer Science Chapter 10 Implementing and Using Classes Study Guide

CS1004: Intro to CS in Java, Spring 2005

Lesson 10A OOP Fundamentals. By John B. Owen All rights reserved 2011, revised 2014

Introduction to Programming Using Java (98-388)

Objects and Classes. 1 Creating Classes and Objects. CSCI-UA 101 Objects and Classes

CmSc 150 Fundamentals of Computing I. Lesson 28: Introduction to Classes and Objects in Java. 1. Classes and Objects

Recursion 1. Recursion is the process of defining something in terms of itself.

CSE 214 Computer Science II Java Classes and Information Hiding

Outline. Object Oriented Programming. Course goals. Staff. Course resources. Assignments. Course organization Introduction Java overview Autumn 2003

CS 170 Java Programming 1. Week 12: Creating Your Own Types

Binghamton University. CS-140 Fall Problem Solving. Creating a class from scratch

Contents. I. Classes, Superclasses, and Subclasses. Topic 04 - Inheritance

Java Classes & Primitive Types

Java Classes & Primitive Types

Chapter 15: Object Oriented Programming

ITI Introduction to Computing II

Chapter 4 Defining Classes I

Object-Oriented Programming Concepts

The return Statement

Object Class. EX: LightSwitch Class. Basic Class Concepts: Parts. CS257 Computer Science II Kevin Sahr, PhD. Lecture 5: Writing Object Classes

Table of Contents Date(s) Title/Topic Page #s. Chapter 4: Writing Classes 4.1 Objects Revisited

3.1 Class Declaration

11/19/2014. Objects. Chapter 4: Writing Classes. Classes. Writing Classes. Java Software Solutions for AP* Computer Science A 2nd Edition

CMPT 115. C tutorial for students who took 111 in Java. University of Saskatchewan. Mark G. Eramian, Ian McQuillan CMPT 115 1/32

ITI Introduction to Computing II

Introduction to Visual Basic and Visual C++ Introduction to Java. JDK Editions. Overview. Lesson 13. Overview

Classes, interfaces, & documentation. Review of basic building blocks

CS112 Lecture: Defining Instantiable Classes

Object Oriented Programming: In this course we began an introduction to programming from an object-oriented approach.

Java Object Oriented Design. CSC207 Fall 2014

12/22/11. Java How to Program, 9/e. public must be stored in a file that has the same name as the class and ends with the.java file-name extension.

Outline. Java Models for variables Types and type checking, type safety Interpretation vs. compilation. Reasoning about code. CSCI 2600 Spring

Classes Classes 2 / 35

CS112 Lecture: Working with Numbers

In Java there are three types of data values:

STUDENT LESSON A5 Designing and Using Classes

CS/ENGRD 2110 FALL Lecture 2: Objects and classes in Java

An introduction to Java II

Lecture 3. Lecture

Abstract Data Types (ADT) and C++ Classes

OBJECTS AND CLASSES CHAPTER. Final Draft 10/30/2011. Slides by Donald W. Smith TechNeTrain.com

1B1b Inheritance. Inheritance. Agenda. Subclass and Superclass. Superclass. Generalisation & Specialisation. Shapes and Squares. 1B1b Lecture Slides

boolean, char, class, const, double, else, final, float, for, if, import, int, long, new, public, return, static, throws, void, while

Object Oriented Modeling

Java How to Program, 10/e. Copyright by Pearson Education, Inc. All Rights Reserved.

Chapter 4. Defining Classes I

Chapter 10 Introduction to Classes

An Introduction to C++

Defining Classes and Methods

Lecture 18 Tao Wang 1

Classes. Classes. Classes. Class Circle with methods. Class Circle with fields. Classes and Objects in Java. Introduce to classes and objects in Java.

EMBEDDED SYSTEMS PROGRAMMING OO Basics

Programming II (CS300)

Pointers and Terminal Control

An Introduction To Writing Your Own Classes CSC 123 Fall 2018 Howard Rosenthal

Objects and Classes: Working with the State and Behavior of Objects

Defining Classes and Methods

Goal of lecture. Object-oriented Programming. Context of discussion. Message of lecture

CMSC 132: Object-Oriented Programming II

1B1b Classes in Java Part I

About this exam review

PART A : MULTIPLE CHOICE Circle the letter of the best answer (1 mark each)

Fundamentos de programação

CS18000: Programming I

JAVA: A Primer. By: Amrita Rajagopal

CS 231 Data Structures and Algorithms, Fall 2016

Method Invocation. Zheng-Liang Lu Java Programming 189 / 226

CompuScholar, Inc. 9th - 12th grades

Assumptions. History

inside: THE MAGAZINE OF USENIX & SAGE August 2003 volume 28 number 4 PROGRAMMING McCluskey: Working with C# Classes

SPRING 13 CS 0007 FINAL EXAM V2 (Roberts) Your Name: A pt each. B pt each. C pt each. D or 2 pts each

Computer Science II. OO Programming Classes Scott C Johnson Rochester Institute of Technology

CS11 Intro C++ Spring 2018 Lecture 1

System.out.print(); Scanner.nextLine(); String.compareTo();

Subclass Gist Example: Chess Super Keyword Shadowing Overriding Why? L10 - Polymorphism and Abstract Classes The Four Principles of Object Oriented

CS111: PROGRAMMING LANGUAGE II. Lecture 1: Introduction to classes

Administration. Classes. Objects Part II. Agenda. Review: Object References. Object Aliases. CS 99 Summer 2000 Michael Clarkson Lecture 7

Java and OOP. Part 2 Classes and objects

CS112 Lecture: Defining Classes. 1. To describe the process of defining an instantiable class

Assignment 1 due Monday at 11:59pm

Container Vs. Definition Classes. Container Class

CS 11 C++ track: lecture 1

Operations. I Forgot 9/4/2016 COMPUTER SCIENCE DEPARTMENT PICNIC. If you forgot your IClicker, or your batteries fail during the exam

Java Classes and Objects

Scope. Scope is such an important thing that we ll review what we know about scope now:

Designing and Implementing Classes Topic 6

Lecture 7: Classes and Objects CS2301

CHAPTER 7 OBJECTS AND CLASSES

Transcription:

Introduction to Objects: Semantics and Syntax Defining i an object Creating an object Instance variables Instance methods

What is OOP? Object-oriented programming (constructing software using objects) and procedural-oriented programming g are different programming g styles Its all about decomposing complexity Procedural: use methods for modularization and abstraction OOP: use objects for modularization and abstraction OOP approaches break down problems into program things (objects) that parallel real-world things in the problem domain itself. We construct fields (instance variables) - things that the object knows or represents a property or thing that the object has. Object state! We construct routines (methods) - things that the object can do. name Point Card Boat thing (generally a noun) fields x y suit rank name maxspeed knows (generally nouns) methods() display() show() go() does (generally verbs) UML (Unified modeling language) diagrams 30

Example: TETRIS What are the game s objects? piece, board Capabilities: What do those objects know how to do? piece: be created, fall, rotate, stop at collision board: be created, remove rows, check for end of game Properties: What attributes and components do they have? Piece: orientation, position, shape, color board: size, rows 31

Classes and objects A class is used to describe the name, fields, and methods for an object. A class is not an object. Its more like a blueprint for how to make one and a set of instructions for what it does and how. Each object instance made from a class has the same fields (instance variables), but the values of those fields (state) may differ from object instance to object instance. Analogy: Rolodex A class is like a set of instructions ti for creating a blank Rolodex card. It notes that you need to have an entry for name, phonenumber, etc. When you create an object (using the new operator) you create a blank Rolodex card with those space for that t information. You then set those values for that specific card/object instance. Another card/object instance would have the same type of information, but not necessarily the same values 32

Creating/using an object It generally takes two classes to develop an object: One to create the object One to test/use the object (this one has main()). class PointTest { class Point { public static void main (String[] args) { int x; Point p1 = new Point(); int y; p1.x = 5; p1.display(); void display() { } // end method main System.out.println( ( + x +, + y + ) ); } // end class PointTest } // end method display } // end class Point Instance variables are declared inside a class, but not within a method. These fields define state and last for as long as the object lives! Local variables are declared within a method and are not persistent 33

UML for containment (HAS-A) A) class PointTest { class Point { public static void main (String[] args) { int x; Point p1 = new Point(); int y; p1.x = 5; p1.display(); void display() { } // end method main System.out.println( ( + x +, + y + ) ); } // end class PointTest } // end method display } // end class Point PointTest main (args : String[]) : void PointTest HAS-A Point Point is contained by PointTest Point x : int y : int display() : void 34

Procedural Vs. OO programming Consider a program that uses a deck of cards. Write a procedural program to implement a random draw of a card Write the description of objects to do the same thing Now use your implementation to play blackjack You now need to draw two cards Or more (on a hit) What if there are six decks of cards in the shoe? How does that change your procedural program? How does that change your object-oriented program? OOP mantra: Flexibility and Extendibility. Specs ALWAYS change Code should easy to reuse in future projects 35

Objects and Reference variables In your prerequisite coverage, you saw: Primitive data types (int, boolean, double, etc.) Derived data types (String, Scanner, etc). There is actually no such thing as an object variable, all non-primitive data types are actually the same thing, a reference variable. Rf Reference variables ibl hold hldbits that thtrepresent a way to access an object Where are the objects variables in memory? Where do I go in memory to find the instructions to run an object s methods? It doesn t hold the object itself, just a pointer, or address, or reference to the object. Steps of Object Creation Step 1: Declare a reference variable Point p1; Step 2: Create an object new Point(); Step 3: Store the new object s reference in the variable (link the object) p1 = new Point(); p1 1 2 3 Pit Point object 36

Strings are objects String is a derived data type (an object) String is such a useful data type that it is built into the language (java.lang) String has its own context for + Concatenate String objects are built automatically (without the need to invoke new). String objects have methods like length(), tolowercase(), and charat() String message1 = Hello + + World + \n ; String message2 = Goodbye ; char letter = message2.charat(0); Primitive data types store values int x = 10; int y = 5; message1 x 10 y 5 x = y; x 5 y 5 Objects use references/pointers message2 message1 message2 Hello World\n Goodbye message1 = message2; Hello World\n Goodbye 37

Arrays are objects A variable can be assigned to any object of the appropriate type. // Create an array referenced numbers. int[] numbers = new int[10]; int[] digits = new int[500]; numbers = digits; numbers reference digits reference Both variables now reference the same object (NOT a copy) If an object has no references then object then the object is lost it is garbage and will be collected 38

Life and death on the heap The default value of a newly declared reference variable is null. It doesn t point at anything until you store a reference in it All new objects exist in an area of controlled memory called the heap. As long as an object has an active reference then it is reachable. An object that does not have an active reference is abandoned In Java, abandoned objects are eligible for garbage collection (automatic destruction) In C++, objects must be explicitly destroyed by the program. Objects that are abandoned can never be destroyed and cause a memory leak. Point p1; At the end of this code segment: Point p2 = new Point(); - How many active references exist? p1 = p2; - How many reachable objects? p2 = new Point(); - How many abandoned objects? p1 = null; Consider drawing a diagram 39

Passing objects as method arguments Object references can be passed to methods like any data type. int[] numbers = {5,10,15};, printlist(numbers); numbers list reference reference void printlist(int[] list){ for (int i = 0; i < list.length; i++) System.out.print(list[i] + " "); } } // end method printlist void clearlist(int[] list){ for (int i = 0; i < list.length; i++) list[i] = 0; } } // end method clearlist What does this call by reference imply pyto changes made in clearlist()? 40

Arrays of objects Array elements can contain primitive data types or references to objects. We ve seen this so far as arrays of String String[] namelist = { Sam, Bobbie, Pat, Kim, Teri }; String Sam namelist reference namelist[0] namelist[1] namelist[2] namelist[3] reference reference reference reference String String String Bobbie namelist[4] reference String Teri An array of references can be made of any object type Point[] pointlist = {new Point(), new Point()}; Pat Kim 41

Style: Javadoc method headers /** * gofish: Searches a hand for all cards of a named rank * @param rank The rank of card to be searched. Must * be passed as a String in the form Ace, King,, Two * @param hand The hand of cards to be searched, found cards * will be removed! * @return All cards contained in hand that have the * specified rank (may be empty) */ ArrayList<Card> gofish(string rank, ArrayList<Card> hand) { ArrayList<Card> foundcards = new ArrayList<Card>(); for (Card thecard : hand) { if ( thecard.rank.equals(rank) ) { foundcards.add(thecard); hand.remove(thecard); } // end if found card of correct rank } // end for each card in hand return foundcards; } // end method gofish 42

Static: Class variables Point p1 = new Point(); class Point { p1.x = 7; static int numpoints = 0; p1.y = 8; int x; Point.numPoints++; int y; Point p2 = new Point(); p2.x = 3; } // end class Point p2.y = 1; p2.numpoints++; System.out.println(p1.numPoints); p1 x 7 Static variables: y 8 a single memory location shared by all instances of the class one value per class, instead of one value per instance can be accessed w/o an instance x y 3 1 numpoints p1.numpoints++ Vs. p1.x++ Vs. Point.x++ Vs. Point.numPoints++ p2 2 43

Static methods Static methods can run without any instance of the class Behavior is not dependent on the state of an object instance Thus they may be called without any instance variables actually exist Therefore, static methods may NEVER use instance variables Static methods can only use static variables Static methods of a class cannot use regular (non-static) methods of a class! Style: Static methods and variables should be called using the class name rather than an object reference variable 44

Example: Calculating the midpoint of a line segment Problem: Develop a method that, given two points returns the midpoint of the line (round to the closest integer coordinates). Recall: A method can only return one value Example: Test that t given the point (3,3) 3) and the point (5,5) 5) that t your method returns the point (4,4) Note: It is often a good idea to come up with the test code first. This is one of the fundamental principles of extreme programming. 45

Introduction to Objects: Style accessors/mutators access modifiers encapsulation keyword: this standard methods in java constructors

Example: CanvasPoint Say we want to use our Point class to keep track of things on a graphics canvas of a specific standard size <- y -> 0 0 <- x -> 800 (0,0) (800,0) x y Point display() 600 (0,600) (800,600) What if someone using our CanvasPoint object does this: CanvasPoint point1 = new CanvasPoint(); Point1.x = 1000; We need a way to protect our instance values from misuse from an uninformed user (or an honest mistake) Unfortunately, if someone has access to our variables, they can do pretty much anything that they want to with them. We need a way to FORCE the user to check the values CanvasPoint x y display() 47

Using mutators (set methods) We can create new methods whose only job is to provide checked access to the variables. public void setx(int xvalue) { if (xvalue < 0) { xvalue = 0; } if (xvalue > 800) { xvalue = 800; } x = xvalue; } // end method setx public void sety(int yvalue) { if (yvalue < 0) { yvalue = 0; } if (yvalue > 600) { yvalue = 600; } y = yvalue; } // end method sety These are called a mutator methods. CanvasPoint It changes the state of the object (instance variable value) What if later we decide to use a different sized Canvas? But can we force the user to use the mutator? sety(iyvalue : int) x y display() setx(xvalue : int) 48

Hiding data We can use access specifier to change to is allowed to access an object s variables and methods public: any class can access our fields private: only methods in the class can access its fields public class CanvasPoint { private int x; private int y; -x public void setx (int xvalue) { -y // our code here +display() } public void sety (int yvalue) { // our code here } } // end class CanvasPoint How can other classes look at (and use) the private values of x and y? CanvasPoint +setx(xvalue : int) +sety(yvalue : int) 49

Using accessors (get methods) We create methods whose only job is to access private variables and return the result for use public class CanvasPoint { private int x; private int y; public void setx (int xvalue) public void sety (int yvalue) public int getx () { return x; } public int gety () { return y; } } // end class CanvasPoint -x -y CanvasPoint +display() : void +setx(xvalue : int) : void +sety(yvalue : int) : void +getx() : int +gety() : int These are called accessor methods. 50

Encapsulation This is the most important OO rule for style Hide your data! Do not expose your variables Mark all instance variables private Mark access/mutators (getters/setters) public Always force the use of getters/setters. Never expose your data! Even if you do no checking right now, you may want to later This means less code changing later! -x -y CanvasPoint Well encapsulated! But what is with xvalue and yvalue stuff? +display() : void +setx(xvalue : int) : void +sety(yvalue : int) : void +getx() : int +gety() : int 51

Keyword: this Stylistically, it makes sense to use the logical name for -x the method parameters that you use for the private -y instance variables. How do we deal with the namespace collision? Note: Overloading variables names is a feature! +gety() : int CanvasPoint +display() : void +setx(x : int) : void +sety(y : int) : void +getx() : int public class CanvasPoint { private x; public void setx (int x) { // code to check valid x x = x; // semantic error! } } // end class CanvasPoint public class CanvasPoint { private x; public void setx (int x) { //code to check valid x this.x = x; } } // end class CanvasPoint The keyword this refers to the current object. 52

Making your objects behave like library objects What happens when you do this? int s = 5; System.out.println(s); Scanner s = new Scanner(); System.out.println(s); Why? String s = Hello Class ; System.out.println(s); CanvasPoint s = new CanvasPoint(); System.out.println(s); Print methods call the tostring() method on your object If you don t have one, it just prints the value stored in the reference variable (more on this later) What other methods should all new objects define? -x -y CanvasPoint +display() : void +setx(x : int) : void +sety(y : int) : void +getx() : int +gety() : int 53

Standard methods public class CanvasPoint { -x public String tostring() { -y return ( + x +, + y + ) ; } // end method tostring public boolean equals (CanvasPoint obj) { return ( (x == obj.getx()) && (y == obj.gety()) ); } // end method equals public CanvasPoint clone () { CanvasPoint cloneobject; cloneobject = new CanvasPoint(); cloneobject.setx(x); cloneobject.sety(y); return cloneobject; } // end method clone } // end class CanvasPoint CanvasPoint +display() : void +setx(x : int) : void +sety(y : int) : void +getx() : int +gety() : int +tostring() : String +equals(obj : CanvasPoint) : boolean +clone() : CanvasPoint 54

Constructors Often there is code that we need/want to run every -x time we create a new object -y CanvasPoint p1 = new CanvasPoint(); p1.setx(x); p1.sety(y); Constructor: a method automatically called when an object is created public CanvasPoint (int x, int y) { this.setx(x); this.sety(y); } // end constructor CanvasPoint p1 = new CanvasPoint(x,y); CanvasPoint +CanvasPoint( x: int, y : int ) +display() : void +setx(x : int) : void +sety(y : int) : void +getx() : int +gety() : int +tostring() : String +equals(obj : CanvasPoint) : boolean +clone() : CanvasPoint 55

Default/Multiple Constructors public CanvasPoint () {} // no-arg public CanvasPoint (int x, int y) { this.setx(x); this.sety(y); } // end constructor Constructors have no return type and must have the same name as the class The default constructor is a no-arg constructor The compiler adds this for you if you do not define you own constructor for the class If you do define a constructor then the compiler does not add this for you. You can overload the constructor method Multiple constructors Re-add a default constructor if you d like -x -y CanvasPoint +CanvasPoint() +CanvasPoint( x: int, y : int ) +display() : void +setx(x : int) : void +sety(y : int) : void +getx() : int +gety() : int +tostring() : String +equals(obj : CanvasPoint) : boolean +clone() : CanvasPoint Discussion topic: Should programming languages provide destructors? 56