Blue Box Diagram Method contains a block of instructions We pass (give) The method information (expressions) as arguments to use Processing is done in.

Slides:



Advertisements
Similar presentations
PHP functions What are Functions? A function structure:
Advertisements

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.
Methods. int month; int year class Month Defining Classes A class contains data declarations (static and instance variables) and method declarations (behaviors)
CIT 590 Intro to Programming Java lecture 4. Agenda Types Collections – Arrays, ArrayLists, HashMaps Variable scoping Access modifiers – public, private,
Unit 08 & 091 Nested Classes Introduction Inner Classes Local Classes Anonymous Classes Exercises.
Functions and Methods Function : Block of instructions called (executed) by name Method: A function operating within an object –World Example: start car.
COMP 14 Introduction to Programming Miguel A. Otaduy May 25, 2004.
Enhancing classes Visibility modifiers and encapsulation revisited
COMP 14 Introduction to Programming Mr. Joshua Stough February 28, 2005 Monday/Wednesday 11:00-12:15 Peabody Hall 218.
Primitive Data Types byte, short, int, long float, double char boolean Are all primitive data types. Primitive data types always start with a small letter.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 4 Defining Your Own Classes.
Writing Methods. Create the method Methods, like functions, do something They contain the code that performs the job Methods have two parts.
Lecture From Chapter 6 & /8/10 1 Method of Classes.
COMP More About Classes Yi Hong May 22, 2015.
SE-1010 Dr. Mark L. Hornick 1 Defining Your Own Classes Part 3.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
Java Quiz Bowl A fun review of the Java you should know from CMPT 201 If you don’t know the answers - this week is for you to study up!
Methods We write methods in our programs for many reasons: –By breaking the program into smaller units it is easier to design the program it is easier.
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.
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.
Methods in Java. Program Modules in Java  Java programs are written by combining new methods and classes with predefined methods in the Java Application.
CSE 114 Computer Science I Objects Lake Superior, Michigan.
Questions? Suggestions?. References References Revisited What happens when we say: int x; double y; char c; ???
JAVA Classes Review. Definitions Class – a description of the attributes and behavior of a set of computational objects Constructor – a method that is.
OOP in Java : © W. Milner 2005 : Slide 1 Java and OOP Part 2 – Classes and objects.
Methods We write methods in our programs for many reasons:
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
1 Building Java Programs Chapter 3: Introduction to Parameters and Objects These lecture notes are copyright (C) Marty Stepp and Stuart Reges, They.
COP3502 Programming Fundamentals for CIS Majors 1 Instructor: Parisa Rashidi.
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.
Classes and Methods. Classes Class Definition Data Fields –Variables to store data items –Differentiate multiple objects of a class –They are called.
Coding Bat: Ends in ly Given a string of even length, return a string made of the middle two chars, so the string "string" yields "ri". The string.
User Defined Methods Methods are used to divide complicated programs into manageable pieces. There are predefined methods (methods that are already provided.
1 BUILDING JAVA PROGRAMS CHAPTER 2 PRIMITIVE DATA AND DEFINITE LOOPS.
Interfaces and Inner Classes
Nested Classes CompSci 230 S Software Construction.
Topic 8Classes, Objects and Methods 1 Topic 8 l Class and Method Definitions l Information Hiding and Encapsulation l Objects and Reference Classes, Objects,
1 Predefined Classes and Objects Chapter 3. 2 Objectives You will be able to:  Use predefined classes available in the Java System Library in your own.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
A High Flying Overview CS139 – Fall 2006 How far we have come.
Array Size Arrays use static allocation of space. That is, when the array is created, we must specify the size of the array, e.g., int[] grades = new int[100];
Methods.
Java: Variables and Methods By Joshua Li Created for the allAboutJavaClasses wikispace.
AP Computer Science A – Healdsburg High School 1 Unit 9 - Parameter Passing in Java.
OOP Basics Classes & Methods (c) IDMS/SQL News
Methods What is a method? Main Method the main method is where a stand alone Java program normally begins execution common compile error, trying.
CSCI 51 Introduction to Programming Dr. Joshua Stough February 24, 2009.
Chapter 9 Introduction to Arrays Fundamentals of Java.
Lecture 9: Object and Classes Michael Hsu CSULA. 2 OO Programming Concepts Object-oriented programming (OOP) involves programming using objects. An object.
Programming in Java Transitioning from Alice. Becomes not myFirstMethod but …. public static void main (String[] arg) { // code for testing classes goes.
Methods. Methods are groups of statements placed together under a single name. All Java applications have a class which includes a main method class MyClass.
A Simple Object Oriented Program public class Simple { public static void main (String [] args) { System.out.println(“howdy”); } System.out is an object.
Information and Computer Sciences University of Hawaii, Manoa
Functions + Overloading + Scope
Topic: Classes and Objects
Chapter 7 User-Defined Methods.
Examples of Classes & Objects
Java Programming: Guided Learning with Early Objects
Lecture 5: Some more Java!
Programming Language Concepts (CIS 635)
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.
Starting Out with Java: From Control Structures through Objects
Writing Methods.
Week 6 CS 302 Jim Williams, PhD.
Classes & Objects: Examples
Scope of variables class scopeofvars {
Methods/Functions.
Methods (a.k.a functions)
Corresponds with Chapter 5
Presentation transcript:

Blue Box Diagram Method contains a block of instructions We pass (give) The method information (expressions) as arguments to use Processing is done in the blue box Out can come a value that is returned Instance Variables (side effects) Definition: A method is a block of instructions in a class or object that is called by name

Parameters and Arguments Parameter: name a function gives an expression used to call a method Argument: expression used to call a method Why the difference? – We might want to use the method more than once. – We might want to call a function using literals 30 public int triple(int x) { return x*3; } x = triple(10); First Call 60 public int triple(int x) { return x*3; } x = triple(20); Second Call 60 public int triple(int x) { return x*3; } int z = 20; x = triple(z); Third Call 90 public int triple(int x) { return x*3; } x = triple(triple(10)); Fourth Call

General Class Syntax class {… Your instance variables here … ( ) { …. Instructions here …} ∙ ( ) { …. Instructions here …} } Example that we did in labs public class Grader { public final static int LABMAX = 90; public static void main(String[] args) { … instructions …} } Note: protected is something that can be inherited by a child class (This is a CS 257 topic – memorize the definition for now.

Methods Static method – It is using the class name – Call round() method in Math class : System.out.println(Math.round(4.5) ; Non static method – It is called using an instantiated object – String str = “abc”; – Call the length() method in str, an instantiated object of type String System.out.println(str.length(); Java has a huge class library. – We can use this library to access methods that do all sorts of useful things. – See: Definition: Block of instructions called (executed) by name Dot notation: The syntax to access methods inside classes and objects

More on static and non-static public class MyClass {public static void main(String[] args) {MyClass data = new MyClass(); // Make instance of MyClass(). String s1 = data.name(); // The name method executes. String s2 = name(); // Error – name() is not static. String s3 = name2(); // OK – name2() is a static method. String s4 = MyClass.name2(); // OK – name2() is a static method. System.out.println(s1); } public String name(){ return “Dan ” + doIt(); } public String doIt() { return “Harvey”; } public static String name2() { return “Static Dan Harvey”; } } Note: when we call methods in the same class, dot notation is not needed.

A method returning a value public class ReturnValue {private int y = 10; public static void main(String[] args) {ReturnValue myObject = new ReturnValue(); int x = myObject.doReturn(); System.out.println(x); } private int doReturn() {int x = 33 + y; return x; } What prints? Where is x visible? Where is y visible? Is x in doReturn the same variable as in main? Which are the instance variables?

A method with parameters public class LotsOfParams { protected static int x = 3; public static void main(String[] args) {LotsOfParams params = new LotsOfParams(); int y = x + 4; int x = params.doSomething (1,2,x + y -3,4,params.doSomething(x, y, 3, 4, 5)); } private int doSomething(int a, int b, int c, int d, int e) {System.out.println(“” + c + b + d + a + e ); return a + b + c + d + e; } What prints? Why private? Which are the arguments? Which are the parameters (formal arguments)? Which are instance variables? What are the methods?

Pass by value A copy of arguments gets passed to a method, not the variable itself Example 1.Call: int x = 5; tryToChange(x); 2.Method: private void tryToChange(int x) { x = 3; } 3.Result: x in the call does not change! The x in the method only lives between the method’s braces. Question: Why doesn’t x change?

Method to search an array public class FindMe {String[] list = {“hello”, “this”, “is”, “a”, “good”, “day”}; public static void main(String[] args) {FindMe find = new FindMe(); System.out.println(find.isItThere(“is”)); System.out.println(find.isItThere(“cat”)); } private int isItThere(String which) {for (int i=0; i<list.length; i++) { if (list[i].equals(which)) return i; } return -1; } Question: Why do we need find in find.isItThere(“is”));

Method to remove and add public class RemoveMe {String[] list = {“hello”, “this”, “is”, “a”, “good”, “day”}; int howMany = list.length; public static void main(String[] args) {removeIt(3); addIt(“greetings”); } private static void removeIt(int which) {for (int i=which; i<howMany-1; i++) { list[i] = list[i+1]; } howMany--;} public static boolean addIt(String newOne) {if (howMany == list.length) return false; list[howMany++] = newOne;}

Review 1.What is a method? 2.What is an instance variable? 3.What is the difference between an argument an a parameter? 4.What does it mean to limit scope? 5.What is an argument? 6.What is a side effect? 7.What does private and public mean? 8.What is the definition of protected? 9.How do you return a value from a method? 10.What does pass by value mean? 11.What is the difference between an object variable and primitive variable?