In: Computer Science
Write a program (name it firstMiddleLast _yourInitials.java (yourInitials represents your initials) that will:
1. Ask the user (include appropriate dialog) to enter their:
first name
middle name
last name
save each of the above as a String variable as follows:
firstName
middleName
lastName
2. Print to the screen the number of characters in each of the
names (first, middle and last)
3. Print to the screen total number of characters in all three names. Include appropriate dialog in your output.
4. Print to the screen the initials of the person (first letter of the first, middle and last names) in all capitals with no space or lines between them. For example JFK.
5. Print to the screen the total of the ASCII values of the initials printed in #4 above. For example the initials JFK would sum to 219 (74 + 70 + 75). Remember that the ASCII values are as follows and to find the ASCII value of a single character by casting the character to an int by using (int).
A |
65 |
N |
78 |
B |
66 |
O |
79 |
C |
67 |
P |
80 |
D |
68 |
Q |
81 |
E |
69 |
R |
82 |
F |
70 |
S |
83 |
G |
71 |
T |
84 |
H |
72 |
U |
85 |
I |
73 |
V |
86 |
J |
74 |
W |
87 |
K |
75 |
X |
88 |
L |
76 |
Y |
89 |
M |
77 |
Z |
90 |
import java.util.Scanner;
class Test{
//function for charcter count of String name
public int charCount(String nm){
int count = 0;
//Counts each character except
space
for(int x = 0; x < nm.length();
x++) {
if(nm.charAt(x)
!= '\n')
count++;
}
return count;
}
//function for capitalize fitst character in String
name
public String getFirstChar(String nm){
String capCh="";
for(int
x=0;x<nm.length();x++){
String first = nm.substring(0,1); // get first character of each
word
capCh = first.toUpperCase(); // capitalize first character
}
return capCh;
}
//main function starts
public static void main(String[] args){
//taking input from user first
,middle and last names
Scanner read =new
Scanner(System.in);
System.out.print("Enter first name
: ");
String firstName =
read.nextLine();
System.out.print("Enter middle name
: ");
String middleName =
read.nextLine();
System.out.print("Enter last name :
");
String lastName =
read.nextLine();
//creating object for Test
class
Test obj = new Test();
int fCount =
obj.charCount(firstName);
int mCount =
obj.charCount(middleName);
int lCount =
obj.charCount(lastName);
//displaying count of characters in
names
System.out.println("first name
characters count : "+fCount);
System.out.println("middle name
characters count : "+mCount);
System.out.println("last name
characters count : "+lCount);
//displaying count of all
characters in 3 names
System.out.println("number of
characters in 3 names : "+ (fCount+mCount+lCount));
//converting every first letter of
name as Capitalize
String ch1 =
obj.getFirstChar(firstName);
String ch2 =
obj.getFirstChar(middleName);
String ch3 =
obj.getFirstChar(lastName);
//displaying every first letter of
name as Capitalize
System.out.println("initials : "+
(ch1+ch2+ch3));
//process of getting ascii value of
initials
char c1 = ch1.charAt(0);
char c2 = ch2.charAt(0);
char c3 = ch3.charAt(0);
int char1=c1;
int char2=c2;
int char3=c3;
//displaying initials Ascii
value
System.out.println("Ascii value :
"+ ((int)char1+(int)char2+(int)char3) );
}
}
-------------------------------------------------------------------------------------------------------------
name the File as Test.java