Defining Classes and Methods

Similar documents
5. Defining Classes and Methods

5. Defining Classes and Methods

Defining Classes and Methods

Objectives. Defining Classes and Methods. Objectives. Class and Method Definitions: Outline 7/13/09

Defining Classes and Methods

Defining Classes and Methods. Objectives. Objectives 6/27/2014. Chapter 5

Defining Classes and Methods

Recitation 02/02/07 Defining Classes and Methods. Chapter 4

EECS168 Exam 3 Review

Chapter 4 Defining Classes I

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

Chapter 4. Defining Classes I

Defining Classes and Methods

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

Defining Classes and Methods

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

COMP 250 Winter 2011 Reading: Java background January 5, 2011

CS 251 Intermediate Programming Methods and Classes

CS 251 Intermediate Programming Methods and More

Dot and Scope Resolution Operator

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

Methods and Data (Savitch, Chapter 5)

Object-Oriented Modeling Using UML. CS151 Chris Pollett Aug. 29, 2005.

Data Structures. Data structures. Data structures. What is a data structure? Simple answer: a collection of data equipped with some operations.

TaxiBot New attributes Variables Math! TaxiBot

Graphical Interface and Application (I3305) Semester: 1 Academic Year: 2017/2018 Dr Antoun Yaacoub

CS112 Lecture: Defining Instantiable Classes

Programming II (CS300)

Programming II (CS300)

CMSC131. Library Classes

Chapter 6 Structures and Classes. GEDB030 Computer Programming for Engineers Fall 2017 Euiseong Seo

APCS Semester #1 Final Exam Practice Problems

Chapter 6 Lab Classes and Objects

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

Chapter 6 Lab Classes and Objects

Chapter 6 Introduction to Defining Classes

Weiss Chapter 1 terminology (parenthesized numbers are page numbers)

Express Yourself. Writing Your Own Classes

Some miscellaneous concepts

CSCI 1301: Introduction to Computing and Programming

Objects and Iterators

Comp 248 Introduction to Programming Chapter 4 - Defining Classes Part A

Module Contact: Dr Geoff McKeown, CMP Copyright of the University of East Anglia Version 2

Creating an object Instance variables

Introduction to Programming Using Java (98-388)

ECE 122. Engineering Problem Solving with Java

Mobile Application Programming. Objective-C Classes

Chapter 3 Objects and Classes

Computer Science II (20073) Week 1: Review and Inheritance

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

AP COMPUTER SCIENCE A

Module Contact: Dr Geoff McKeown, CMP Copyright of the University of East Anglia Version 1

Object Oriented Programming

Ch 7 Designing Java Classes & Class structure. Fields have data values define/describe an instance

Aggregation. Introduction to Computer Science I. Overview (1): Overview (2): CSE 1020 Summer Bill Kapralos. Bill Kapralos.

Polymorphism and Inheritance

(a) Write the signature (visibility, name, parameters, types) of the method(s) required

Chapter 15: Object Oriented Programming

Encapsulation in C++

Review questions. Review questions, cont d. Class Definition. Methods. Class definition: methods. April 1,

AP Computer Science Chapter 10 Implementing and Using Classes Study Guide

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

Reviewing OO Concepts

What will this print?

Part 1: Group Brainstorm (NO computers during this time) Part 2: Submit Individual Brainstorm (You can now use a computer)

Calling and Returning From a Method. Control Flow

Introduction to Classes

Chapter 8. Operator Overloading, Friends, and References. Copyright 2010 Pearson Addison-Wesley. All rights reserved

Introduction to UML and Class Diagrams

CS 170 Java Programming 1. Week 13: Classes, Testing, Debugging

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

Defining Classes and Methods

Chapter 4. Defining Classes I

Ch 7 Designing Java Classes & Class structure. Methods: constructors, getters, setters, other e.g. getfirstname(), setfirstname(), equals()

Java. Classes 3/3/2014. Summary: Chapters 1 to 10. Java (2)

CS 335 Java Programming Controls. Fall 2007

Classes. Classes as Code Libraries. Classes as Data Structures. Classes/Objects/Interfaces (Savitch, Various Chapters)

Inheritance, Polymorphism, and Interfaces

CS-140 Fall Binghamton University. Methods. Sect. 3.3, 8.2. There s a method in my madness.

