Introduction to Methods. How do we use Methods in Java? Let us start by creating one which displays “hello” on Dos Prompt.

Slides:



Advertisements
Similar presentations
Overloading Having more than one method with the same name is known as overloading. Overloading is legal in Java as long as each version takes different.
Advertisements

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.
Basic Java Constructs and Data Types – Nuts and Bolts
Simple Programs Simple Strings Writing some programs Only in Parameters? I/O Gadget.
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.
Self Check 1.Which are the most commonly used number types in Java? 2.Suppose x is a double. When does the cast (long) x yield a different result from.
What is a pointer? First of all, it is a variable, just like other variables you studied So it has type, storage etc. Difference: it can only store the.
Procedural programming in Java
Written by: Dr. JJ Shepherd
Chapter 7 User-Defined Methods. Chapter Objectives  Understand how methods are used in Java programming  Learn about standard (predefined) methods and.
Intro Programming By Trevor Decker Team 1743 By Trevor Decker Team 1743.
 Draft timetable has the same times as this semester: - ◦ Monday 9:00 am to 12:00 noon, 1:00 pm to 3:00 pm. ◦ Tuesday 9:00 am to 12:00 noon, 1:00 pm.
Week 2: Primitive Data Types 1.  Programming in Java  Everything goes inside a class  The main() method is the starting point for executing instructions.
COMP 14 Introduction to Programming Miguel A. Otaduy May 25, 2004.
Some basic I/O.
© The McGraw-Hill Companies, 2006 Chapter 5 Arrays.
CS31: Introduction to Computer Science I Discussion 1A 4/2/2010 Sungwon Yang
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 8, 2005.
Introduction to Computers and Programming Strings Professor: Evan Korth New York University.
Introduction to Python
Javascript II Expressions and Data Types. 2 JavaScript Review programs executed by the web browser programs embedded in a web page using the script element.
29-Jun-15 Using Objects. 2 Classes and objects The type of an object is the class that describes that object If we say int count, the type of count is.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 7 User-Defined Methods.
PHP Server-side Programming. PHP  PHP stands for PHP: Hypertext Preprocessor  PHP is interpreted  PHP code is embedded into HTML code  interpreter.
1 Chapter 2 JAVA FUNDAMENTALS CONT’D. 2 PRIMITIVE DATA TYPES Computer programs operate on data values. Values in Java are classified according to their.
Day 4 Objectives Constructors Wrapper Classes Operators Java Control Statements Practice the language.
Python Control of Flow.
Java Unit 9: Arrays Declaring and Processing Arrays.
What is RobotC?!?! Team 2425 Hydra. Overview What is RobotC What is RobotC used for What you need to program a robot How a robot program works Framework.
Java Programming Constructs 1 MIS 3023 Business Programming Concepts II The University of Tulsa Professor: Akhilesh Bajaj All slides in this presentation.
General Computer Science for Engineers CISC 106 Lecture 02 Dr. John Cavazos Computer and Information Sciences 09/03/2010.
© The McGraw-Hill Companies, 2006 Chapter 4 Implementing methods.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
Introduction to Programming David Goldschmidt, Ph.D. Computer Science The College of Saint Rose Java Fundamentals (Comments, Variables, etc.)
Computer Programming 2 Lab(1) I.Fatimah Alzahrani.
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 An array is a data structure that consists of an ordered collection of similar items (where “similar items” means items of the same type.) An array.
C STRUCTURES. A FIRST C PROGRAM  #include  void main ( void )  { float height, width, area, wood_length ;  scanf ( "%f", &height ) ;  scanf ( "%f",
COP-3330: Object Oriented Programming Flow Control May 16, 2012 Eng. Hector M Lugo-Cordero, MS.
Variables, Expressions and Statements
Java methods Methods break down large problems into smaller ones Your program may call the same method many times saves writing and maintaining same code.
Exceptions Chapter 16 This chapter explains: What as exception is Why they are useful Java exception facilities.
Counting Loops.
Python Let’s get started!.
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.
Written by: Dr. JJ Shepherd
Week 10 - Wednesday.  What did we talk about last time?  Method example  Roulette simulation  Types in Java.
Classes - Intermediate
Java: Variables and Methods By Joshua Li Created for the allAboutJavaClasses wikispace.
Loops (While and For) CSE 1310 – Introduction to Computers and Programming 1.
1 Agenda  Unit 7: Introduction to Programming Using JavaScript T. Jumana Abu Shmais – AOU - Riyadh.
Lesson 4: Introduction to Control Statements 4.1 Additional Operators Extended Assignment Operators –The assignment operator can be combined with the.
Tarik Booker CS 242. What we will cover…  Functions  Function Syntax  Local Variables  Global Variables  The Scope of Variables  Making Functions.
FILES AND EXCEPTIONS Topics Introduction to File Input and Output Using Loops to Process Files Processing Records Exceptions.
Quiz 1 A sample quiz 1 is linked to the grading page on the course web site. Everything up to and including this Friday’s lecture except that conditionals.
CPSC 233 Tutorial January 21 st /22 nd, Linux Commands.
>> Introduction to JavaScript
Chapter 7 User-Defined Methods.
Decisions Chapter 4.
Java Primer 1: Types, Classes and Operators
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.
Topics Introduction to File Input and Output
Introduction to Java Programming
An Introduction to Java – Part I, language basics
T. Jumana Abu Shmais – AOU - Riyadh
Fundamental Programming
Introduction to Computer Science
Topics Introduction to File Input and Output
Strings Taken from notes by Dr. Neil Moore & Dr. Debby Keen
Presentation transcript:

