3. Methods. Programming Paradigms Procedural Programming ‘Imperative’ assignment used to create state, and procedures manipulate state. E.g., C, Assembly,

Slides:



Advertisements
Similar presentations
CSCI 160 Midterm Review Rasanjalee DM.
Advertisements

Programming Languages Marjan Sirjani 2 2. Language Design Issues Design to Run efficiently : early languages Easy to write correctly : new languages.
Lecture 2 Basics of C#. Members of a Class A field is a variable of any type that is declared directly in a class. Fields are members of their containing.
Lecture 2: Object Oriented Programming I
Classes and Objects: Recap A typical Java program creates many objects which interact with one another by sending messages. Through the objects interactions,
For use of IST410 Students only Arrays-1 Arrays. For use of IST410 Students only Arrays-2 Objectives l Declaring arrays l Instantiating arrays l Using.
1 Chapter 4 Language Fundamentals. 2 Identifiers Program parts such as packages, classes, and class members have names, which are formally known as identifiers.
Java Syntax Primitive data types Operators Control statements.
Classes and Objects  A typical Java program creates many objects which interact with one another by sending messages. Through the objects interactions,
Fundamental Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
1 Introduction to C++ Programming Concept Basic C++ C++ Extension from C.
©2004 Brooks/Cole Chapter 10 More on Classes. Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Object assignment With primitive types, setting.
Advanced Java and Android Day 1 Object-Oriented Programming in Java Advanced Java and Android -- Day 11.
1 Chapter 8 Objects and Classes. 2 Motivations After learning the preceding chapters, you are capable of solving many programming problems using selections,
Java Methods By J. W. Rider. Java Methods Modularity Declaring methods –Header, signature, prototype Static Void Local variables –this Return Reentrancy.
Chapter 8 :: Subroutines and Control Abstraction
Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java.
Programming Languages
Introduction to Programming Writing Java Beginning Java Programs.
CSC 212 Object-Oriented Programming and Java Part 1.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
Copyright © Curt Hill Java Looking at our first console application in Eclipse.
APCS Java AB 2004 Review of CS1 and CS2 Review for AP test #1 Sources: 2003 Workshop notes from Chris Nevison (Colgate University) AP Study Guide to go.
Chapter 6 Programming Languages (1) Introduction to CS 1 st Semester, 2015 Sanghyun Park.
ECE122 Feb. 22, Any question on Vehicle sample code?
Procedural programming in Java Methods, parameters and return values.
More About Objects and Methods Chapter 5. Outline Programming with Methods Static Methods and Static Variables Designing Methods Overloading Constructors.
OOP in Java : © W. Milner 2005 : Slide 1 Java and OOP Part 2 – Classes and objects.
C++ Programming Basic Learning Prepared By The Smartpath Information systems
COMPUTER PROGRAMMING. Functions’ review What is a function? A function is a group of statements that is executed when it is called from some point of.
Programs and Classes A program is made up from classes Classes may be grouped into packages A class has two parts static parts exist independently Non-static.
Introduction to Programming Writing Java Beginning Java Programs.
Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java.
C++ / G4MICE Course Session 2 Basic C++ types. Control and Looping Functions in C Function/method signatures and scope.
Classes. Constructor A constructor is a special method whose purpose is to construct and initialize objects. Constructor name must be the same as the.
Method Overloading  Methods of the same name can be declared in the same class for different sets of parameters  As the number, types and order of the.
CSI 3125, Preliminaries, page 1 Compiling the Program.
Computer Numbers Integers (byte, short, int, long) –whole numbers –exact –Relatively limited in magnitude (~10 19 ) Floating Point (float, double) –fractional.
Chapter 3: Developing Class Methods Object-Oriented Program Development Using Java: A Class-Centered Approach.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Method OverloadingtMyn1 Method overloading Methods of the same name can be declared in the same class, as long as they have different sets of parameters.
1-1 1 Introduction  Programming linguistics: concepts and paradigms syntax, semantics, and pragmatics language processors.  Historical development of.
 Static  Example for Static Field  Example for Static Method  Math class methods  Casting  Scope of Declaration  Method Overloading  Constructor.
