How Objects Behave Chapter 4.

Slides:



Advertisements
Similar presentations
Lecture 9: More on objects, classes, strings discuss hw3 assign hw4 default values for variables scope of variables and shadowing null reference and NullPointerException.
Advertisements

1 Classes, Encapsulation, Methods and Constructors (Continued) Class definitions Instance data Encapsulation and Java modifiers Method declaration and.
H OW O BJECTS B EHAVE. O VERVIEW Methods use object state Arguments and return types in methods Java passes by value Getters and Setters Encapsulation.
Road Map Introduction to object oriented programming. Classes
Chapter 41 Defining Classes and Methods Chapter 4.
Chapter 3 Using Classes and Objects. 2 Creating Objects  A variable holds either a primitive type or a reference to an object  A class name can be used.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 4 Defining Your Own Classes.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 4 Defining Your Own Classes.
Writing Classes (Chapter 4)
Chapter 5 - Writing a Problem Domain Class Definition1 Chapter 5 Writing a Problem Domain Class Definition.
CSE 332: C++ templates This Week C++ Templates –Another form of polymorphism (interface based) –Let you plug different types into reusable code Assigned.
BPJ444: Business Programming Using Java Classes and Objects Tim McKenna
CSC 212 – Data Structures Lecture 12: Java Review.
Chapter 8 Script-free pages. Problem with scripting in JSP When you use scripting (declaration, scriplet, expressions) in your JSP, you actually put Java.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java: Early Objects Third Edition by Tony Gaddis Chapter.
More with Methods (parameters, reference vs. value, array processing) Corresponds with Chapters 5 and 6.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 2.
P Chapter 2 introduces Object Oriented Programming. p OOP is a relatively new approach to programming which supports the creation of new data types and.
Peyman Dodangeh Sharif University of Technology Fall 2013.
ECE122 Feb. 22, Any question on Vehicle sample code?
Sadegh Aliakbary Sharif University of Technology Spring 2011.
Chapter 4 Writing Classes Part 2. © 2004 Pearson Addison-Wesley. All rights reserved4-2 Classes A class can contain data declarations and method declarations.
Java™ How to Program, 10/e © Copyright by Pearson Education, Inc. All Rights Reserved.
An Object-Oriented Approach to Programming Logic and Design Chapter 3 Using Methods and Parameters.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 4 Defining Your Own Classes.
CSC1401 Classes - 2. Learning Goals Computing concepts Adding a method To show the pictures in the slide show Creating accessors and modifiers That protect.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
Working With Objects Tonga Institute of Higher Education.
Peyman Dodangeh Sharif University of Technology Spring 2014.
Copyright © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 3 A First Look at Classes and Objects.
Java Programming, Second Edition Chapter Three Using Methods, Classes, and Objects.
Topics Instance variables, set and get methods Encapsulation
Comp1004: Building Better Objects II Encapsulation and Constructors.
Objects and Memory Mehdi Einali Advanced Programming in Java 1.
Methods, classes, and Objects Dr. Jim Burns. Question  Which of the following access modifiers is the default modifier?  public  private  protected.
Copyright © 2012 Pearson Education, Inc. Chapter 4 Writing Classes : Review Java Software Solutions Foundations of Program Design Seventh Edition John.
Copyright © 2011 Pearson Education, Inc. Starting Out with Java: Early Objects Fourth Edition by Tony Gaddis Chapter 3: A First Look at Classes and Objects.
Objects as a programming concept
Programming Logic and Design Seventh Edition
3 Introduction to Classes and Objects.
Introduction to the C programming language
Yanal Alahmad Java Workshop Yanal Alahmad
Chapter 7 Top-Down Development
Chapter 3: Using Methods, Classes, and Objects
Creating Your OwnClasses
HKCT Java OOP Unit 02 Object Oriented Programming in Java Unit 02 Methods, Classes, and Objects 1.
Methods The real power of an object-oriented programming language takes place when you start to manipulate objects. A method defines an action that allows.
Subroutines Idea: useful code can be saved and re-used, with different data values Example: Our function to find the largest element of an array might.
Lesson 2: Building Blocks of Programming
Chapter 3 Introduction to Classes, Objects Methods and Strings
Chapter 3 Introduction to Classes, Objects Methods and Strings
Defining Classes and Methods
Chapter 4: Writing classes
6 Chapter Functions.
Encapsulation and Constructors
Classes, Encapsulation, Methods and Constructors (Continued)
Static Class Members March 29, 2006 ComS 207: Programming I (in Java)
Namespaces, Scopes, Access privileges
Chapter 6 – Methods Topics are:
Object Oriented Programming in java
Java Programming Language
Agenda Warmup Lesson 2.2 (parameters, etc)
Barb Ericson Georgia Institute of Technology Oct 2005
Methods/Functions.
Object-Oriented Design AND CLASS PROPERTIES
Object-Oriented Design AND CLASS PROPERTIES
Day 11 The Last Week!.
Object-Oriented Design AND CLASS PROPERTIES
Presentation transcript:

