62.3k views
4 votes
Create a Java programming example of passing a name and age to a method. in the Main method, create variables for name and age. use the Scanner class to input the name and then the age. create a method called displayNameAge. pass the name and age to the method. display --"The name is _" -- "The age is ____" from the method. example: -"The name is Tom" "The age is 15"

User Verrtex
by
4.9k points

1 Answer

6 votes

Answer:

import java.util.Scanner;

public class num14 {

public static void main(String[] args) {

Scanner input = new Scanner(System.in);

System.out.println("Enter a Name");

String name = input.next();

System.out.println("Enter the age");

int age = input.nextInt();

//Calling the Method

displayNameAge(name,age);

}

public static void displayNameAge(String name, int age){

System.out.println("The name is "+name+". The age is "+ age);

}

}

Step-by-step explanation:

Using Java Programming language:

  • Firstly create a method called displayNameAge() That accepts two parameter a string for name and an int for age
  • The method uses string concatenation to produce the required output
  • In a main method the scanner class is used to prompt user to enter values for name and age. These are stored in two seperate variable
  • The method displayNameAge() is then called and passed these values as arguments.

User JasonTS
by
4.4k points