/
01 - Introduction to Java - Exercise 5
01 - Introduction to Java - Exercise 5
Task 1 - Create a simple method
Create a new project - call it
MethodsPractice
.Create a class that will contain the
main
method.Create a new method - within the class but outside of the main method, create a method called
greeting
. This method will take one String argumentname
and return a String. The method needs to be declaredpublic
andstatic
, for reasons we will learn later.public static String greeting(String name){ }
In the body of the
greeting
method, use the argumentname
and concatenate it with "Hello ". Return the result from your method.Call the greeting method. In the
main
method, invoke the greeting methodgreeting("Martina");
Run the main method. Does anything happen? Why?
Task 2 - Create some more methods
Reopen your code from the previous exercise - the one with the loops. Pick any task you want and try to identify and create some methods.