Create a Simple Java program following the below instructions: A)Store your name
ID: 3785157 • Letter: C
Question
Create a Simple Java program following the below instructions:
A)Store your name (first and last together separated by one space ie. "Mary Flower") in a String object referenced by the reference variable myFullName.
B) Display myFullName in all upper case, display myFullName in all lower case, and next display the original myFullName.
C) Display the number of characters in the myFullName object (ie. My name is Mary Flower and it is 11 characters long)
D) Extract the first name from the myFullName object and store it in the myFirstName String object (utilize indexOf method to compute index of space and use this information in the call to substring)
E) Extract the last name from the myFullName object and store it in the myLastName String object (use a similar technique as above)
F) Display myLastName followed by comma followed by myFirstName (ie. My inverted name is Flower,Mary)
G) Store your friend's name (first, middle, and last together separated by one space ie "John Henry Smith") in a String object referenced by the reference variable friendsFullName.
H) Display number of characters in the friendsFullName object (ie. My friend's name is John Henry Smith and it is 16 characters long)
I) Extract the first name initial from the friendsFullName object and store it in the friendsFirstNameInitial char
J) Extract the last name initial from the friendsFullName object and store it in the friendsLastNameInitial char (utilize lastIndexOf method to get the index of the last space)
K)Display a message showing yours and your friend's initials (ie. MF and JS are friends)
Explanation / Answer
IntialsDemo.java
import java.util.Scanner;
public class IntialsDemo {
public static void main(String[] args) {
//Declaring variables
String myFullName,myFirstName,myLastName;
String friendsFullName,friendsFirstName,friendsLastName;
char friendsFirstNameInitial,friendsLastNameInitial;
//Scanner object is used to get the inputs entered by the user
Scanner sc=new Scanner(System.in);
//Getting your full name
System.out.print("Enter your Full Name :");
myFullName=sc.nextLine();
//Getting your friend's full name
System.out.print("Enter your Friend's Full Name :");
friendsFullName=sc.nextLine();
//Displaying the corresponding output
System.out.println("Your Full Name :"+myFullName.toUpperCase());
System.out.println("Your Full Name :"+myFullName.toLowerCase());
System.out.println("Your Original Full Name :"+myFullName);
System.out.println("My name is "+myFullName+" and it is "+myFullName.length()+" characters long");
int myspacepos=myFullName.indexOf(' ');
myFirstName=myFullName.substring(0,myspacepos);
myLastName=myFullName.substring(myspacepos+1);
System.out.println("Your Inverted name is "+myLastName+","+myFirstName);
System.out.println("My friend's name is "+friendsFullName+" and it is "+friendsFullName.length()+" characters long");
friendsFirstName=friendsFullName.substring(0, friendsFullName.indexOf(' '));
friendsFirstNameInitial=friendsFirstName.charAt(0);
friendsLastName=friendsFullName.substring(friendsFullName.lastIndexOf(' ')+1);
friendsLastNameInitial=friendsLastName.charAt(0);
char myFirstnameInitial=myFirstName.charAt(0);
char myLastnameInitial=myLastName.charAt(0);
System.out.println(myFirstnameInitial+""+myLastnameInitial+" and "+friendsFirstNameInitial+friendsLastNameInitial+" are friends");
}
}
___________________
output:
Enter your Full Name :Mary Flower
Enter your Friend's Full Name :John Henry Smith
Your Full Name :MARY FLOWER
Your Full Name :mary flower
Your Original Full Name :Mary Flower
My name is Mary Flower and it is 11 characters long
Your Inverted name is Flower,Mary
My friend's name is John Henry Smith and it is 16 characters long
M
F
MF and JS are friends
________________Thank You