Methods and Parameters

Slides:



Advertisements
Similar presentations
STRING AN EXAMPLE OF REFERENCE DATA TYPE. 2 Primitive Data Types  The eight Java primitive data types are:  byte  short  int  long  float  double.
Advertisements

IT 325 OPERATING SYSTEM C programming language. Why use C instead of Java Intermediate-level language:  Low-level features like bit operations  High-level.
Core Java Lecture 4-5. What We Will Cover Today What Are Methods Scope and Life Time of Variables Command Line Arguments Use of static keyword in Java.
 2005 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
1 Classes, Encapsulation, Methods and Constructors (Continued) Class definitions Instance data Encapsulation and Java modifiers Method declaration and.
 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
BASIC JAVA. Hello World n // Hello world program public class MyFirstJavaProgram { public static void main(String args[]) { char c = 'H'; String s =
Comp 248 Introduction to Programming Chapter 4 - Defining Classes Part A Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
CHAPTER 3 Function Overloading. 2 Introduction The polymorphism refers to ‘one name having many forms’ ‘different behaviour of an instance depending upon.
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.
Lec 6 Data types. Variable: Its data object that is defined and named by the programmer explicitly in a program. Data Types: It’s a class of Dos together.
Methods F Hello World! F Java program compilation F Introducing Methods F Declaring Methods F Calling Methods F Passing Parameters by value F Overloading.
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.
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 Programming, Second Edition Chapter Two Using Data Within a Program.
Java Classes Chapter 1. 2 Chapter Contents Objects and Classes Using Methods in a Java Class References and Aliases Arguments and Parameters Defining.
 Variables are nothing but reserved memory locations to store values. This means that when you create a variable you reserve some space in memory. 
CSC 142 F 1 CSC 142 References and Primitives. CSC 142 F 2 Review: references and primitives  Reference: the name of an object. The type of the object.
Methods.
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.
Data Types References:  Data Type:  In computer science and computer programming, a data type or simply type is a.
1 Lecture # 2. * Introducing Programming with an Example * Identifiers, Variables, and Constants * Primitive Data Types * Byte, short, int, long, float,
Introduction C# program is collection of classes Classes are collection of methods and some statements That statements contains tokens C# includes five.
Chapter 2 Variables and Constants. Objectives Explain the different integer variable types used in C++. Declare, name, and initialize variables. Use character.
Mark Fontenot CSE Honors Principles of Computer Science I Note Set 6.
C++ Lesson 1.
Functions + Overloading + Scope
3 Introduction to Classes and Objects.
UNIT 5 C Pointers.
Introduction to Classes and Objects
Hassan Khosravi / Geoffrey Tien
Principles of programming languages 4: Parameter passing, Scope rules
Peer Instruction 9 File Input/Output.
Lecture 14 Writing Classes part 2 Richard Gesick.
Methods The real power of an object-oriented programming language takes place when you start to manipulate objects. A method defines an action that allows.
An Introduction to Java – Part I
Data Types.
CMSC 202 Static Methods.
Control Statement Examples
Subroutines Idea: useful code can be saved and re-used, with different data values Example: Our function to find the largest element of an array might.
Peer Instruction 6 Java Arrays.
Defining Your Own Classes Part 1
Defining Classes and Methods
Polymorphism.
An Introduction to Java – Part I, language basics
WEL COME PRAVEEN M JIGAJINNI PGT (Computer Science)
Functions A function is a “pre-packaged” block of code written to perform a well-defined task Why? Code sharing and reusability Reduces errors Write and.
Classes, Encapsulation, Methods and Constructors (Continued)
Classes and Objects 5th Lecture
Lecture 18 Arrays and Pointer Arithmetic
CONSTRUCTORS AND DESRUCTORS
Sridhar Narayan Java Basics Sridhar Narayan
CS2011 Introduction to Programming I Methods (II)
Chap 1 Chap 2 Chap 3 Chap 5 Surprise Me
Recap Week 2 and 3.
Chapter 6 Methods.
CS 200 Primitives and Expressions
Programs and Classes A program is made up from classes
2. Second Step for Learning C++ Programming • Data Type • Char • Float
Java Programming Language
In this class, we will cover:
Java’s Central Casting
Classes, Objects and Methods
Peer Instruction 4 Control Loops.
Names of variables, functions, classes
Chapter 3 Introduction to Classes, Objects Methods and Strings
Two-Dimensional Arrays
Variables and Constants
Presentation transcript:

