Lecture 03 & 04 Method and Arrays Jaeki Song.

Slides:



Advertisements
Similar presentations
Chapter 4 Methods F Introducing Methods –Benefits of methods, Declaring Methods, and Calling Methods F Passing Parameters –Pass by Value F Overloading.
Advertisements

BBS514 Structured Programming (Yapısal Programlama)1 Functions and Structured Programming.
1 Classes, Encapsulation, Methods and Constructors (Continued) Class definitions Instance data Encapsulation and Java modifiers Method declaration and.
Chapter 7 User-Defined Methods. Chapter Objectives  Understand how methods are used in Java programming  Learn about standard (predefined) methods and.
Methods. int month; int year class Month Defining Classes A class contains data declarations (static and instance variables) and method declarations (behaviors)
Introduction to Computers and Programming Lecture 11: Introduction to Methods Professor: Evan Korth New York University.
Introduction to arrays Data in economy size packages.
Math class methods & User defined methods Introduction to Computers and Programming in JAVA: V
COMP 14 Introduction to Programming Miguel A. Otaduy May 25, 2004.
1 Chapter 7 User-Defined Methods Java Programming from Thomson Course Tech, adopted by kcluk.
ECE122 L13: Arrays of Objects March 15, 2007 ECE 122 Engineering Problem Solving with Java Lecture 13 Arrays of Objects.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 8, 2005.
03/16/ What is an Array?... An array is an object that stores list of items. Each slot of an array holds an individual element. Characteristics.
 Introduction Introduction  Types of Function Types of Function  Library function Library function  User defined function User defined function 
