4-Methods Dr. John P. Abraham Professor UTPA. Common ways of packaging code Properties Methods Classes Namespaces (related classes are grouped into a.

Slides:



Advertisements
Similar presentations
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.
Advertisements

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.
 2006 Pearson Education, Inc. All rights reserved Functions.
Chapter 5 Functions.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 5 Function Basics.
Math class methods & User defined methods Introduction to Computers and Programming in JAVA: V
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 5 - Functions Outline 5.1Introduction 5.2Program.
FunctionsFunctions Systems Programming. Systems Programming: Functions 2 Functions   Simple Function Example   Function Prototype and Declaration.
 2007 Pearson Education, Inc. All rights reserved C Functions.
Review for Midterm 2 Nested loops & Math class methods & User defined methods.
Chapter 7 Methods: A Deeper Look Visual C# 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.
FunctionsFunctions Systems Programming Concepts. Functions   Simple Function Example   Function Prototype and Declaration   Math Library Functions.
Differences between C# and C++ Dr. Catherine Stringfellow Dr. Stewart Carpenter.
Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java.
Methods Chapter 6. 2 Program Modules in Java What we call "functions" in C++ are called "methods" in Java Purpose Reuse code Modularize the program This.
CSCI 3327 Visual Basic Chapter 6: Methods: A Deeper Look UTPA – Fall 2011.
Dale Roberts Procedural Programming using Java Dale Roberts, Lecturer Computer Science, IUPUI Department of Computer and.
More with Methods (parameters, reference vs. value, array processing) Corresponds with Chapters 5 and 6.
CSE 1302 Lecture 7 Object Oriented Programming Review Richard Gesick.
 2008 Pearson Education, Inc. All rights reserved Function Call Stack and Activation Records Data structure: collection of related data items.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. C How To Program - 4th edition Deitels Class 05 University.
Chapter 06 (Part I) Functions and an Introduction to Recursion.
* * 0 Chapter 6 Java Methods. * * 0 Method Syntax [access specifier][qualifier] return type method name(argument list) Access specifier public – method.
1 Introduction Modules  Most computer programs solve much larger problem than the examples in last sessions.  The problem is more manageable and easy.
 2005 Pearson Education, Inc. All rights reserved. 1 Methods Called functions or procedures in other languages Modularize programs by separating its tasks.
Iterative Constructs Review l What are named constants? Why are they needed? l What is a block? What is special about declaring a variable inside a block?
 2005 Pearson Education, Inc. All rights reserved Methods: A Deeper Look.
Java™ How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
Android How to Program, 2/e © Copyright by Pearson Education, Inc. All Rights Reserved.
Part II © Copyright by Pearson Education, Inc. All Rights Reserved.
CMSC 1041 Functions II Functions that return a value.
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 6.
1 Announcements Note from admins: Edit.cshrc.solaris instead of.tcshrc Note from admins: Do not use delta.ece.
 Pearson Education, Inc. All rights reserved Methods: A Deeper Look.
Methods: A Deeper Look. Template for Class Definition public class { } A.Import Statement B.Class Comments C.Class Name D.Data members E.Methods (inc.
Chapters 2 & 3. .NET Software development model that allows applications created in disparate programming languages to communicate Universal data access.
Java™ How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
Java™ How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
CSCI 3328 Object Oriented Programming in C# Chapter 6: Methods 1 Xiang Lian The University of Texas Rio Grande Valley Edinburg, TX 78539
1. 2 Framework Classes and libraries: 3.
Review for Nested loops & Math class methods & User defined methods.
Creating and Using Class Methods. Definition Class Object.
Methods Chapter 6. 2 Program Modules in Java What we call "functions" in C++ are called "___________________" in Java Purpose –Reuse code –Modularize.
Java How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
 Static  Example for Static Field  Example for Static Method  Math class methods  Casting  Scope of Declaration  Method Overloading  Constructor.
(C) 2010 Pearson Education, Inc. All rights reserved.  Best way to develop and maintain a large program is to construct it from small, simple pieces,
(C) 2010 Pearson Education, Inc. All rights reserved. Java How to Program, 8/e.
A DVANCED P ROGRAMMING C HAPTER 7: M ETHODS : A D EEPER L OOK Dr Shahriar Bijani Spring 2016.
1 This week Basics of functions Stack frames Stack vs. Heap (brief intro) Calling conventions Storage classes vs. scope Library functions Overloading.
 Pearson Education, Inc. All rights reserved Methods: A Deeper Look.
Mark Fontenot CSE Honors Principles of Computer Science I Note Set 6.
Functions.
Java Primer 1: Types, Classes and Operators
Methods Chapter 6.
Functions, Part 2 of 2 Topics Functions That Return a Value
Chapter 5 Function Basics
Programming Fundamentals Lecture #7 Functions
C-language Lecture By B.S.S.Tejesh, S.Neeraja Asst.Prof.
6 Functions.
Formatted and Unformatted Input/Output Functions
Chapter 6 Methods: A Deeper Look
Chapter 5 - Functions Outline 5.1 Introduction
Advanced Programming Chapter 7: Methods: A Deeper Look
Chapter 5 Function Basics
MSIS 655 Advanced Business Applications Programming
CSCI 3327 Visual Basic Chapter 6: Methods: A Deeper Look
CSCI 3328 Object Oriented Programming in C# Chapter 6: Methods
6 Methods: A Deeper Look.
Java Programming Language
Corresponds with Chapter 5
Presentation transcript:

4-Methods Dr. John P. Abraham Professor UTPA

Common ways of packaging code Properties Methods Classes Namespaces (related classes are grouped into a namespace) Prepackaged codes are available in.NET framework class library

Class Math From namespace System Provides a collection of methods for mathematics Console.Writeline(Math.sqrt(900.0)) Abs(x) Ceiling(x) rounds x to the smallest integer, greater than x. Int32 numofGalons = Convert.ToInt16(Math.Ceiling(totalSqFt/450)); Floor(x) rounds x to the largest integer, less than or equal to x. Pow(x,y) x raised to the power of y

Example of floor and ceiling Floor: Floor(2.10) = 2 Floor(2.00) = 2 Floor(1.90) = 1 Floor(1.80) = 1 Ceiling: Ceiling(0.00) = 0 Ceiling(0.10) = 1 Ceiling(0.20) = 1 Ceiling(0.30) = 1

Main declared as static Why must Main be declared static? It is the applications entry point. Declaring main as static allows the execution environment to invoke main without creating an instance of the class – Public static void Main (string args[]) – The args allows one to call the application and pass parameters in from command line.

Declaring Methods with Multiple Parameters Parameters are specified as a comma- separated list. Actual parameters appear in the call and Formal parameters appear in the method declaration. Both actual and formal parameters must have exactly same number of parameters Both actual and formal parameters must agree in their types. Values are assigned according to the order of appearance rather than variable names.

Method-call stack and activation records When an application calls amethod the return address of the calling method is pushed on to the program-execution stack (method-call stack). If series of methods are called, successive return addresses are pushed onto the stack (LIFO). The program-execution stack also contains the memory for the local variables used in each invocation of a method during the application’s execution. This is known as the activation record (stack frame) of the method call. When the method completes and return to its caller, the activation record for this method call is popped off the stack, and those local variables are no longer known to the application. If more methods calls occur than available memory for program-execution stack, stack overflow error occurs.

Argument Promotion and Casting Math.Sqrt(4) – Expects a double argument. However, accepts an integer and promotes it to a double and returns the result of 2.0 When a method expects an integer, if you pass a real number, you get an error, since the method can’t truncate numbers arbitrarily. Float can be promoted double, but double can’t be converted to float implicitly. If you want to go the other way you have cast it.

The.NET framework library Some examples – System.Windows.Forms – System.Windows.Controls (for WPFs) – System.Linq (for language integrated querry) – System.IO (for files, keyboards, monitors, etc) – System.Web (creating web applications) – System.Text (manipulate characters and strings)

Random Number generation Needed for your card game assignment coming up later. Objects of class Random can produce random byte, int and double values. – Random randomnumbers = new Random(); – int randomValue = randomnumbers.Next(); Generates random values in the range 0 to +2,147,483,646 This uses the current time as seed value to generate a psuedorandom number. If you place number.NEXT(1,53), it will generate a random value 1 to 52. – This can lead to a repeating exact same sequence. To avoid that you can use a seed value – Random randomnumbers = new Random(seedvalue);

Method overloading Methods of the same name can be declared in the same class, as long as they have different set of parameters(number, type and order). Examples: Public int Square (int intValue) {Return intvalue*intvalue;} Public double Square (double doubleValue) {return doubleValue*doubleValue;}

Recursion Run some examples here.

Recursion Method that calls itself. A recursive method solves only a simple problem (base case). Any thing other than the base case it calls itself with a slightly simpler problem. Eventually it becomes the base case for which it knows the answer. Run some examples here.

Passing Arguments: value and reference Applying the ref keyword to a parameter declaration allows you pass a variable to a method by reference. The method now will be able to modify the original variable. The original variable must be initialized before passing to the method. If you do not want to initialize the variable, you can use the modifier out. This means that the called method will assign a value to the original variable. If not a compiler error will result.