Writing Methods Mrs. C. Furman October 9, 2008. Drawing a Square World worldObj = new World(); Turtle turtle1 = new Turtle(100, 100, worldObj); turtle1.forward.

Slides:



Advertisements
Similar presentations
Chapter 4 Constructors and Destructors. Objectives Constructors – introduction and features The zero-argument constructor Parameterized constructors Creating.
Advertisements

Chapter 6 Advanced Function Features Pass by Value Pass by Reference Const parameters Overloaded functions.
Function in Notepad def drawSquare(myTurtle): myTurtle.forward(100) myTurtle.right(90) # side 1 myTurtle.forward(100) myTurtle.right(90) # side 2 myTurtle.forward(100)
Review What is a virtual function? What can be achieved with virtual functions? How to define a pure virtual function? What is an abstract class? Can a.
By Now, 1. Your Student ID should open the classroom door 2. You should have C:\100 folder Desktop shortcuts to Command Prompt and Notepad 3. You should.
A C LOSER L OOK AT C LASSES 1. A SSIGNING O BJECTS One object can be assigned to another provided that both objects are of the same type. It is not sufficient.
TOPIC 13 OBJECTS IN MEMORY, PASSING PARAMETERS 1 Notes adapted from Introduction to Computing and Programming with Java: A Multimedia Approach by M. Guzdial.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 7: User-Defined Functions II.
Passing information through Parameters ( our setter methods or mutators) Formal Parameter – parameter in the method. public void SetA(int x){ (int x) is.
Methods. int month; int year class Month Defining Classes A class contains data declarations (static and instance variables) and method declarations (behaviors)
1 Chapter Three Using Methods. 2 Objectives Learn how to write methods with no arguments and no return value Learn about implementation hiding and how.
Chapter 5 Functions.
Chapter 4 Methods F Introducing Methods –Benefits of methods, Declaring Methods, and Calling Methods F Passing Parameters –Pass by Value F Overloading.
// Functions that take no arguments #include using namespace std; void function1(); void function2( void ); int main() { function1(); function2(); return.
COMP 14 Introduction to Programming Miguel A. Otaduy May 25, 2004.
CS-212 Classes (cont) Dick Steflik. Member functions Member functions of a class include – getters used to retrieve state data – setters used to set state.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 8, 2005.
Lecture 4. Review (Attributes and Methods) Class will contain –a set of attributes and methods public class Turtle { ///// Field (Attributes) ///// …
Chapter 4 Methods F Introducing Methods –Benefits of methods, Declaring Methods, and Calling Methods F Passing Parameters –Pass by Value F Overloading.
Chapter 6: Function. Scope of Variable A scope is a region of the program and broadly speaking there are three places, where variables can be declared:
TOPIC 3 INTRODUCTION TO PROGRAMMING 1 Notes adapted from Introduction to Computing and Programming with Java: A Multimedia Approach by M. Guzdial and B.
04-Intro-Object-Oriented-In-Java1 Barb Ericson Georgia Institute of Technology Sept 2009 Introduction to Object-Oriented Programming in Java.
Introduction to Programming Writing Java Beginning Java Programs.
TOPIC 3 INTRODUCTION TO PROGRAMMING 1 Notes adapted from Introduction to Computing and Programming with Java: A Multimedia Approach by M. Guzdial and B.
Methods in Java CSC1401. Overview In this session, we are going to see how to create class-level methods in Java.
CSE8A Lecture3 TODO: –Finish PSA1 individually (no partner!) and turn it in with the bundlePSA1 command GET AN INTERVIEW for PSA1 from a tutor See tutor.
Chapter 3: Developing Class Methods Object-Oriented Program Development Using Java: A Class-Centered Approach.
Methods and Parameters Chapter 5 How to split large programs into isolated sections. Focus on one part of the problem – which can be “called” on or “invoked”
ADTs and C++ Classes Classes and Members Constructors The header file and the implementation file Classes and Parameters Operator Overloading.
Functions CIS Feb-06. Summary Slide Using Functions Mathematical Functions Misc. Functions Naming Conventions Writing Functions –Function Prototype.
Functions Top-down design Breaking a complex problem into smaller parts that we can understand is a common practice. The process of subdividing a problem.
Inheritance Chapter 10 Programs built from objects/instances of classes An O.O. approach – build on earlier work. Use classes in library and ones you have.
Built-In and user-Defined functions Software Design Concepts Lecture IV Dr. Sothy Vignarajah.
Introduction to Programming Writing Java Beginning Java Programs.
Functions in C CSE 2451 Rong Shi. Functions Why use functions? – Reusability Same operation, different data – Abstraction Only need to know how to call.
CSC 143F 1 CSC 143 Constructors Revisited. CSC 143F 2 Constructors In C++, the constructor is a special function automatically called when a class instance.
C++ Programming Lecture 13 Functions – Part V The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
Georgia Institute of Technology Barb Ericson Georgia Institute of Technology Aug 2006 Introduction to Object-Oriented Programming in Alice and Java.
Chapter 6 Methods Chapter 6 - Methods.
ECE 103 Engineering Programming Chapter 31 C Scopes Herbert G. Mayer, PSU CS Status 8/1/2015 Initial content copied verbatim from ECE 103 material developed.
Chapter 5 Methods 1. Motivations Method : groups statements that perform a function.  Level of abstraction (black box)  Code Reuse – no need to reinvent.
نظام المحاضرات الالكترونينظام المحاضرات الالكتروني Destructors The destructor fulfills the opposite functionality. It is automatically called when an object.
Classes - Intermediate
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.
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 6-1 Learning Objectives  Classes  Constructors  Principles of OOP  Class type member.
Georgia Institute of Technology Barb Ericson Georgia Institute of Technology Sept 2006 Introduction to Object-Oriented Programming in Alice and Java.
C++ Programming Lecture 13 Functions – Part V By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
Georgia Institute of Technology Dr Usman Saeed Assistant Professor Faculty of Computing and Information Technology North Jeddah Branch King Abdulaziz University.
C++ Features Function Overloading Default Functions arguments Thinking about objects – relationship to classes Types of member functions Constructor and.
Constructors and Destructors
Chapter 5 Functions DDC 2133 Programming II.
Introduction to Object-Oriented Programming in Java
C Passing arrays to a Function
Chapter 5 Function Basics
Contents Introduction to Constructor Characteristics of Constructor
Chapter 4 (part 2).
Group Status Project Status.
Learning Objectives Classes Constructors Principles of OOP
Constructors and Destructors
Teaching Java using Turtles part 3
CS2011 Introduction to Programming I Methods (II)
Introduction to Object-Oriented Programming in Alice and Java
Lecture 03 & 04 Method and Arrays Jaeki Song.
Unit-1 Introduction to Java
ENERGY 211 / CME 211 Lecture 8 October 8, 2008.
Teaching Java using Turtles part 2
Constructors & Destructors
Corresponds with Chapter 5
Chapter 6: Methods CS1: Java Programming Colorado State University
Presentation transcript:

Writing Methods Mrs. C. Furman October 9, 2008

Drawing a Square World worldObj = new World(); Turtle turtle1 = new Turtle(100, 100, worldObj); turtle1.forward (30); turtle1.turnRight(); turtle1.forward (30); turtle1.turnRight(); turtle1.forward (30); turtle1.turnRight(); turtle1.forward (30); turtle1.turnRight();

World worldObj = new World(); Turtle turtle1 = new Turtle(100, 100, worldObj); int sideLength = 30; turtle1.forward (sideLength); turtle1.turnRight(); turtle1.forward (sideLength); turtle1.turnRight(); turtle1.forward (sideLength); turtle1.turnRight(); turtle1.forward (sideLength); turtle1.turnRight();

void drawSquare(int sideLength) { this.forward (sideLength); this.turnRight(); this.forward (sideLength); this.turnRight(); this.forward (sideLength); this.turnRight(); this.forward (sideLength); this.turnRight(); }

this operator We needed to change turtle1 to this, because turtle1 is a specific Turtle object, which is declared in the client class (in this case, the interactions pane). this – a keyword that refers to the calling object. So, turtle1.drawSquare(30); --turtle1 is the calling object, so this gets replaced with turtle1. turtle2.drawSquare (50); --turtle2 is the calling object, so this gets replaced with turtle2.

Scope of a Variable turtle1 is not defined in the drawSquare() method. Variable names are known in a context (area that they apply). Variables declared in the interactions pane are only known in the interactions pane. Variables declared inside methods are only known in the method.

Object Methods methods that work on a particular object. they are implicitly passed a reference to the object the method was invoked on. In this case, when we call drawSquare, we have sideLength sent explicitly (we see it right there in the parameter list), but turtle1 is sent implicitly and can be reference by using this.

Method Overloading Create a drawSquare method that does not take any parameters, and only draws squares with side lengths of 100.

Method OverLoading Cont Now we have: public void drawSquare(int sideLength) and public void drawSquare () How will the compiler know which method to use?

Abstraction The more general we can make a method, the more it can be used in multiple cases. Which one of our methods is more general? We call this generalization, abstraction! Abstraction leads to general solutions that work in lots of situations!

Pass by Value turtle1.drawSquare(20); actual parameter input value argument Each formal parameter (ex: int sideLength), is set to a copy of the actual parameter. This is called pass by value.

Write a drawRectangle method The method should have parameters for length and width.