10/25: Methods & templates Return to concepts: methods Math class methods 1 st Program of the day About DrawLine.java modifications Method definitions.
Operator Precedence First the contents of all parentheses are evaluated beginning with the innermost set of parenthesis. Second all multiplications, divisions,
C Functions Programmer-defined functions – Functions written by the programmer to define specific tasks. Functions are invoked by a function call. The.
More with Methods (parameters, reference vs. value, array processing) Corresponds with Chapters 5 and 6.
Lecture # 5 Methods and Classes. What is a Method 2 A method is a set of code which is referred to by name and can be called (invoked) at any point in.
CS 115 OBJECT ORIENTED PROGRAMMING I LECTURE 4 part 3 GEORGE KOUTSOGIANNAKIS.
Chapter 7 One-Dimensional Arrays 7.1 Arrays in C One of the more useful features of C is the ability to create arrays for storing a collection of related.
Arrays Module 6. Objectives Nature and purpose of an array Using arrays in Java programs Methods with array parameter Methods that return an array Array.
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.
Review of ICS 102. Lecture Objectives To review the major topics covered in ICS 102 course Refresh the memory and get ready for the new adventure of ICS.
1 Arrays An array is a collection of data values, all of which have the same type. The size of the array is fixed at creation. To refer to specific values.
Functions CIS Feb-06. Summary Slide Using Functions Mathematical Functions Misc. Functions Naming Conventions Writing Functions –Function Prototype.
Structure Programming Lecture 8 Chapter 5&6 - Function – part I 12 December 2015.
1 Methods Introduction to Methods Passing Arguments to a Method More About Local Variables Returning a Value from a Method Problem Solving with Methods.
10/25: Methods & templates Return to concepts: methods Math class methods Program of the day.
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.
User Defined Methods Methods are used to divide complicated programs into manageable pieces. There are predefined methods (methods that are already provided.
Java Classes Chapter 1. 2 Chapter Contents Objects and Classes Using Methods in a Java Class References and Aliases Arguments and Parameters Defining.
TOPIC 6 Methods F Introducing Methods F Declaring Methods F Calling Methods F Passing Parameters F Pass by Value F Overloading Methods F Method Abstraction.
Chapter 1 Java Programming Review. Introduction Java is platform-independent, meaning that you can write a program once and run it anywhere. Java programs.
ITM 3521 ITM 352 Functions. ITM 3522 Functions  A function is a named block of code (i.e. within {}'s) that performs a specific set of statements  It.
Chapter 5 Arrays F Introducing Arrays F Declaring Array Variables, Creating Arrays, and Initializing Arrays F Passing Arrays to Methods F Copying Arrays.
Chapter 5 Arrays F Introducing Arrays F Declaring Array Variables, Creating Arrays, and Initializing Arrays F Passing Arrays to Methods F Copying Arrays.
Chapter VII: Arrays.
Functions + Overloading + Scope
Chapter 7 User-Defined Methods.
Java Programming: From Problem Analysis to Program Design, 3e Chapter 7 User-Defined Methods.
Suppose we want to print out the word MISSISSIPPI in big letters.
Introduction to Modular Programming
Chapter 7 Top-Down Development
Chapter 5 Functions DDC 2133 Programming II.
Chapter 5 Functions.
Object Oriented Systems Lecture 03 Method
C-language Lecture By B.S.S.Tejesh, S.Neeraja Asst.Prof.
JavaScript: Functions.
CSCI 161: Introduction to Programming Function
User-Defined Functions
Chapter 6 - Arrays Outline 6.1 Introduction 6.2 Arrays
Chapter 6 Methods: A Deeper Look
Chapter 5 Arrays Introducing Arrays
Classes, Encapsulation, Methods and Constructors (Continued)
Review of Arrays and Pointers
Introduction To Programming Information Technology , 1’st Semester
Object Oriented Programming in java
Data Structures (CS212D) Week # 2: Arrays.
BBIT 212/ CISY 111 Object Oriented Programming (OOP)
Chapter 9: Value-Returning Functions
Arrays ICS2O.
Visual Programming COMP-315
Functions Imran Rashid CTO at ManiWeber Technologies.
Arrays and Pointers CSE 2031 Fall May 2019.
Arrays and Pointers CSE 2031 Fall July 2019.
Review for Midterm 3.
ITM 352 Functions.
Corresponds with Chapter 5
Presentation transcript:

Lecture 03 & 04 Method and Arrays Jaeki Song

Outlines Method Array Creating method Method call Passing parameters Method overloading Math class Array Definition of an Array How to Declare and Initialize an Array How to Pass Arrays to Methods

Methods Java API provides a rich collection of classes and methods User-defined method Build-in method Math class method

Creating Method Structure modifier returnValueType methodName (list of parameters) modifiers Return value Type Method header public static int max (int num1, int num2) { ….. } methodName parameters Method body

Return Type Void Nonvoid does not return a value to the calling program Nonvoid returns a single value to the calling program.

Method Name A verb or a verb phrase. Capitalize the first letter of the second word Never mention the method name inside of the function definition except if the function is recursive.

Parameter Listings Are variables sent to the method to perform its designated tasks. Must determine and list the data type of each parameter before listing its name.

Method Body Includes the statements that will perform the task Must be framed with curly braces. Start by defining variables (only the ones outside of the class and not listed within the parameter listing)

Calling a Method A method is invoked by a method call Specifies the method name and provides information (argument) Two ways to call a method Based on whether the method returns a value or not Return value int larger = max (3,4); System.out.println( max (3,4)); Return void A call to the method must be a statement System.out.println (“ Welcome”); Example: Max

Passing Parameters Pass by value When you invoke a method with parameters, a copy of the value of the actual parameter is passed to the method Example: Swap Formal parameters are changed in the example, but the actual parameter are not affected

Overloading Methods Create another method with the same name, but different parameters Example Overloading the Max method

Commonly Used Math Class Methods Description abs (x) Absolute value of x exp (x) Exponential method ex log (x) Natural logarithm of x (base e) max (x, y) Larger value of x and y min (x, y) Smaller value of x and y pow (x, y) X raised to power y (xy) sqrt (x) Square root of x

Example Car Loan Systems Shipping Charge

Arrays A list of related values with the same data type that is stored using a single group name When you need to store multiple values, use an array It is a static entity, in that it remains the same size throughout program execution

Array studentGrade[ ] array The individual variable called element of the array Subscript (or index): the first element in every array is the zeroth element [0] [1] [2] [3] [4] .. 87 57 69 78 95 “second element of the array  it has a subscript of 1 “array element two”  has a subscript of 2

Declaring Array Specifies the type of the elements and uses operator new to allocate the number of elements Arrays are considered to be objects and all objects must be created with new operator

Declaring Array General format DataType ArrayName [ ]; //Declares the array ArrayName = new DataType [Size]; // allocates the array DataType ArrayName[ ] = new DataType [ Size] e.g: float fltPayment[]; or float[ ] fltPayment; fltPayment = new float [ 20]; float fltPayment[] = new float [20];

Initializing an Array For primitive data types and Strings, you can give initial values to array element as you declare an array e.g. int DepartNumber[ ] = {423, 635, 589}; String DepartName[] = {“MIS”, “Marketing”, “Finance”}; This does not allow you to specify the size of the array and do not use new keyword

Array Length Every array in java knows it own length Example: Grade fltPayment = new float [ 20]; fltPayment.length Example: Grade

Passing Arrays to Methods To pass an array argument to a method, specify the name of the array of the array without any brackets Pass the entire array (call-by-reference) float score [ ] = new float [20]; grade = calculateAverage (score); public float calculateAverage (float b [ ])

Passing Arrays to Methods To pass an array element to a method, use the subscripted name of the array element as an argument in the method call float score [ ] = new float [20]; grade = calculateAverage (score[3] ); public float calculateAverage (float b)

Example: Bubble Sort Compare pairs of adjacent memory cells The small number: “bubble” The large number: “sink” Example: Comparison Results Action num[0] > num[1] False None num[1] > num[2] True Swap num[2] > num[3] True Swap 5 10 8 2 num[0] num[1] num[2] num[3]

Example: Bubble Sort for(i=0 ; i<num.length – 1 ; i++) { } Ascending order for (i=1; i<num.length; i++) { } num[1]=10 num[2]=8 > if (num[j] < num [j-1]) { temp = num[j]; // one swap num[j] = num[j-1]; num[j-1] = temp; } temp=8 num[1]=8 num[2]=10 Example 2

Example: Selection Sort Finds the largest/smallest number in the list and places it last/first. num[0] num[1] num[2] num[3] 8 9 5 4 num[0] num[1] num[2] num[3] 8 4 5 9 Example 3