Modules (Methods) Functions and Procedures Parameters...Output.
Java: Variables and Methods By Joshua Li Created for the allAboutJavaClasses wikispace.
Grouping Data Together Often we want to group together a number of values or objects to be treated in the same way e.g. names of students in a tutorial.
OOP Basics Classes & Methods (c) IDMS/SQL News
JAVA Programming (Session 2) “When you are willing to make sacrifices for a great cause, you will never be alone.” Instructor: รัฐภูมิ เถื่อนถนอม
CSCI 51 Introduction to Programming Dr. Joshua Stough February 24, 2009.
Structure A Data structure is a collection of variable which can be same or different types. You can refer to a structure as a single variable, and to.
Lecture 9: Object and Classes Michael Hsu CSULA. 2 OO Programming Concepts Object-oriented programming (OOP) involves programming using objects. An object.
Memory Management in Java Mr. Gerb Computer Science 4.
User-Written Functions
Examples of Classes & Objects
Java Primer 1: Types, Classes and Operators
Chapter 3: Using Methods, Classes, and Objects
Types of Programming Languages
Programmazione I a.a. 2017/2018.
Methods Additional Topics
Classes & Objects: Examples
CHAPTER 6 GENERAL-PURPOSE METHODS
Programs and Classes A program is made up from classes
Object Oriented Programming in java
Fundamental OOP Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
Java Programming Language
Java Basics Data Types in Java.
In this class, we will cover:
Java Looking at our first console application in Eclipse
Corresponds with Chapter 5
Presentation transcript:

3. Methods

Programming Paradigms Procedural Programming ‘Imperative’ assignment used to create state, and procedures manipulate state. E.g., C, Assembly, Pascal int y; int x = 3; y = manipulateData(x); Functional Programming Functions (procedures that do not depend on outside data) are used to provided data. E.g., Lisp. (defun check-member (input-item input-list) (cond ((null input-list) nil) ((equal input-item (first input-list)) T) (T (check-member input-item (rest input-list)))))

Object-Oriented Programming All data exists in objects; interaction occurs only between objects. Even numbers are objects that know how to add themselves. E.g., SmallTalk: | array | array := Array new: 5. rect := corner: 1 to: array size do: [ :item | rect origin: array at: item put: rect copy ]. Java: Object-oriented, but not 100% OO, since it contains primitives, and tolerates some (hopefully small) degree of procedural programming. Programming Paradigms There are other paradigms, but these three help explain where Java comes from

Java Methods There exists in Java a single construct, the method, for both procedures and functions: when a procedure is called for, specify the return type “void” before method name public void printHelloWorld( ) { System.out.println(“Hello World!”); } // of printHelloWorld Note: All methods must have parentheses for parameters... even if no parameters! Note the comment

Single construct for both procedures and functions: when a function is called for, specify the appropriate return type before method name public float average (float fNum1, float fNum2, float fNum3) { float fReturnVal; fReturnVal = (fNum1 + fNum2 + fNum3)/ 3; return (fReturnVal); } // of average to make a procedure, one might create a method manipulating instance variables. (More on instance later…) Java Methods

Writing Methods: A Larger Look A Java requirement: --All methods belong to an object (or class). --Name of object (or class) must be unambiguous when method called. --To run a program, there must be a class (whose name is the name-of-the-program), containing a special method called main: a class method, not an instance method visible to all nothing returned Method name for command line parameters public static void main (String[ ] argv)

Method Signatures “The signature of a method consists of the name of the method and the number and types of formal parameters to the method. A class may not declare two methods with the same signature, or a compile time error occurs.” --Java Language Specification s Method overloading occurs where identically named methods have subtle variations in the method parameters. public int getCube(int iNum){ return iNum*iNum*iNum; } public int getCube(float fNum){ return (int)(fNum*fNum*fNum); } public int getCube(double dNum){ return (int) (dNum*dNum*dNum); }

Methods: Common Mistakes public float average (float fNum1, float fNum2, float fNum3); { float fReturnVal; fReturnVal = (fNum1 + fNum2 + fNum3)/ 3; return (fReturnVal); } // of average Note ending semicolon -- could be viewed as ‘abstract’ method (more on abstract methods later) -- results in unhelpful error message

Where do Methods Go? Methods (and variables) are contained in Classes, as either class or instance members. More on creating classes and objects shortly...