SlideShare una empresa de Scribd logo
1 de 30
Mobile App:IT
Eclipse Basics and Hello World
IHS GIRLS TECH CLUB: Java
Methods
WHAT IS A METHOD?
- A method is a set of code which is referred to by name and can
be called (invoked) at any point in a program simply by
utilizing the method's name.
- This makes it easy to reuse portions of code so they do not
have to be rewritten.
WHAT IS WRONG WITH THIS CODE?
Here is some code that greets people and says goodbye as they
enter and leave the classroom.
THE ANSWER
- While there is nothing wrong with the previous code, you will
notice that you are having to rewrite large portions of the
code over and over again.
- Imagine if you had 20 or more people coming in and leaving a
class. That is a lot of extra typing!
- This is where a method can come in handy.
CURRENT PROGRAM INSTRUCTIONS
Here is how the program is currently working:
Code Flow
main method:
Print Hello Line
Print Hello Line
Print Hello Line
Print Hello Line
Print Hello Line
Print Goodbye
Line
Print Hello Line
Print Goodbye
Line
METHOD PROGRAM INSTRUCTIONS
Here is how the program can work if we break it into methods.
Notice the difference?
Code Flow
main method:
hello();
hello();
hello();
hello();
hello();
goodbye();
hello();
goodbye();
hello method:
Print Hello Line
goodbye method:
Print Goodbye Line
YOU ALREADY KNOW METHODS
- You have previously used the println and print methods in your
previous programs. This means you are already familiar with
how method calls work. When you want to print a line of text,
you make a call to one of the print methods.
- You do not need to know how the method works, you just need
to know what the method does and how to call it.
DIFFERENT METHOD STYLES
There are two different types of methods:
- Built In - These are methods that are part of the Java compiler.
An example of this type of method is System.out.println
- User Defined - These are methods that you can create and use
in your own programs. You control the name of these
methods and what they do.
WRITING OUR FIRST METHOD
Here is an example of how a basic method is written. Here we
are building a simple hello method.
Explanation:
- "public" tells the program that anyone can use this method.
The word "public" is optional in our example.
- "static" tells the program that this method does not require an
instance of an object to be called.
- "void" tells the program that this method is not going to return
anything. We will talk more about returning values later.
EXPLANATION CONTINUED
- "hello" is the name we want to give our method. This could be
anything we want.
- () is used to let the program know that we are not going to pass
any variables (called parameters) into this method. More on
method parameters later.
- The method is then enclosed in {} (often called curly brackets).
Anything inside these brackets gets run every time the
method is called.
CODE STRUCTURE
- You will notice that within the hello method, the println line is
indented.
- This indentation does not control how the program functions,
however it makes the program much easier to read.
- The indentation is simply a tab character.
- When building your programs it is important to focus on
keeping your programs easy to read and keeping the
indentation correct.
FOLLOW ALONG WITH ECLIPSE
- Now it is time to follow along with the code samples in the
upcoming slides.
- The first step is to open Eclipse and create a new project called
"Greetings".
- After creating the project, Create a new Java class called
"Greetings". Make sure to check the box to add the "public
static void main" method.
- At the end of each code section, there will be a slide showing
the full code. The code on these end slides will help to
validate that you are putting the code in the correct location
and you are maintaining the correct indentation structure.
ADD THE FIRST METHOD
The first step is to add the first method we built in a previous
slide. This should be placed outside of the main method but
within the Greetings class.
FIRST METHOD CALL
Now we will add a call to the hello method. Inside the main
method add a call to hello.
RUN YOUR PROGRAM
Your program should look like this, go ahead and run it now. You
should have one line printed out.
CALLING THE METHOD MULTIPLE TIMES
You can now reuse the hello method.
The only problem with this is that it just prints the same
message three times.
METHOD PARAMETERS
- Wouldn't it be great if we could get the name in the message to
change?
- With method parameters and variables it is possible.
- Parameters are the variables that are listed as part of a method
declaration. Each parameter must have a unique name and a
defined data type.
METHOD PARAMETERS
- The first step is to determine what variable type (data type) we
want our method parameter to use. In our example we will
use String.
- The second step is to decide on a variable name. In our
example we will use first_name.
- The third step is to add the parameter to the method
definition. Lets do that now.
ADDING THE PARAMETERS
You will notice that after you change your hello method, your
hello method calls look like this.
ERRORS AND DEBUGGING
- These pink squiggly lines let you know that there is an error in
your program.
- If you hover over the pink lines you will see an error message
that can help you debug the error.
- The definition of debug is to identify and remove errors from
(computer hardware or software).
RUNNING A PROGRAM WITH ERRORS
- If you try to run your program with these errors in place, you
will receive an error message.
- If you proceed with running the program, there will be error
messages listed in the Console window.
- Now we will fix these errors.
CALLING A METHOD WITH A PARAMETER
To fix the errors, we simply need to pass in a String variable to
our hello method.
Go ahead and run your program now. You will notice that it runs
without errors.
MULTIPLE METHOD CALLS
Now we will go ahead and make a few more hello method calls
with different first names.
If you run the program, you will notice that there seems to be a
problem. Do you see what the problem is?
USING THE FIRST_NAME VARIABLE
We are not using the first_name variable that is getting passed
into our hello method. We already know how to use
variables, so let's go ahead and do that now.
Now run the program. The output should look much better now,
but there is still a spacing error. You can fix this one on your
own before you move on.
MULTIPLE PARAMETERS
- Now let's add a second parameter to the hello method.
- Multiple parameters can be added and separated by commas.
Try This:
- Make a 2nd parameter for the hello method.
- Make it a string data type.
- Give it a variable name of "last_name".
- Add "last_name" to the print statement inside the hello
method.
- Add a last name to each of the hello method calls inside the
main method. Use the last names "Smith", "Jones", and
"Johnson"
PRINTING THE FULL NAME CODE
MORE PARAMETERS
- The next step is to add the time the student checks into the class.
- We will do this by adding two more parameters to the hello method.
Try This:
- Add two more parameters, both integer data types to the hello
method.
- Name the two new variables "hour" and "minute"
- Add the times to the hello method calls.
Name John Adam Tina
Hour 9 9 9
Minute 25 27 32
MORE PARAMETERS (TRY THIS)
- Break up the print statements to use multiple print method
calls instead of just one println method call.
- Set it up so the output will display:
"Hello John Smith, welcome to our class today. The time is 9:25."
This one is a little more difficult so do not get discouraged. Try to
walk through each step one at a time and use the past
examples for help.
THE SOLUTION
THE OUTPUT
We will continue adding even more to the Greeting class so it is
important to make sure you are able to get the same output
that is listed above.

