SlideShare a Scribd company logo
1 of 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.

More Related Content

Similar to 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 to 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
 

Recently uploaded

1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf
1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf
1_Introduction + EAM Vocabulary + how to navigate in EAM.pdfAldoGarca30
 
AIRCANVAS[1].pdf mini project for btech students
AIRCANVAS[1].pdf mini project for btech studentsAIRCANVAS[1].pdf mini project for btech students
AIRCANVAS[1].pdf mini project for btech studentsvanyagupta248
 
Thermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - VThermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - VDineshKumar4165
 
Kuwait City MTP kit ((+919101817206)) Buy Abortion Pills Kuwait
Kuwait City MTP kit ((+919101817206)) Buy Abortion Pills KuwaitKuwait City MTP kit ((+919101817206)) Buy Abortion Pills Kuwait
Kuwait City MTP kit ((+919101817206)) Buy Abortion Pills Kuwaitjaanualu31
 
Design For Accessibility: Getting it right from the start
Design For Accessibility: Getting it right from the startDesign For Accessibility: Getting it right from the start
Design For Accessibility: Getting it right from the startQuintin Balsdon
 
Unit 4_Part 1 CSE2001 Exception Handling and Function Template and Class Temp...
Unit 4_Part 1 CSE2001 Exception Handling and Function Template and Class Temp...Unit 4_Part 1 CSE2001 Exception Handling and Function Template and Class Temp...
Unit 4_Part 1 CSE2001 Exception Handling and Function Template and Class Temp...drmkjayanthikannan
 
Introduction to Serverless with AWS Lambda
Introduction to Serverless with AWS LambdaIntroduction to Serverless with AWS Lambda
Introduction to Serverless with AWS LambdaOmar Fathy
 
Hospital management system project report.pdf
Hospital management system project report.pdfHospital management system project report.pdf
Hospital management system project report.pdfKamal Acharya
 
S1S2 B.Arch MGU - HOA1&2 Module 3 -Temple Architecture of Kerala.pptx
S1S2 B.Arch MGU - HOA1&2 Module 3 -Temple Architecture of Kerala.pptxS1S2 B.Arch MGU - HOA1&2 Module 3 -Temple Architecture of Kerala.pptx
S1S2 B.Arch MGU - HOA1&2 Module 3 -Temple Architecture of Kerala.pptxSCMS School of Architecture
 
DC MACHINE-Motoring and generation, Armature circuit equation
DC MACHINE-Motoring and generation, Armature circuit equationDC MACHINE-Motoring and generation, Armature circuit equation
DC MACHINE-Motoring and generation, Armature circuit equationBhangaleSonal
 
Online electricity billing project report..pdf
Online electricity billing project report..pdfOnline electricity billing project report..pdf
Online electricity billing project report..pdfKamal Acharya
 
Online food ordering system project report.pdf
Online food ordering system project report.pdfOnline food ordering system project report.pdf
Online food ordering system project report.pdfKamal Acharya
 
Wadi Rum luxhotel lodge Analysis case study.pptx
Wadi Rum luxhotel lodge Analysis case study.pptxWadi Rum luxhotel lodge Analysis case study.pptx
Wadi Rum luxhotel lodge Analysis case study.pptxNadaHaitham1
 
Unleashing the Power of the SORA AI lastest leap
Unleashing the Power of the SORA AI lastest leapUnleashing the Power of the SORA AI lastest leap
Unleashing the Power of the SORA AI lastest leapRishantSharmaFr
 
Orlando’s Arnold Palmer Hospital Layout Strategy-1.pptx
Orlando’s Arnold Palmer Hospital Layout Strategy-1.pptxOrlando’s Arnold Palmer Hospital Layout Strategy-1.pptx
Orlando’s Arnold Palmer Hospital Layout Strategy-1.pptxMuhammadAsimMuhammad6
 
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXssuser89054b
 
NO1 Top No1 Amil Baba In Azad Kashmir, Kashmir Black Magic Specialist Expert ...
NO1 Top No1 Amil Baba In Azad Kashmir, Kashmir Black Magic Specialist Expert ...NO1 Top No1 Amil Baba In Azad Kashmir, Kashmir Black Magic Specialist Expert ...
NO1 Top No1 Amil Baba In Azad Kashmir, Kashmir Black Magic Specialist Expert ...Amil baba
 
Engineering Drawing focus on projection of planes
Engineering Drawing focus on projection of planesEngineering Drawing focus on projection of planes
Engineering Drawing focus on projection of planesRAJNEESHKUMAR341697
 