8. Polymorphism and Inheritance

OBJECT ORIENTED SIMULATION LANGUAGE. OOSimL Reference Manual - Part 1

Computer Science 2 Lecture 4 Inheritance: Trinidad Fruit Stand 02/15/2014 Revision : 1.7

COMP-202 Unit 8: Defining Your Own Classes. CONTENTS: Class Definitions Attributes Methods and Constructors Access Modifiers and Encapsulation

Documenting, Using, and Testing Utility Classes

Methods. Methods. Mysteries Revealed

Defensive Programming

TeenCoder : Java Programming (ISBN )

Introduction to UML and Class Diagrams

CS18000: Programming I

Classes. Classes as Code Libraries. Classes as Data Structures

Object Oriented Programming

CSE115 Introduction to Computer Science I Coding Exercise #7 Retrospective Fall 2017

ECOM 2324 COMPUTER PROGRAMMING II

Understanding class definitions

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

JAVA GUI PROGRAMMING REVISION TOUR III

C++ Review. CptS 223 Advanced Data Structures. Larry Holder School of Electrical Engineering and Computer Science Washington State University

For this section, we will implement a class with only non-static features, that represents a rectangle

Programming II (CS300)

CLASSES AND OBJECTS IN JAVA

Transcription:

Defining Classes and Methods Chapter 5

Objects and References: Outline Variables of a Class Type Defining an equals Method for a Class Boolean-Valued Methods Parameters of a Class Type

Variables of a Class Type All variables are implemented as a memory location Data of primitive type stored in the memory location assigned to the variable Variable of class type contains memory address of object named by the variable

Variables of a Class Type Object itself not stored in the variable Stored elsewhere in memory Variable contains address of where it is stored Address called the reference to the variable A reference type variable holds references (memory addresses) This makes memory management of class types more efficient

Variables of a Class Type Figure 5.5a Behavior of class variables

Variables of a Class Type Figure 5.5b Behavior of class variables

Variables of a Class Type Figure 5.5c Behavior of class variables

Variables of a Class Type Figure 5.5d Behavior of class variables

Variables of a Class Type Figure 5.6a Dangers of using == with objects

Variables of a Class Type Figure 5.6b Dangers of using == with objects

Defining an equals Method As demonstrated by previous figures We cannot use == to compare two objects We must write a method for a given class which will make the comparison as needed View sample code, listing 5.17 class Species The equals for this class method used same way as equals method for String

Demonstrating an equals Method View sample program, listing 5.18 class SpeciesEqualsDemo Note difference in the two comparison methods == versus.equals( ) Sample screen output

Complete Programming Example View sample code, listing 5.19 class Species Figure 5.7 Class Diagram for the class Species in listing 5.19

Boolean-Valued Methods Methods can return a value of type boolean Use a boolean value in the return statement Note method from listing 5.19

Parameters of a Class Type When assignment operator used with objects of class type Only memory address is copied Similar to use of parameter of class type Memory address of actual parameter passed to formal parameter Formal parameter may access public elements of the class Actual parameter thus can be changed by class methods

Programming Example View sample code, listing 5.21 class DemoSpecies Note different parameter types and results View sample program, listing 5.22 Parameters of a class type versus parameters of a primitive type class ParametersDemo

Programming Example Sample screen output

Summary Classes have Instance variables to store data Method definitions to perform actions Instance variables should be private Class needs accessor, mutator methods Methods may be Value returning methods Void methods that do not return a value

Summary Keyword this used within method definition represents invoking object Local variables defined within method definition Formal arguments must match actual parameters with respect to number, order, and data type Formal parameters act like local variables

Parameters of a Class Type When assignment operator used with objects of class type Only memory address is copied Similar to use of parameter of class type Memory address of actual parameter passed to formal parameter Formal parameter may access public elements of the class Actual parameter thus can be changed by class methods

Summary Parameter of primitive type initialized with value of actual parameter Value of actual parameter not altered by method Parameter of class type initialized with address of actual parameter object Value of actual parameter may be altered by method calls A method definition can include call to another method in same or different class

Summary Utility program javadoc creates documentation Class designers use UML notation to describe classes

Summary Operators = and == behave differently with objects of class types (vs. primitive types) Designer of class should include an equals method