Más contenido relacionado

Similar a 06 java methods

The Java Learning Kit Chapter 5 – Methods and Modular.docx
The Java Learning Kit Chapter 5 – Methods and Modular.docxThe Java Learning Kit Chapter 5 – Methods and Modular.docx
The Java Learning Kit Chapter 5 – Methods and Modular.docxarnoldmeredith47041
 
Refactoring Tips by Martin Fowler
Refactoring Tips by Martin FowlerRefactoring Tips by Martin Fowler
Refactoring Tips by Martin FowlerIgor Crvenov
 
How, When, and Why to Patch a Module
How, When, and Why to Patch a Module How, When, and Why to Patch a Module
How, When, and Why to Patch a Module Phase2
 
Code Smells and Its type (With Example)
Code Smells and Its type (With Example)Code Smells and Its type (With Example)
Code Smells and Its type (With Example)Anshul Vinayak
 
Android application (how to add a splash screen with timer) tutorial #4
Android application (how to add a splash screen with timer) tutorial #4Android application (how to add a splash screen with timer) tutorial #4
Android application (how to add a splash screen with timer) tutorial #4Yasmine Sherif EL-Adly
 
Intro to programing with java-lecture 1
Intro to programing with java-lecture 1Intro to programing with java-lecture 1
Intro to programing with java-lecture 1Mohamed Essam
 
Cis355 a ilab 2 control structures and user defined methods devry university
Cis355 a ilab 2 control structures and user defined methods devry universityCis355 a ilab 2 control structures and user defined methods devry university
Cis355 a ilab 2 control structures and user defined methods devry universitysjskjd709707
 
CIS 1403 lab 3 functions and methods in Java
CIS 1403 lab 3 functions and methods in JavaCIS 1403 lab 3 functions and methods in Java
CIS 1403 lab 3 functions and methods in JavaHamad Odhabi
 
Clean Code - Part 2
Clean Code - Part 2Clean Code - Part 2
Clean Code - Part 2Knoldus Inc.
 
Cis 1403 lab1- the process of programming
Cis 1403 lab1- the process of programmingCis 1403 lab1- the process of programming
Cis 1403 lab1- the process of programmingHamad Odhabi
 
