132k views
2 votes
Write a program that prompts for and reads the user's first and last name (separately). Then print a string composed of the first letter of the user's first name, followed by the first five characters of the user's last name, followed by a random number in the range 10 to 99.

User LulzCow
by
4.7k points

1 Answer

1 vote

Answer:

import java.util.Random;

import java.util.Scanner;

public class num14 {

public static void main(String[] args) {

Scanner in = new Scanner(System.in);

System.out.println("Enter Your first Name");

String firstName = in.next();

System.out.println("Enter Your Last Name");

String lastName = in.next();

char firstLetter = firstName.charAt(0);

String firstFiveLetters = lastName.substring(0,5);

System.out.print(firstLetter+firstFiveLetters);

Random rand = new Random();

int randomNum = rand.nextInt(100)+10;

System.out.println(randomNum);

}

}

Step-by-step explanation:

  1. Using Java programming language
  2. Import Scanner class to receive user input and Random class to generate random number
  3. Prompt user for first and last name separately and store in variables firstName and lastName.
  4. Extract the first character of the firstname char firstLetter = firstName.charAt(0);
  5. Extract the first five characters of the last name String firstFiveLetters = lastName.substring(0,5);
  6. Concatenate the two extracted values and output
  7. Generate a random number between 10 and 99 and output.
  8. Observe that the random number utilizes a non-inclusive upperbound. that is rand.nextInt(100) will generate numbers from 0-99. We add 10 to this value to ensure it is between 10-99
User Slikts
by
3.3k points