Introduction to Methods

How do we use Methods in Java? Let us start by creating one which displays “hello” on Dos Prompt

Why it is not working? We are missing the start point of our program –The main method –Add the main method

Still Not working!!  Our main method does not have any code We need to call the new method, sayHello(), from the main method!

Voila! Is this magic???? How is this possible? The program first goes to the main method by default and executes the code that there is in the main method(). In this case the main method is pointing to another method sayHello() and executes the code in that method. A method which is not referenced from the main method does not execute on its own

So is println a method? Yes The System class has a method called println amongst others –These methods are used to display text in the dos screen amongst other functionalities The parseInt and parseDouble from Integer and Double classes respectively are methods too.

Can we add more methods? Let us add a method which says bye

Is there a limit on how many methods I can have in a class? Theoritically speaking no But ideally we should group methods into classes in which they naturally fit –Like the Integer class has the methods belonging to integers –Like the Double class has the methods belonging to double (real numbers)

Method Parameters Can we pass information to a method, like the name we want to display? –Yes –These are called method parameters

What about numbers? Yes you can pass numbers like everything else as long as the method being called is expecting a number

What about displaying a real number?

How does Java know? How does Java know which method to execute? –This is called METHOD OVERLOADING – two methods with the same name but different signatures are found in the same class –The first method is expecting an int while the second is expecting a double –Like in jigsaw puzzles only the right piece fits in its place, the method matching the parameters will be executed

Can we have more than one method parameter? Yes – methods can receive as many parameters as they require. The following has 2 parameters

Can we have 2 parameters with the same name? No – all parameters must have a unique name Furthermore, you cannot have a variable within a method which shares the same name with one of the parameters

Let us program Let us create a program where we pass 2 integers and the program will tell us which is the largest of the 2 integers

How? Let us create a method called larger which takes 2 parameters of type int. This method will use a boolean operator to check which is the largest From the main method we call this method and pass 2 different numbers

Methods are typed as everything else – this method is of type int The return keyword stops the method being executed and returns the value which follows The values of a and b in the main method are copied in the method parameters a and b respectively

Can we return more than 1 value? No A method can only return one value But you can create complex objects which hold all the information you would like to return. More about objects later in the course.

Let us program! Write a method which returns the vowels within a string –Example for “Hello” will return “eo”

Let us understand the problem first We need to check every character that the given string has and –Ignore them if they are consonants –Save them if they are vowels –A loop can be used to go through each character to determine which is a vowel and which is not

Which loop Java has 3 loops The for loop is ideal for this problem as it has a built-in counter which we require Assume that the string variable is called input

What is the function of the method length? The string method length returns the number of characters the string has –This method will return 5 if the string was “hello” –Try out the following code:

Let us display each character first Create the following class:

Now we need to check the vowels We can either use a switch statement OR We can use an if statement It is better to use a switch since we are matching values not checking for ranges

Using case

Using If

How can we return all vowels? I want to return the String of all the vowels concatenated to each other not display each vowel one by one Create an empty string Concatenate each vowel to the String Return the String

The final touch

Was it tough? THE MAGIC WORD IS PRACTICE

Questions?