In: Computer Science
Java
Write a method, makeUserName, that is passed two Strings and an int: the first is a first name, the second is a last name, and the last is a random number. The method returns a user name that contains the last character of the first name, the first five characters of the last name (assume there are five or more characters in last name), and the random number. An example: Assume that “John”, “Smith”, 45, are passed when the method is called, it will return “nSmith45” as the user name.
import java.util.*;
class Test
{
public static void main (String[] args)
{
Scanner input = new
Scanner(System.in);
System.out.println("Enter first
name of user : ");
String firstName =
input.next();
System.out.println("Enter last name
of user : ");
String lastName =
input.next();
int num =
(int)(Math.random()*99+1); // random num between 1 and 100
System.out.println("User name :
"+makeUserName(firstName,lastName,num));
}
public static String makeUserName(String
firstName,String lastName, int num)
{
String result;
result = firstName.charAt(0)+
lastName + num;
return result;
}
}
Output:
Enter first name of user : John Enter last name of user : Smith User name : JSmith5
Do ask if any doubt. Please upvote.