Mark Fontenot CSE Honors Principles of Computer Science I Note Set 6.

Slides:



Advertisements
Similar presentations
(C) 2010 Pearson Education, Inc. All rights reserved. Java™ How to Program, 8/e.
Advertisements

Road Map Introduction to object oriented programming. Classes
Lecture 3: Topics If-then-else Operator precedence While loops Static methods Recursion.
Chapter 4 Methods F Introducing Methods –Benefits of methods, Declaring Methods, and Calling Methods F Passing Parameters –Pass by Value F Overloading.
Overloading methods review When is the return statement required? What do the following method headers tell us? public static int max (int a, int b)
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.
Java: How to Program Methods Summary Yingcai Xiao.
FunctionsFunctions Systems Programming. Systems Programming: Functions 2 Functions   Simple Function Example   Function Prototype and Declaration.
Evan Korth New York University Computer Science I Classes and Objects Professor: Evan Korth New York University.
Terms and Rules Professor Evan Korth New York University (All rights reserved)
1 Chapter 5 Methods. 2 Introducing Methods A method is a collection of statements that are grouped together to perform an operation.
FunctionsFunctions Systems Programming Concepts. Functions   Simple Function Example   Function Prototype and Declaration   Math Library Functions.
Comp 248 Introduction to Programming Chapter 4 - Defining Classes Part A Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia.
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.
Dale Roberts Procedural Programming using Java Dale Roberts, Lecturer Computer Science, IUPUI Department of Computer and.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. C How To Program - 4th edition Deitels Class 05 University.
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 2.
* * 0 Chapter 6 Java Methods. * * 0 Method Syntax [access specifier][qualifier] return type method name(argument list) Access specifier public – method.
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 10.
 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.
Introduction to Computers and Programming Lecture 14: User defined methods (cont) Professor: Evan Korth New York University.
4-Methods Dr. John P. Abraham Professor UTPA. Common ways of packaging code Properties Methods Classes Namespaces (related classes are grouped into a.
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 5.
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 6.
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 5 - Functions Outline 5.1Introduction 5.2Program.
 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.
Java™ How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
Object Oriented Programming with Java 03 - Introduction to Classes and Objects.
CSCI 3328 Object Oriented Programming in C# Chapter 6: Methods 1 Xiang Lian The University of Texas Rio Grande Valley Edinburg, TX 78539
Mark Fontenot CSE Honors Principles of Computer Science I Note Set 15.
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.
 Static  Example for Static Field  Example for Static Method  Math class methods  Casting  Scope of Declaration  Method Overloading  Constructor.
Methods.
Part III © Copyright by Pearson Education, Inc. All Rights Reserved.
(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,
Java 5 Class Anatomy. User Defined Classes To this point we’ve been using classes that have been defined in the Java standard class library. Creating.
Spring 2008 Mark Fontenot CSE Honors Principles of Computer Science I Note Set 10.
 Pearson Education, Inc. All rights reserved Methods: A Deeper Look.
Functions + Overloading + Scope
Chapter 9: Value-Returning Functions
Suppose we want to print out the word MISSISSIPPI in big letters.
Department of Computer Science
C Functions Pepper.
Methods Chapter 6.
Iterative Constructs Review
Programming Fundamentals Lecture #7 Functions
Deitel- C:How to Program (5ed)
Lecture 4 D&D Chapter 5 Methods including scope and overloading Date.
Methods and Parameters
Chapter 6 Methods: A Deeper Look
Chapter 5 - Functions Outline 5.1 Introduction
Methods Additional Topics
Overloading and Constructors
Group Status Project Status.
Iterative Constructs Review
CS2011 Introduction to Programming I Methods (II)
CSCI 3328 Object Oriented Programming in C# Chapter 6: Methods
6 Methods: A Deeper Look.
Scope of variables class scopeofvars {
Chapter 5 Methods: A Deeper Look
Classes, Objects and Methods
Peer Instruction 4 Control Loops.
Java Methods: A Deeper Look Academic 2019 Class: BIT23/BCS10 Chapter 06 Abdulaziz Yasin Nageye Faculty of Computing Java How to Program, 10/e 1 © Co py.
Corresponds with Chapter 5
Presentation transcript:

Mark Fontenot CSE Honors Principles of Computer Science I Note Set 6

Note Set 6 Overview Methods – In Depth Return values and the call stack Argument promotion/casting Random Number Generation Scope

Understanding Calls - Stack Data Structure Understanding stacks is useful for understanding return Stacks are LIFO Last In – First Out Last value put in (pushed) is first value removed (pop) When method A calls method B, the system (jvm) must know where to return to in A when B is finished Activation Record – information on local variables in a method

Argument Promotion and Casting Arguments can have different data types from parameters under certain conditions Header for square root public static double sqrt(double a) Expects a double, but can be sent an int. Int would be cast to double System.out.println(sqrt(4)); Expects a double, but can be sent an int. Int would be cast to double System.out.println(sqrt(4));

Valid promotions May get compile time errors if you try to implicitly “demote” a type double to int Use explicit casting in this case square( (int) doubleValuedVariable );

Random Number Generation used in simulation, games, cryptography Use Random Class that is in java.util can generate random boolean, byte, float, double, int, long Produce numbers using mathematical function with VERY large period Random randGenerator = new Random() int randomValue = randGenerator.nextInt(); double doubleRandom = randomGenerator.nextDouble();

Random Ints Produces values from -2,147,483,648 to 2,147,483,647 Limit between 0 and n int x = randGenerator(24) Will produce pseudo sequence of numbers on the range [0, 23]. How would you produce numbers between m and n, m > n and m >= 0?

Random Generator Because it is a function, will produce the same sequence of numbers unless you tell it where to start every time Random randGenerator = new Random() Random randGenerator = new Random( 25 ); Random randGenerator = new Random(???) Need something that changes every time the program is run. Ideas???

Scope of Variables declaration introduces a entity with a name that can be referred to in code. Scope – the portion of the program that can refer to the declared entity by name Scope Rules scope of parameter declaration is the body of the method in which the declaration appears scope of local variable is from the point of declaration to the end of the block scope of variable that appears in the initialization section of a for loop header if for the body of the for statement and other parts of for header scope of a method or field of a class is the entire body of the class

Shadowing Local variable in an instance method with same name as an instance variable shadows the instance variable. public class Test { private int x; public void someMethod() { int x = 5; System.out.println(x); } Will access Local variable x instead of instance variable x Will access Local variable x instead of instance variable x

Method Overloading Overloaded Methods – Methods in the same class that have the same name but different sets of parameters determined by the number, types and order of parameters Differences in return type are irrelevant when determining if 2 methods are overloaded public class X { public void myMethod (int x, int y) { } public int myMethod (float z, float y){ } public class TestX { public static void main (String [] args) { X myVar = new X(); myVar.myMethod(1, 2); myVar.myMethod (3.0, 5.0); }

Example

Overloading Compiler distinguishes methods by their signature combination of the method’s name and the number, types and order of its parameters. Remember – Return type of method DOES NOT MATTER Overloaded methods do not need to have the same number of parameters public class Tester { public int myMethod(int) { } public void myMethod(int) { } Still ambiguous to the compiler