158k views
3 votes
Assume that given , middle and family are three variables of type string that have been assigned values . Write an expression whose value is a string consisting of the first character of given followed by a period followed by the first character of middle followed by a period followed by the first character of family followed by a period: in other words, the initials of the name . So if the values of these three variables were "John" "Fitzgerald" "Kennedy", then the expression 's value would be "J.F.K."

User Ian Chu
by
5.3k points

1 Answer

7 votes

Answer:

String initials = given.charAt(0) + "." + middle.charAt(0) + "." + family.charAt(0) + "." ;

Step-by-step explanation:

The code has been written in Java.

To get the first character of a String variable say, name, the syntax is:

name.charAt(0);

Zero(0) in the above expression indicated the position of the first character.

Therefore, to get the first character of the string given, we have

given.charAt(0).

To get the first character of the string middle, we have:

middle.charAt(0);

To get the first character of the string family, we have:

family.charAt(0);

Concatenating all of these first characters together and separating them by a period(.) we have;

given.charAt(0) + "." + middle.charAt(0) + "." + family.charAt(0) + "." ;

These will form a string which can be stored in a String variable, say 'initials', as follows;

String initials = given.charAt(0) + "." + middle.charAt(0) + "." + family.charAt(0) + "." ;

Hope this helps!

User Serginho
by
5.2k points