Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 6.

Slides:



Advertisements
Similar presentations
Chapter 5: Elementary Data Types Properties of types and objects –Data objects, variables and constants –Data types –Declarations –Type checking –Assignment.
Advertisements

Road Map Introduction to object oriented programming. Classes
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.
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.
SE-1010 Dr. Mark L. Hornick 1 Defining Your Own Classes Part 3.
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.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
© 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.
Classes and Methods Computer Engineering Department Java Course Asst. Prof. Dr. Ahmet Sayar Kocaeli University - Fall 2014.
CSE 131 Computer Science 1 Module 1: (basics of Java)
More arrays Primitive vs. reference parameters. Arrays as parameters to functions.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
Arrays Chapter 8. What if we need to store test scores for all students in our class. We could store each test score as a unique variable: int score1.
 2005 Pearson Education, Inc. All rights reserved. 1 Methods Called functions or procedures in other languages Modularize programs by separating its tasks.
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.
C Functions Pepper. Objectives Create functions Function prototypes Parameters – Pass by value or reference – Sending a reference Return values Math functions.
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.
Methods F Hello World! F Java program compilation F Introducing Methods F Declaring Methods F Calling Methods F Passing Parameters by value F Overloading.
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.
OOP in Java : © W. Milner 2005 : Slide 1 Java and OOP Part 2 – Classes and objects.
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.
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.
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
1 Chapter 6 Methods. 2 Motivation Find the sum of integers from 1 to 10, from 20 to 30, and from 35 to 45, respectively.
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 7.
1 Static Variable and Method Lecture 9 by Dr. Norazah Yusof.
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.
6 th week Spring 2011 Midterm Review 1. Primitive Types vs. Reference Types Java’s types: primitive types and reference types. Primitive types: boolean,
 Static  Example for Static Field  Example for Static Method  Math class methods  Casting  Scope of Declaration  Method Overloading  Constructor.
Methods.
5.1 Basics of defining and using classes A review of class and object definitions A class is a template or blueprint for an object A class defines.
Mark Fontenot CSE Honors Principles of Computer Science I Note Set 7.
(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,
Recursion occurs when a method calls itself. Google “recursion”
Georgia Institute of Technology More on Creating Classes Barb Ericson Georgia Institute of Technology June 2006.
Recursion occurs when a method calls itself. public class RecursionOne { public void run(int x) { System.out.println(x); run(x+1); } public static void.
 Pearson Education, Inc. All rights reserved Methods: A Deeper Look.
Mark Fontenot CSE Honors Principles of Computer Science I Note Set 6.
Functions + Overloading + Scope
Objects as a programming concept
C Functions Pepper.
Lecture 4 D&D Chapter 5 Methods including scope and overloading Date.
Methods and Parameters
Chapter 6 Methods: A Deeper Look
Group Status Project Status.
Chapter 6 Methods.
Scope of variables class scopeofvars {
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:

Spring 2008 Mark Fontenot CSE 1341 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

Parameter Passing Concepts

Primitive Variables public class Test1 { public static void foo(int x) { x++; } public static void main (String[]args) { int z = 10; foo(z); } } z x when method is called, value of z is copied into x. We can conclude that…

Passing an Object Reference public class Test2 { public static void bar(Point x) { x.setLocation(10, 20); } public static void main(String [] args) { Point p = new Point(2, 3); System.out.printf("p = %s\n", p.toString()); bar(p); System.out.printf("now p = %s\n", p.toString()); } p = java.awt.Point[x=2,y=3] now p = java.awt.Point[x=10,y=20] p = java.awt.Point[x=2,y=3] now p = java.awt.Point[x=10,y=20] OUTPUT:

Passing an Array public class Test3 { public static void fun(int [] x) { x[0] = 99999; } public static void main(String [] args) { int[] values = new int[3]; values[0] = 1; values[1] = 2; values[2] = 3; System.out.printf("value[0] = %d\n", values[0]); fun(values); System.out.printf("value[0] = %d\n", values[0]); } value[0] = 1 value[0] = value[0] = 1 value[0] = OUTPUT:

What’s going on here? public class Test2 { public static void bar(Point x) { x.setLocation(10, 20); } public static void main(String [] args) { Point p = new Point(2, 3); System.out.printf("p = %s\n", p.toString()); bar(p); System.out.printf("now p = %s\n", p.toString()); } Main: p 2 3 bar: x The object reference stored in p gets copied into x. System Stack

What’s going on here? public class Test2 { public static void bar(Point x) { x.setLocation(10, 20); } public static void main(String [] args) { Point p = new Point(2, 3); System.out.printf("p = %s\n", p.toString()); bar(p); System.out.printf("now p = %s\n", p.toString()); } } Main: p bar: x The object reference stored in p gets copied into x. System Stack

Strings mess it up public class Test4 { public static void main (String [] args) { String myString = "Mark"; System.out.printf("MyString 1.0 = %s\n", myString); function(myString); System.out.printf("MyString 2.0 = %s\n", myString); } public static void function(String s) { s = "Hello”; System.out.printf ("s = %s\n", s); } } MyString 1.0 = Mark s = Hello MyString 2.0 = Mark MyString 1.0 = Mark s = Hello MyString 2.0 = Mark OUTPUT: What’s going on here?