Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a program that separately prompts the user for a first name and last name

ID: 3743163 • Letter: W

Question

Write a program that separately prompts the user for a first name and last name and outputs a string containing the following information, in order: a. First letter of the user's name. b. First five letters of the user's last name. c. A random two-digit integer You must construct the desired string ensuring all characters are lowercase; output the identification string accordingly. Assume the last name contains at least 5 characters. You must use the Random (java.util.Random) class to generate random numbers. As an example, input of Chris Alvin may result in calvin15.

Explanation / Answer

JAVA PROGRAM

import java.util.Random;
import java.util.Scanner;

class UserName
{

public static void main(String args[])
{
// declare variables
String fname,lname;
int n=0;
Scanner scr=new Scanner(System.in); // create Scanner object scr

System.out.print("Enter First Name: "); fname=scr.next(); // read First Name
System.out.print("Enter Last Name: "); lname=scr.next(); // read Last Name

Random rand=new Random(); // create Random object rand
// generate random 2 digit numbers
for(int i=10;i<=99;i++)
n=rand.nextInt(i);
// Extract first character from fname using charAt(0) and convert lowercase letter using toLowerCase()

// Extract first 5 letters from lname using substring
// and convert into lowercase letter using toLowerCase()
// after extract and convert the letters concatnation with random 2 digit number
String con=Character.toLowerCase(fname.charAt(0))+lname.substring(0,5).toLowerCase()+n;

System.out.println("User Name is: "+con); // display user name
}
}

OUTPUT

F:>javac UserName.java

F:>java UserName
Enter First Name: Chris
Enter Last Name: Alvin
User Name is: calvin61

F:>java UserName
Enter First Name: Philips
Enter Last Name: Washington
User Name is: pwashi93

F:>java UserName
Enter First Name: Robin
Enter Last Name: Michael
User Name is: rmicha83

F:>java UserName
Enter First Name: Linda
Enter Last Name: Elizabeth
User Name is: leliza83

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote