Methods Matthew Harrison. Overview ● There are five main aspects of methods... ● 1) Modifiers – public, private ● 2) Method Name ● 3) Parameters ● 4)

Slides:



Advertisements
Similar presentations
Continuation of chapter 6…. Nested while loop A while loop used within another while loop is called nested while loop. Q. An illustration to generate.
Advertisements

Lecture 10 Methods COMP1681 / SE15 Introduction to Programming.
Methods and Formatted Output Chapter 4. Overview Breaking down a big program into small methods. Formatting output – using printf – using DecimalFormat.
Introduction to C# Erick Pranata © Sekolah Tinggi Teknik Surabaya 1.
© Vinny Cahill 1 Classes in Java. © Vinny Cahill 2 Writing a Java class Recall the program to calculate the area and perimeter of a rectangle of given.
1 Repetition structures Overview while statement for statement do while statement.
ISQA 360 – July 8, 2002 Methods Dr. Sergio Davalos.
Loops – While Loop Repetition Statements While Reading for this Lecture, L&L, 5.5.
COMP 14 Introduction to Programming Miguel A. Otaduy May 25, 2004.
Scanner Pepper With credits to Dr. Siegfried. The Scanner Class Most programs will need some form of input. At the beginning, all of our input will come.
1 Methods Overview l Closer Look at Methods l Parameter passing l Passing parameters by value l Passing parameters by reference.
Method exercises Without IF. Setup Create one new project to hold all of these. In that project, create an empty class called WorkIt to hold all of your.
 To be able to write larger programs ◦ By breaking them down into smaller parts and passing data between the parts.  To understand the concepts of Methods.
Computer Programming Lab(5).
Saravanan.G.
Programming Progamz pls. Importance VERY IMPORTANT.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
Chapter 4 Procedural Methods. Learning Java through Alice © Daly and Wrigley Objectives Identify classes, objects, and methods. Identify the difference.
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.
Chapter 3: Developing Class Methods Object-Oriented Program Development Using Java: A Class-Centered Approach.
COM S 207 Method Instructor: Ying Cai Department of Computer Science Iowa State University
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
Writing Static Methods Up until now, we have been USING (calling) static methods that other people have written. Now, we will start CREATING our own static.
Classes. Student class We are tasked with creating a class for objects that store data about students. We first want to consider what is needed for the.
PART 1 Part 1: The Material. Whether you knew it or not, all the programming that you have performed until now was in the boundaries of procedural programming.
User Defined Methods Methods are used to divide complicated programs into manageable pieces. There are predefined methods (methods that are already provided.
Methods OR HOW TO MAKE A BIG PROGRAM SEEM SMALLER.
1 Advanced Programming Examples Output. Show the exact output produced by the following code segment. char[,] pic = new char[6,6]; for (int i = 0; i
Software development For large and complex software use divide and conquer rule. Software design methods: Structured design Object-Oriented design.
Methods.
Module 13: Properties and Indexers. Overview Using Properties Using Indexers.
import java.util.Scanner; class myCode { public static void main(String[] args) { Scanner input= new Scanner(System.in); int num1; System.out.println(“Enter.
COP 2220 Computer Science I Topics –Breaking Problems Down –Functions –User-defined Functions –Calling Functions –Variable Scope Lecture 4.
JAVA METHODS (FUNCTIONS). Why are they called methods? Java is a strictly object-oriented programming language Methods are functions inside of objects.
SUMMARY OF CHAPTER 2: JAVA FUNDAMENTS STARTING OUT WITH JAVA: OBJECTS Parts of a Java Program.
Programming in Java Transitioning from Alice. Becomes not myFirstMethod but …. public static void main (String[] arg) { // code for testing classes goes.
Import javax.swing.JOptionPane; public class Rectangle { public static void main(String[] args) { double width, length, area, perimeter; String lengthStr,
Methods CSC 171 FALL 2001 LECTURE 3. History The abacus.
Introduction to Programming using Java Day 3 and 4 Java Language Basics Review The “For” loop Subroutines The “String” class.
Introduction to Programming using Java
Functions + Overloading + Scope
Department of Computer Science
using System; namespace Demo01 { class Program
Sum of natural numbers class SumOfNaturalNumbers {
Java Methods Making Subprograms.
Chapter 4 Procedural Methods.
Method Mark and Lyubo.
Starting Out with Java: From Control Structures through Objects
Conditional Loops.
Functions Used to write code only once Can use parameters.
Pemrograman Dasar Methods PTIIK - UB.
Stack Memory 2 (also called Call Stack)
An Introduction to Java – Part I, language basics
Java Methods Making Subprograms.
Subprograms Functions.
More on Classes and Objects
Code Animation Examples
The Basics of Recursion
Recursive GCD Demo public class Euclid {
Chapter 7 Procedural Methods.
class PrintOnetoTen { public static void main(String args[]) {
Web Service.
Introduction to Java Brief history of Java Sample Java Program
Method exercises Without IF
Local variables and how to recognize them
Methods/Functions.
Methods (a.k.a functions)
Basic Exception Handling
Corresponds with Chapter 5
MIS 222 – Lecture 12 10/9/2003.
Presentation transcript:

Methods Matthew Harrison

Overview ● There are five main aspects of methods... ● 1) Modifiers – public, private ● 2) Method Name ● 3) Parameters ● 4) Body of the Method ● 5) Return Type

Method Name ● The first letter of a method name must begin with a lower-case letter ● Can be public or private ● Can be static or non-static ● Can return values ● Ex) public static void main(String args) or ● public static int calculateArea(int x, int y)

Types of Methods ● If a method does not return a value, it is void ● public static void calculateArea(int x, int y) ● If a method has a return type, it is not void ● public static int calculateArea(int x, int y) ● A method can have many return types, such as int, double, String, etc.

Return Statements ● When a method is not void, it must return a value at the end of the method syntax. ● Public static int calculateArea(int x, int y) ● { int area = x * y; return(area); ● }

Method Parameters ● Following along with the previous code... ● Public static int calculateArea(int x, int y) ● { int area = x * y; return(area); ● } ● The parameters for this method are located inside the parenthesis of the method name (int x, int y)

Method Parameters ● These parameter values are input values found somewhere else in the program and they are required for the method to work correctly ● System.out.println(“Enter the length of the box: “); ● x = keyboard.nextInt(); ● System.out.println(“Enter the width of the box: “); ● y = keyboard.nextInt(); ● Public static int calculateArea(int x, int y) ● { int area = x * y; return(area); ● }

Calling Methods ● To call a method, type the method name as well as any required parameters ● calculateArea(); //void method with no parameters ● calculateArea(int x, int y); //void method with params ● int area = calculateArea(); ● //method with return type int...no params ● int area = calculateArea(int x, int y); ● //method with return type int... also has params