Lecture 10 Methods COMP1681 / SE15 Introduction to Programming.

Slides:



Advertisements
Similar presentations
AP Computer Science Anthony Keen. Computer 101 What happens when you turn a computer on? –BIOS tries to start a system loader –A system loader tries to.
Advertisements

1 Classes and Objects in Java Basics of Classes in Java.
Basic Java Constructs and Data Types – Nuts and Bolts
Introduction to Programming Java Lab 1: My First Program 11 January JavaLab1.ppt Ping Brennan
Introduction to Programming
Introduction to Programming Java Lab 2: Variables and Number Types 1 JavaLab2 lecture slides.ppt Ping Brennan
Introduction to Programming Java Lab 5: Boolean Operations 8 February JavaLab5 lecture slides.ppt Ping Brennan
Lecture 5 Types, Expressions and Simple I/O COMP1681 / SE15 Introduction to Programming.
Lecture 6 Strings and more I/O COMP1681 / SE15 Introduction to Programming.
Lecture 10 Flow of Control: Loops (Part 2) COMP1681 / SE15 Introduction to Programming.
DONT PANIC!! Lots of new notions coming in these slides Dont worry if not all of it makes perfect sense Well meet most of this stuff again in detail later.
This is Java Jeopardy.
1 Review Quisioner Kendala: Kurang paham materi. Praktikum Pengaruh teman.
Purpose : To convert this string to a new character array. Return Type : char[ ] Parameters : none Declaration : public char[ ] toCharArray() Returns.
Loops –Do while Do While Reading for this Lecture, L&L, 5.7.
Introduction to Programming G51PRG University of Nottingham Revision 1
Today’s lecture Review of Chapter 1 Go over homework exercises for chapter 1.
Lecture 12 Recursion part 1
Air Force Institute of Technology Electrical and Computer Engineering
1 Various Methods of Populating Arrays Randomly generated integers.
Chapter 2: Using Objects Part 1. To learn about variables To understand the concepts of classes and objects To be able to call methods To learn about.
Chapter 7 User-Defined Methods. Chapter Objectives  Understand how methods are used in Java programming  Learn about standard (predefined) methods and.
Constructors & An Introduction to Methods. Defining Constructor – Car Example Public class car { String Model; double speed; String colour; { Public Car.
1 Fall 2009ACS-1903 Methods – Ch 5 A design technique referred to as stepwise refinement (or divide and conquer, or functional decomposition) is used to.
Lecture 4 Types & Expressions COMP1681 / SE15 Introduction to Programming.
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.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 7 User-Defined Methods.
Parameters, Arguments, Local Variables, and Scope CSC 1401: Introduction to Programming with Java Week 8 – Lecture 1 Wanda M. Kunkle.
COMP 14: Primitive Data and Objects May 24, 2000 Nick Vallidis.
11 Chapter 5 METHODS. 22 INTRODUCTION TO METHODS A method is a named block of statements that performs a specific task. Other languages use the terms.
Introduction to Methods
COMP More About Classes Yi Hong May 22, 2015.
Comp 248 Introduction to Programming Chapter 4 - Defining Classes Part A Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia.
Comments are for people Header comments supply basic information about the artifact.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
11 Chapter 5 METHODS CONT’D. 22 MORE ON PASSING ARGUMENTS TO A METHOD Passing an Object Reference as an Argument to a Method Objects are passed by reference.
The Java Programming Language
Using Data Within a Program Chapter 2.  Classes  Methods  Statements  Modifiers  Identifiers.
Jens Dalsgaard Nielsen Jan Dimon Bendtsen Dept. of Electronic Systems Basic Programming INS-basis GF, PDP and HST.
An Introduction to Java – Part 1 Dylan Boltz. What is Java?  An object-oriented programming language  Developed and released by Sun in 1995  Designed.
Chapter 2 Using Objects. Types A type defines a set of values and the operations that can be carried out on the values Examples: 13 has type int "Hello,
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
Identifiers.
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.
Methods OR HOW TO MAKE A BIG PROGRAM SEEM SMALLER.
Agenda Comments Identifiers Keywords Syntax and Symentics Indentation Variables Datatype Operator.
Aside: Running Supplied *.java Programs Just double clicking on a *.java file may not be too useful! 1.In Eclipse, create a project for this program or.
AP Java Ch. 4 Review Question 1  Java methods can return only primitive types (int, double, boolean, etc).
Functions, Part 1 of 3 Topics  Using Predefined Functions  Programmer-Defined Functions  Using Input Parameters  Function Header Comments Reading 
CSCI 51 Introduction to Programming Dr. Joshua Stough February 24, 2009.
CSE 501N Fall ‘09 03: Class Members 03 September 2009 Nick Leidenfrost.
SUMMARY OF CHAPTER 2: JAVA FUNDAMENTS STARTING OUT WITH JAVA: OBJECTS Parts of a Java Program.
Chapter 7 User-Defined Methods.
Working with Java.
Suppose we want to print out the word MISSISSIPPI in big letters.
Chapter 4 Procedural Methods.
2008/11/05: Lecture 15 CMSC 104, Section 0101 John Y. Park
Program Style Console Input and Output
Functions I Creating a programming with small logical units of code.
An Introduction to Java – Part I, language basics
Chapter 1: Computer Systems
Chapter 7 Procedural Methods.
METHODS, CLASSES, AND OBJECTS A FIRST LOOK
Anatomy of a Java Program
Classes, Objects and Methods
2008/11/05: Lecture 15 CMSC 104, Section 0101 John Y. Park
Functions I Creating a programming with small logical units of code.
Corresponds with Chapter 5
Presentation transcript:

Lecture 10 Methods COMP1681 / SE15 Introduction to Programming

SE15: Methods10–2 Todays Learning Objectives For you to appreciate the benefits of dividing a program up into a set of methods For you to see how methods are defined, invoked and documented in Java

SE15: Methods10–3 Lecture Outline Why methods are needed Structure of a method definition Passing data into methods Returning data from methods Documenting methods

SE15: Methods10–4 Why Do We Need Other Methods? So far, weve been putting all our code in main… Can you think of any problems with this?

SE15: Methods10–5 Anatomy of a Method Definition [ modifiers ] return-type name ( [ parameter-list ] ) { statements } public static void main(String[] args) { System.out.println("Hello, World!"); }

SE15: Methods10–6 Anatomy of a Method Definition Modifiers public means anyone can call the method static is needed for methods that are not called on objects; well stop using it shortly! Return type Specifies the type of value returned to methods caller void must be used if method returns no value Parameter list Allows us to pass data into a method Optional (except for main!)

SE15: Methods10–7 Method Names Rules Name must begin with letter or underscore Other characters can be letters, digits or underscore Convention Use camel case with initial lower-case letter Use verbs or verb phrases Exception: mathematical functions Exception: methods that return a boolean (true/false) value

SE15: Methods10–8 Answers Question 2 secondValue _Second_Value _2ndValue Question 3 displayResults

SE15: Methods10–9 A Very Simple Example class Greeting { public static void greet() { System.out.println("Hello, World!"); } public static void main(String[] args) { greet(); } }

SE15: Methods10–10 Passing Data to Methods Method definition can have a list of formal parameters Parameters act as placeholders for data that are actually passed to the method when it is called Values passed to the method are called arguments Each parameter has a name and a type public static void greet(String name) { System.out.println("Hello, " + name + "!"); } Savitch, p

SE15: Methods10–11 Returning Data From Methods Method must have a non-void return type Body of method must include a return statement public static String getName() { Scanner keyboard = new Scanner(System.in); System.out.println("Enter your name"); String name = keyboard.next(); return name; } Local variables Savitch, p

SE15: Methods10–12 Question 4 public static int minimum(int a, int b) { if (a < b) return a; else return b; }

SE15: Methods10–13 Documenting Methods /** * Issues a personalised greeting. * name Name of person to be greeted */ public static void greet(String name) { System.out.println("Hello, " + name + "!"); }

SE15: Methods10–14 Documenting Methods /** * Obtains a person's name from keyboard. * Name of person */ public static String getName() { Scanner keyboard = new Scanner(System.in); System.out.println("Enter your name"); String name = keyboard.next(); return name; }

SE15: Methods10–15 Summary We have Recognised a need to manage the complexity of programs using methods Examined the syntax of method definitions Considered how methods should be named Looked at how parameter lists allow data to be passed into a method Looked at how a method can return a value to its caller Seen how doc comments can be used to provide documentation for methods (and the enclosing class)