Program by Demonstration using Version Space Algebra
Program by Demonstration using Version Space AlgebraProgram by Demonstration using Version Space Algebra
Program by Demonstration using Version Space AlgebraMaeda Hanafi
 
Diving in OOP (Day 1) : Polymorphism and Inheritance (Early Binding/Compile T...
Diving in OOP (Day 1) : Polymorphism and Inheritance (Early Binding/Compile T...Diving in OOP (Day 1) : Polymorphism and Inheritance (Early Binding/Compile T...
Diving in OOP (Day 1) : Polymorphism and Inheritance (Early Binding/Compile T...Akhil Mittal
 
Software Design
Software DesignSoftware Design
Software DesignSpy Seat
 
CSCI1250 Project 3 Fall 2015 CSCI1250 INTRODUCTIO.docx
CSCI1250    Project 3  Fall 2015  CSCI1250 INTRODUCTIO.docxCSCI1250    Project 3  Fall 2015  CSCI1250 INTRODUCTIO.docx
CSCI1250 Project 3 Fall 2015 CSCI1250 INTRODUCTIO.docxfaithxdunce63732
 
The Expression Problem - Part 2
The Expression Problem - Part 2The Expression Problem - Part 2
The Expression Problem - Part 2Philip Schwarz
 
Django tutorial
Django tutorialDjango tutorial
Django tutorialKsd Che
 
Javascript breakdown-workbook
Javascript breakdown-workbookJavascript breakdown-workbook
Javascript breakdown-workbookHARUN PEHLIVAN
 
Problem Solving Techniques
Problem Solving TechniquesProblem Solving Techniques
Problem Solving TechniquesAshesh R
 

Similar a 06 java methods (20)

The Java Learning Kit Chapter 5 – Methods and Modular.docx
The Java Learning Kit Chapter 5 – Methods and Modular.docxThe Java Learning Kit Chapter 5 – Methods and Modular.docx
The Java Learning Kit Chapter 5 – Methods and Modular.docx
 
Refactoring Tips by Martin Fowler
Refactoring Tips by Martin FowlerRefactoring Tips by Martin Fowler
Refactoring Tips by Martin Fowler
 
How, When, and Why to Patch a Module
How, When, and Why to Patch a Module How, When, and Why to Patch a Module
How, When, and Why to Patch a Module
 
Code Smells and Its type (With Example)
Code Smells and Its type (With Example)Code Smells and Its type (With Example)
Code Smells and Its type (With Example)
 
Android application (how to add a splash screen with timer) tutorial #4
Android application (how to add a splash screen with timer) tutorial #4Android application (how to add a splash screen with timer) tutorial #4
Android application (how to add a splash screen with timer) tutorial #4
 
Intro to programing with java-lecture 1
Intro to programing with java-lecture 1Intro to programing with java-lecture 1
Intro to programing with java-lecture 1
 
Cis355 a ilab 2 control structures and user defined methods devry university
Cis355 a ilab 2 control structures and user defined methods devry universityCis355 a ilab 2 control structures and user defined methods devry university
Cis355 a ilab 2 control structures and user defined methods devry university
 
CIS 1403 lab 3 functions and methods in Java
CIS 1403 lab 3 functions and methods in JavaCIS 1403 lab 3 functions and methods in Java
CIS 1403 lab 3 functions and methods in Java
 
Clean Code - Part 2
Clean Code - Part 2Clean Code - Part 2
Clean Code - Part 2
 
Cis 1403 lab1- the process of programming
Cis 1403 lab1- the process of programmingCis 1403 lab1- the process of programming
Cis 1403 lab1- the process of programming
 
Programming Fundamentals
Programming FundamentalsProgramming Fundamentals
Programming Fundamentals
 
Program by Demonstration using Version Space Algebra
Program by Demonstration using Version Space AlgebraProgram by Demonstration using Version Space Algebra
Program by Demonstration using Version Space Algebra
 
Diving in OOP (Day 1) : Polymorphism and Inheritance (Early Binding/Compile T...
Diving in OOP (Day 1) : Polymorphism and Inheritance (Early Binding/Compile T...Diving in OOP (Day 1) : Polymorphism and Inheritance (Early Binding/Compile T...
Diving in OOP (Day 1) : Polymorphism and Inheritance (Early Binding/Compile T...
 
Software Design
Software DesignSoftware Design
Software Design
 
CSCI1250 Project 3 Fall 2015 CSCI1250 INTRODUCTIO.docx
CSCI1250    Project 3  Fall 2015  CSCI1250 INTRODUCTIO.docxCSCI1250    Project 3  Fall 2015  CSCI1250 INTRODUCTIO.docx
CSCI1250 Project 3 Fall 2015 CSCI1250 INTRODUCTIO.docx
 
The Expression Problem - Part 2
The Expression Problem - Part 2The Expression Problem - Part 2
The Expression Problem - Part 2
 
Django tutorial
Django tutorialDjango tutorial
Django tutorial
 
MPP-UPNVJ
MPP-UPNVJMPP-UPNVJ
MPP-UPNVJ
 
Javascript breakdown-workbook
Javascript breakdown-workbookJavascript breakdown-workbook
Javascript breakdown-workbook
 
Problem Solving Techniques
Problem Solving TechniquesProblem Solving Techniques
Problem Solving Techniques
 

Último

HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVHARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVRajaP95
 
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Serviceranjana rawat
 
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escortsranjana rawat
 
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130Suhani Kapoor
 
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Christo Ananth
 
Coefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxCoefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxAsutosh Ranjan
 
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...Christo Ananth
 
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICSHARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICSRajkumarAkumalla
 
Extrusion Processes and Their Limitations
Extrusion Processes and Their LimitationsExtrusion Processes and Their Limitations
Extrusion Processes and Their Limitations120cr0395
 
SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )Tsuyoshi Horigome
 
Introduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxIntroduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxupamatechverse
 
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...Call Girls in Nagpur High Profile
 
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSMANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSSIVASHANKAR N
 
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)Suman Mia
 
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Dr.Costas Sachpazis
 
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 

Último (20)

Roadmap to Membership of RICS - Pathways and Routes
Roadmap to Membership of RICS - Pathways and RoutesRoadmap to Membership of RICS - Pathways and Routes
Roadmap to Membership of RICS - Pathways and Routes
 
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVHARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
 
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
 
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
 
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
 
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
 
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
 
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
 
Coefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxCoefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptx
 
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
 
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICSHARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
 
Extrusion Processes and Their Limitations
Extrusion Processes and Their LimitationsExtrusion Processes and Their Limitations
Extrusion Processes and Their Limitations
 
SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )
 
Introduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxIntroduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptx
 
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
 
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSMANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
 
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)
 
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
 
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
 

06 java methods

  • 1. Mobile App:IT Eclipse Basics and Hello World IHS GIRLS TECH CLUB: Java Methods
  • 2. WHAT IS A METHOD? - A method is a set of code which is referred to by name and can be called (invoked) at any point in a program simply by utilizing the method's name. - This makes it easy to reuse portions of code so they do not have to be rewritten.
  • 3. WHAT IS WRONG WITH THIS CODE? Here is some code that greets people and says goodbye as they enter and leave the classroom.
  • 4. THE ANSWER - While there is nothing wrong with the previous code, you will notice that you are having to rewrite large portions of the code over and over again. - Imagine if you had 20 or more people coming in and leaving a class. That is a lot of extra typing! - This is where a method can come in handy.
  • 5. CURRENT PROGRAM INSTRUCTIONS Here is how the program is currently working: Code Flow main method: Print Hello Line Print Hello Line Print Hello Line Print Hello Line Print Hello Line Print Goodbye Line Print Hello Line Print Goodbye Line
  • 6. METHOD PROGRAM INSTRUCTIONS Here is how the program can work if we break it into methods. Notice the difference? Code Flow main method: hello(); hello(); hello(); hello(); hello(); goodbye(); hello(); goodbye(); hello method: Print Hello Line goodbye method: Print Goodbye Line
  • 7. YOU ALREADY KNOW METHODS - You have previously used the println and print methods in your previous programs. This means you are already familiar with how method calls work. When you want to print a line of text, you make a call to one of the print methods. - You do not need to know how the method works, you just need to know what the method does and how to call it.
  • 8. DIFFERENT METHOD STYLES There are two different types of methods: - Built In - These are methods that are part of the Java compiler. An example of this type of method is System.out.println - User Defined - These are methods that you can create and use in your own programs. You control the name of these methods and what they do.
  • 9. WRITING OUR FIRST METHOD Here is an example of how a basic method is written. Here we are building a simple hello method. Explanation: - "public" tells the program that anyone can use this method. The word "public" is optional in our example. - "static" tells the program that this method does not require an instance of an object to be called. - "void" tells the program that this method is not going to return anything. We will talk more about returning values later.
  • 10. EXPLANATION CONTINUED - "hello" is the name we want to give our method. This could be anything we want. - () is used to let the program know that we are not going to pass any variables (called parameters) into this method. More on method parameters later. - The method is then enclosed in {} (often called curly brackets). Anything inside these brackets gets run every time the method is called.
  • 11. CODE STRUCTURE - You will notice that within the hello method, the println line is indented. - This indentation does not control how the program functions, however it makes the program much easier to read. - The indentation is simply a tab character. - When building your programs it is important to focus on keeping your programs easy to read and keeping the indentation correct.
  • 12. FOLLOW ALONG WITH ECLIPSE - Now it is time to follow along with the code samples in the upcoming slides. - The first step is to open Eclipse and create a new project called "Greetings". - After creating the project, Create a new Java class called "Greetings". Make sure to check the box to add the "public static void main" method. - At the end of each code section, there will be a slide showing the full code. The code on these end slides will help to validate that you are putting the code in the correct location and you are maintaining the correct indentation structure.
  • 13. ADD THE FIRST METHOD The first step is to add the first method we built in a previous slide. This should be placed outside of the main method but within the Greetings class.
  • 14. FIRST METHOD CALL Now we will add a call to the hello method. Inside the main method add a call to hello.
  • 15. RUN YOUR PROGRAM Your program should look like this, go ahead and run it now. You should have one line printed out.
  • 16. CALLING THE METHOD MULTIPLE TIMES You can now reuse the hello method. The only problem with this is that it just prints the same message three times.
  • 17. METHOD PARAMETERS - Wouldn't it be great if we could get the name in the message to change? - With method parameters and variables it is possible. - Parameters are the variables that are listed as part of a method declaration. Each parameter must have a unique name and a defined data type.
  • 18. METHOD PARAMETERS - The first step is to determine what variable type (data type) we want our method parameter to use. In our example we will use String. - The second step is to decide on a variable name. In our example we will use first_name. - The third step is to add the parameter to the method definition. Lets do that now.
  • 19. ADDING THE PARAMETERS You will notice that after you change your hello method, your hello method calls look like this.
  • 20. ERRORS AND DEBUGGING - These pink squiggly lines let you know that there is an error in your program. - If you hover over the pink lines you will see an error message that can help you debug the error. - The definition of debug is to identify and remove errors from (computer hardware or software).
  • 21. RUNNING A PROGRAM WITH ERRORS - If you try to run your program with these errors in place, you will receive an error message. - If you proceed with running the program, there will be error messages listed in the Console window. - Now we will fix these errors.
  • 22. CALLING A METHOD WITH A PARAMETER To fix the errors, we simply need to pass in a String variable to our hello method. Go ahead and run your program now. You will notice that it runs without errors.
  • 23. MULTIPLE METHOD CALLS Now we will go ahead and make a few more hello method calls with different first names. If you run the program, you will notice that there seems to be a problem. Do you see what the problem is?
  • 24. USING THE FIRST_NAME VARIABLE We are not using the first_name variable that is getting passed into our hello method. We already know how to use variables, so let's go ahead and do that now. Now run the program. The output should look much better now, but there is still a spacing error. You can fix this one on your own before you move on.
  • 25. MULTIPLE PARAMETERS - Now let's add a second parameter to the hello method. - Multiple parameters can be added and separated by commas. Try This: - Make a 2nd parameter for the hello method. - Make it a string data type. - Give it a variable name of "last_name". - Add "last_name" to the print statement inside the hello method. - Add a last name to each of the hello method calls inside the main method. Use the last names "Smith", "Jones", and "Johnson"
  • 26. PRINTING THE FULL NAME CODE
  • 27. MORE PARAMETERS - The next step is to add the time the student checks into the class. - We will do this by adding two more parameters to the hello method. Try This: - Add two more parameters, both integer data types to the hello method. - Name the two new variables "hour" and "minute" - Add the times to the hello method calls. Name John Adam Tina Hour 9 9 9 Minute 25 27 32
  • 28. MORE PARAMETERS (TRY THIS) - Break up the print statements to use multiple print method calls instead of just one println method call. - Set it up so the output will display: "Hello John Smith, welcome to our class today. The time is 9:25." This one is a little more difficult so do not get discouraged. Try to walk through each step one at a time and use the past examples for help.
  • 30. THE OUTPUT We will continue adding even more to the Greeting class so it is important to make sure you are able to get the same output that is listed above.