ITP © Ron Poet Lecture 15 1 Helper Objects Again.

Slides:



Advertisements
Similar presentations
Complete Structure class Date {class Date { private :private : // private data and functions// private data and functions public :public : // public data.
Advertisements

ITP © Ron Poet Lecture 4 1 Program Development. ITP © Ron Poet Lecture 4 2 Preparation Cannot just start programming, must prepare first. Decide what.
Topic 10 Java Memory Management. 1-2 Memory Allocation in Java When a program is being executed, separate areas of memory are allocated for each class.
Written by: Dr. JJ Shepherd
Helper Methods ITP © Ron Poet Lecture 11.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Software Development Software Life Cycle UML Diagrams.
Lecture 1: Intro to Computers Yoni Fridman 6/28/01 6/28/01.
Lecture 12: Using Classes Yoni Fridman 7/19/01 7/19/01.
Definition Class In C++, the class is the construct primarily used to create objects.
Macro & Function. Function consumes more time When a function is called, the copy of the arguments are passed to the parameters in the function. After.
Copyright 2010 by Pearson Education Building Java Programs Chapter 8 Lecture 8-2: Object Behavior (Methods) and Constructors reading:
ITP © Ron Poet Lecture 2 1 Mental Models. ITP © Ron Poet Lecture 2 2 Mental Models  A mental model is a way of making sense of something we experience.
Data Objects (revisited) Recall that values are stored in data objects, and that each data object holds one value of a particular type. Data objects may.
ITP © Ron Poet Lecture 13 1 Helper Objects. ITP © Ron Poet Lecture 13 2 Console Helper Object  We have used Console objects for a while, let’s see what.
Lists and More About Strings CS303E: Elements of Computers and Programming.
Java Classes Using Java Classes Introduction to UML.
CSC Programming I Lecture 8 September 9, 2002.
A way to pull together related data A Shape Class would contain the following data: x, y, height, width Then associate methods with that data A Shape.
1 JAVA & MEMORY What did I just say this topic was about ?
Local Variables Garbage collection. © 2006 Pearson EducationParameters2 of 10 Using Accessors and Mutators Together What if you want to get a property.
Agenda Review C++ Library Functions Review User Input Making your own functions Exam #1 Next Week Reading: Chapter 3.
C Functions Pepper. Objectives Create functions Function prototypes Parameters – Pass by value or reference – Sending a reference Return values Math functions.
Defining Classes II. Today’s topics  Static methods  Static variables  Wrapper classes  References  Class parameters  Copy constructor.
Lecture 3 Classes, Structs, Enums Passing by reference and value Arrays.
1 Workin’ with Pointas An exercise in destroying your computer.
 Constructor  Finalize() method  this keyword  Method Overloading  Constructor Overloading  Object As an Argument  Returning Objects.
Classes. Student class We are tasked with creating a class for objects that store data about students. We first want to consider what is needed for the.
Chapter 11 Friends and Overloaded Operators. Introduction to function equal // Date.h #ifndef _DATE_H_ #define _DATE_H_ class CDate { public: CDate();
ITIP © Ron Poet Lecture 14 1 Responsibilities. ITIP © Ron Poet Lecture 14 2 Two Sections of Code  If we ask two people to work together to do a job,
Memory Management in Java Computer Science 3 Gerb Objective: Understand references to composite types in Java.
CMSC 202 Advanced Section Classes and Objects: Object Creation and Constructors.
Written by: Dr. JJ Shepherd
Today’s lecture Review chapter 5 go over exercises.
CIS162AD Constructors Part 2 08_constructors.ppt.
BIT 115: Introduction To Programming Professor: Dr. Baba Kofi Weusijana (say Doc-tor Way-oo-see-jah-nah, Doc-tor, or Bah-bah)
CPSC 233 Tutorial 5 February 9 th /10 th, Java Classes Each Java class contains a set of instance variables and methods Instance Variables: Type.
Functions Dr. Sajib Datta Functions A function is a self-contained unit of program code designed to accomplish a particular task. Some functions.
Internet Computing Module II. Syllabus Creating & Using classes in Java – Methods and Classes – Inheritance – Super Class – Method Overriding – Packages.
CS180 Recitation Chapter 7: Defining Your Own Classes II.
Sections Classes, Objects, and Applications.
Class Definitions and Writing Methods Chapter 3 3/31/16 & 4/4/16.
Sadegh Aliakbary Sharif University of Technology Fall 2010.
CS314 – Section 5 Recitation 9
Java Memory Management
Java Memory Management
Strings and Object References
HKCT Java OOP Unit 02 Object Oriented Programming in Java Unit 02 Methods, Classes, and Objects 1.
Defining Classes II.
Composition Phil Tayco San Jose City College Slide version 1.2
Building Java Programs
Building Java Programs
Writing Functions.
The Assembly Language Level
Introduction of Programming
Which best describes the relationship between classes and objects?
Building Java Programs
CMSC202 Computer Science II for Majors Lecture 07 – Classes and Objects (Continued) Dr. Katherine Gibson Based on slides by Chris Marron at UMBC.
Building Java Programs
Building Java Programs
Building Java Programs
LCC 6310 Computation as an Expressive Medium
Pointers and References
ENERGY 211 / CME 211 Lecture 17 October 29, 2008.
Nate Brunelle Today: Functions again, Scope
References Revisted (Ch 5)
Parameters, Overloading Methods, and Random Garbage
How Classes Work with Memory Diagram
Classes and Objects Object Creation
CMSC 202 Constructors Version 9/10.
Introduction to Pointers
Presentation transcript:

ITP © Ron Poet Lecture 15 1 Helper Objects Again

ITP © Ron Poet Lecture 15 2 A Helper Object Provides Services  A helper object provides services that other bits of code can use.  Like a worker with  A special area of responsibility  A series of tasks they can do.  Each different task is a method.  Each object method needs inputs and produces outputs, just like a helper method.

ITP © Ron Poet Lecture 15 3 MyDate Services  MyDate will do the following things:  Tell you the day ( getDay ).  Tell you the month ( getMonth ).  Tell you year ( getYear ).  Print on a given Console ( print ).  Not all these services will be required in any one program.  But they are there when needed.

ITP © Ron Poet Lecture 15 4 What Info Does MyDate Need  It should know the day, month and year.  Stored as instance variables.  Given initial values by the constructor.  It needs a Console for printing.  Only needed in one method, so not an instance variable.  Provided as a parameter to print.

ITP © Ron Poet Lecture 15 5 Where is the MyDate Code?  All MyDate code must be in a file called MyDate.java  This file should not contain anything else.  Create with New – Class in eclipse.

ITP © Ron Poet Lecture 15 6 Testing MyDate Class  Write a main method in a separate class / file.  Call it Ex1, for example.  Both files are in the same eclipse project.  eclipse will make sure that both files work well together.  The main method should create MyDate objects and call every method.  To make sure they all work.  MyDate will then be ready to use in other programs.

ITP © Ron Poet Lecture 15 7 Office Analogy  When we write the code for MyDate we are writing instructions on how to make a MyDate worker.  A robot worker?  This worker will be able to answer a limited number of questions.  The methods.  We haven’t created any workers yet.

ITP © Ron Poet Lecture 15 8 Creating a Worker  new MyDate will create a worker who understands about a specific date.  The one given in the constructor.  He can then tell us the day, the month or the year whenever we ask him.  He can also print this information.

ITP © Ron Poet Lecture 15 9 Many Worker  If our program needs more than one MyDate worker.  One to know about now.  One to know about dateOfBirth.  We have to create several workers.  We give then different variable names so that we know who is who.

ITP © Ron Poet Lecture Object Parameters

ITP © Ron Poet Lecture An Object as a Parameter  The following program fragment creates a Console object and a MyDate object.  The Console object is passed to the print method of the MyDate object.  What happens to the Console object?

ITP © Ron Poet Lecture Code // in main Console con = new Console(); MyDate d = new MyDate(1, 2, 3); d.print(con); // in MyDate.print public void print(Console c) { c.print(String.format(“%02d”, day));... }

ITP © Ron Poet Lecture Addresses Of Objects  new Console() creates a Console object in special Java object memory.  The memory address of this new object is returned.  Console con =... stores the address of this object so that it can be located when needed.

ITP © Ron Poet Lecture A Copy Of The Address  d.print(con) passes this address as a parameter to the print method.  public void print(Console c) stores this address in the variable c.  There are now two copies of the address.  In con and c.  They are the same address, that of the object created by new.

ITP © Ron Poet Lecture Using The Address  c.print(String.format(“%02d”, day)); uses the address stored in c to find the object and call the print method.  When the MyDate print method is finished, the address c vanishes.  It is a local variable.  The copy of the address in main, stored in con, is still there.  We can use this Console again.

ITP © Ron Poet Lecture Garbage Collection  If all copies of an object’s address are discarded.  The object can’t be used.  The program does not know where it lives.  Java will eventually recognise this.  It will recycle the memory occupied by this object.  So that it can be used by another object.