Assignment 2
Create a Java program called UserNames that reads the user's
first and last name (separately), then prints a string composed of
the first 4 letters of the user's last name, followed by the first
letter of the user's first name, followed by a random number in the
range of 10 to 99 (inclusive). You can assume the first name is at
least one letter long and the last name is at least 4 letters.
After doing this once, the program should ask the user if there is
another user and then repeat if the answer is "y" or "Y".
Otherwise, the program should end.
Additional Requirements
The name of your Java Class that contains the main method should be
UserNames. All your code should be within the main method.
Your code should follow good coding practices, including good use
of whitespace (indents and line breaks) and use of both inline and
block comments.
You need to use meaningful identifier names that conform to
standard Java naming conventions.
SOURCE CODE: *Please follow the comments to better understand the code. **Please look at the Screenshot below and use this code to copy-paste. ***The code in the below screenshot is neatly indented for better understanding. import java.util.Random; import java.util.Scanner; public class UserNames { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); // repeat while (true) { // Prompt for first name System.out.print("\nEnter your first name: "); String firstName = scanner.next(); // prompt for last name System.out.print("Enter your last name: "); String lastName = scanner.next(); Random random = new Random(); String output = lastName.substring(0, 4) + firstName.charAt(0) + ((10 + random.nextInt(100)) % 100); System.out.println("The Output is: " + output); // ask if the user wants to repeat System.out.print("Do you want to continue...(y/n): "); char choice=scanner.next().charAt(0); if(choice=='y' || choice=='Y') continue; else { // STOP here System.out.println("Good Bye..!"); break; } } } }
=======================
SCREENSHOT:
OUTPUT:
Get Answers For Free
Most questions answered within 1 hours.