How Objects Behave Chapter 4

Methods Up until this point, our methods haven’t done much – displaying methods We know how to instantiate objects and how to call them The power is in Passing the method’s data = giving those methods data and they can react based on the information.

Page 73 Dog Class and Dog Test Drive Bark Method –decision-making we’re familiar with – conditional stmts based on size Dog TestDrive – new Object – one.size = 70 Two.size = 8; three.size = 35 Call the method Bark – result is different based on size Each object has a bark method Instance variables for each method are different

You can pass values (send things) to a Method Page 74 – Very Important Concept Up until this point a method name has had blank parentheses: Example d.bark() Specify the type of data you pass it in () A method uses parameters - what is req’d A caller passes arguments d.bark(3) Look at example – bottom of page 74

You can get things back from a method Bark method Example on p. 74 doesn’t return anything Void means the method does not return a value; it just does something. Methods can return values Must put the data type of the value you are sending back

Example int theSecret = life.giveSecret() int giveSecret () { return 42; } Data type that you are sending back must declare a data type; what you say you’ll give back you better give back

Returning Numbers Sometimes methods need to do calculations; Sometimes need to figure something out or come up with an answer The method then needs to “send it back”

Page 76 – Sending things back You can send more than one thing to a method Look at example – page 76 Methods can have multiple parameters Separate with commas Separate the arguments with commas You can pass variables into a method, as long as the variable type matches the parameter type. Example page 76 at bottom t. takeTwo(12, 34) 12 and 34 are passed into x and y Value of foo and value of bar become x and y

Page 77 Java is pass-by-value That means pass-by-copy Big idea – you can pass one, two …. Values separated by commas Most of the time we will be passing variables – the value of the variable to the method Also, you are passing a copy of the method; original stays the same.

Page 82 Encapsulating the GoodDog class Want to make sure that the user is forced to use an accurate value for the class Instance variable called size of type integer GetSize and SetSize When we create new objects and we want to assign our object a size, we have to call the SetSize method.

Encapsulation Getter and Setter Methods Allows you to protect methods so that numbers that are out of range, etc. don’t get entered in. You accomplish this by making the instance variable private, rather than public Variable is only available within the class Getter and setter methods are public

New object One.setSize(70) one based on GoodDog class One.setSize(70) 70 passed into the setSize method We can put checks and balances into the program to be sure that variables are assigned correctly

GetSize Method GetSize – must be changed to the data type that we are returning. Way to make sure other programmers use the classes appropriately. Responsible for returning a data type that we’re returning One.GetSize A failsafe to be sure that other programmers use your methods appropriately.

Private methods - encapsulating Private means that this variable size is private to this class Things inside this class – methods – are the only ones that have access to the variable within the class Getter and setter methods will be used to interact You can only access using the getter & setter

Instance Variables & Local Variables Important to know what they’re set to and how they react Page 84-85 Variable declarations need a name and a type You can initialize and assign a value. What is the value of the variable before you assign a value? Explanation page 84 Number primitives value = 0; boolean’s value =false; object reference variables value null.

Page 85 Difference between instance variables and local variables Instance variables – declared inside a class, but NOT within a method Local variables – declared within a method Local variables must be initialized before you use them. Example – page 85