Recently uploaded (20)

1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf
1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf
1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf
 
AIRCANVAS[1].pdf mini project for btech students
AIRCANVAS[1].pdf mini project for btech studentsAIRCANVAS[1].pdf mini project for btech students
AIRCANVAS[1].pdf mini project for btech students
 
Thermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - VThermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - V
 
Kuwait City MTP kit ((+919101817206)) Buy Abortion Pills Kuwait
Kuwait City MTP kit ((+919101817206)) Buy Abortion Pills KuwaitKuwait City MTP kit ((+919101817206)) Buy Abortion Pills Kuwait
Kuwait City MTP kit ((+919101817206)) Buy Abortion Pills Kuwait
 
Design For Accessibility: Getting it right from the start
Design For Accessibility: Getting it right from the startDesign For Accessibility: Getting it right from the start
Design For Accessibility: Getting it right from the start
 
Unit 4_Part 1 CSE2001 Exception Handling and Function Template and Class Temp...
Unit 4_Part 1 CSE2001 Exception Handling and Function Template and Class Temp...Unit 4_Part 1 CSE2001 Exception Handling and Function Template and Class Temp...
Unit 4_Part 1 CSE2001 Exception Handling and Function Template and Class Temp...
 
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak HamilCara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
 
Introduction to Serverless with AWS Lambda
Introduction to Serverless with AWS LambdaIntroduction to Serverless with AWS Lambda
Introduction to Serverless with AWS Lambda
 
Hospital management system project report.pdf
Hospital management system project report.pdfHospital management system project report.pdf
Hospital management system project report.pdf
 
S1S2 B.Arch MGU - HOA1&2 Module 3 -Temple Architecture of Kerala.pptx
S1S2 B.Arch MGU - HOA1&2 Module 3 -Temple Architecture of Kerala.pptxS1S2 B.Arch MGU - HOA1&2 Module 3 -Temple Architecture of Kerala.pptx
S1S2 B.Arch MGU - HOA1&2 Module 3 -Temple Architecture of Kerala.pptx
 
DC MACHINE-Motoring and generation, Armature circuit equation
DC MACHINE-Motoring and generation, Armature circuit equationDC MACHINE-Motoring and generation, Armature circuit equation
DC MACHINE-Motoring and generation, Armature circuit equation
 
Online electricity billing project report..pdf
Online electricity billing project report..pdfOnline electricity billing project report..pdf
Online electricity billing project report..pdf
 
Online food ordering system project report.pdf
Online food ordering system project report.pdfOnline food ordering system project report.pdf
Online food ordering system project report.pdf
 
Wadi Rum luxhotel lodge Analysis case study.pptx
Wadi Rum luxhotel lodge Analysis case study.pptxWadi Rum luxhotel lodge Analysis case study.pptx
Wadi Rum luxhotel lodge Analysis case study.pptx
 
Unleashing the Power of the SORA AI lastest leap
Unleashing the Power of the SORA AI lastest leapUnleashing the Power of the SORA AI lastest leap
Unleashing the Power of the SORA AI lastest leap
 
Orlando’s Arnold Palmer Hospital Layout Strategy-1.pptx
Orlando’s Arnold Palmer Hospital Layout Strategy-1.pptxOrlando’s Arnold Palmer Hospital Layout Strategy-1.pptx
Orlando’s Arnold Palmer Hospital Layout Strategy-1.pptx
 
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
 
FEA Based Level 3 Assessment of Deformed Tanks with Fluid Induced Loads
FEA Based Level 3 Assessment of Deformed Tanks with Fluid Induced LoadsFEA Based Level 3 Assessment of Deformed Tanks with Fluid Induced Loads
FEA Based Level 3 Assessment of Deformed Tanks with Fluid Induced Loads
 
NO1 Top No1 Amil Baba In Azad Kashmir, Kashmir Black Magic Specialist Expert ...
NO1 Top No1 Amil Baba In Azad Kashmir, Kashmir Black Magic Specialist Expert ...NO1 Top No1 Amil Baba In Azad Kashmir, Kashmir Black Magic Specialist Expert ...
NO1 Top No1 Amil Baba In Azad Kashmir, Kashmir Black Magic Specialist Expert ...
 
Engineering Drawing focus on projection of planes
Engineering Drawing focus on projection of planesEngineering Drawing focus on projection of planes
Engineering Drawing focus on projection of planes
 

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.