/
01 - Introduction to Java - Exercise 5

01 - Introduction to Java - Exercise 5

Task 1 - Create a simple method

  1. Create a new project - call it MethodsPractice.

  2. Create a class that will contain the main method.

  3. Create a new method - within the class but outside of the main method, create a method called greeting. This method will take one String argument name and return a String. The method needs to be declared public and static, for reasons we will learn later.

    public static String greeting(String name){ }
  4. In the body of the greeting method, use the argument name and concatenate it with "Hello ". Return the result from your method.

  5. Call the greeting method. In the main method, invoke the greeting method

    greeting("Martina");
  6. Run the main method. Does anything happen? Why?

Task 2 - Create some more methods

  1. 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.