Methods and Parameters Peer Instruction 5 Methods and Parameters

Which of the method declarations shown below will compile ? public static void method1(int i0, i1, i2, char c0) { ... } public static void method2(int i0, String s0, char c0, int i1) { ... } public static void method3(String s0, void, int i0, char c0) { ... } public static void method4(int, char, String, double) { ... } public static void method5(int i, int j = 10) { ... } must specify a data type per parameter is correct void is not a data type, used for no return value must have parameter names, otherwise how to access, however see this sometimes no way to make a default value for parameter, unlike C++ Formal Parameters cs163/164: Peer 5 - Methods and Parameters - Fall Semester 2016

cs163/164: Peer 5 - Methods and Parameters - Fall Semester 2016 What can you infer about the data types of the formal parameters of someMethod ? someMethod(‘c’, 123, (float)2.5, "Hello", false, 123456789456L); Parameters seem to be char, int, float, String, boolean, long. First parameter may be int, long, float, double instead. Second parameter may be long, float, double instead. Third parameter may be a double instead of a float. Last parameter may actually be a float or double! All of the above E) is correct, all the others are correct because actual parameters might get promoted! NOTE: short and char are not compatible, because short is signed and char is unsigned Formal Parameters cs163/164: Peer 5 - Methods and Parameters - Fall Semester 2016

cs163/164: Peer 5 - Methods and Parameters - Fall Semester 2016 Which statement is a valid invocation of a method with and int and float parameter? myMethod(int i = 12, float f = 2.3); myMethod((int) 12, (float) 2.3f); myMethod(int i, float f); myMethod(int, float); myMethod(12.0, 2.3f); cannot specify data type, cannot have initializer is correct, but type casts are redundant cannot specify data type, compiler already knows is ridiculous, not actual parameters, just data types first parameter cannot be double, second is okay NOTE: actual parameters are those used to invoke method, also called arguments Actual Paramaters cs163/164: Peer 5 - Methods and Parameters - Fall Semester 2016

cs163/164: Peer 5 - Methods and Parameters - Fall Semester 2016 Which line of code uses the integer return value from the method correctly? System.out.println(calculateInteger()); int myInteger = calculateInteger(); double myDouble = (5.0 * calculateInteger()) / 0.12345; double myDouble = Math.max(calculateInteger(), 1234); All of the above Answer is E), anywhere a literal or variable of type int can be used you can have an int return value. Return Values cs163/164: Peer 5 - Methods and Parameters - Fall Semester 2016

What are the limitations of single return value from a method? A single primitive (byte, int, float, double, char, boolean, ...) A single primitive or an array of primitives A single class, (String, Scanner, ...) A single class or an array of classes All of the above Answer is E), thus it’s not much of a limitation! Return Values cs163/164: Peer 5 - Methods and Parameters - Fall Semester 2016

cs163/164: Peer 5 - Methods and Parameters - Fall Semester 2016 How can two different methods in a class read and write the same variable? Allow one method to reference a local variable of the other Declare a variable of the same name in both methods Add the variable as a class or instance variable Pass the variable as a parameter between methods None of the above exactly what you cannot do fine, but they totally different variables is correct, that works D) would have to return the value as well E) not an option Return Values cs163/164: Peer 5 - Methods and Parameters - Fall Semester 2016

cs163/164: Peer 5 - Methods and Parameters - Fall Semester 2016 Given the code below, what is output by the two print statements, in order of execution? // Code fragment int value = 6; printSquare(value); System.out.println(value); public static void printSquare(int value) { value = value * value; } 6, 6 36, 6 36, 36 6, 36 None of the above Answer is B), value variables are completely difference memory locations, parameter is squared but not returned NOTE: Naming the actual and formal parameters the same is misleading! Pass by Value cs163/164: Peer 5 - Methods and Parameters - Fall Semester 2016

cs163/164: Peer 5 - Methods and Parameters - Fall Semester 2016 How many activation records are on the stack when executing code in Math.sin? // code fragment in main foo(1.0); public static void foo(double d) { d += bar(d * d) ; } public static void bar(double d) { d *= Math.sin(Math.PI); 1 2 3 4 E) is correct: main, myMethod0, myMethod1, Math.sin What if bar called foo? What of bar called bar? Pass by Value cs163/164: Peer 5 - Methods and Parameters - Fall Semester 2016