Solution: Create a Method

  • public class Main – defines the class Main.

  • public static void sayHiTo(String name) – a method that takes a name as input and prints “Hi, [name]!”.

  • public static int add(int a, int b) – a method that takes two integers, adds them, and returns the result.

  • public static void main(String[] args) – main method where execution starts.

  • sayHiTo("Saad"); – calls the method to print “Hi, Saad!”.

  • int result = add(5, 7); – calls the add method with 5 and 7, storing the result in result.

  • System.out.println("Sum is: " + result); – prints “Sum is: 12”.

Solution: Create a Method

  • public class Main – defines the class Main.

  • public static void sayHiTo(String name) – a method that takes a name as input and prints “Hi, [name]!”.

  • public static int add(int a, int b) – a method that takes two integers, adds them, and returns the result.

  • public static void main(String[] args) – main method where execution starts.

  • sayHiTo("Saad"); – calls the method to print “Hi, Saad!”.

  • int result = add(5, 7); – calls the add method with 5 and 7, storing the result in result.

  • System.out.println("Sum is: " + result); – prints “Sum is: 12”.