In: Computer Science
Write a class called Name. A tester program is provided in Codecheck, but there is no starting code for the Name class.
The constructor takes a String parameter representing a person's full name. A name can have multiple words, separated by single spaces. The only non-letter characters in a name will be spaces or -, but not ending with either of them.
The class has the following methods. • public String getName() Gets the name string.
• public int consonants() Gets the number of consonants in the name. A consonant is any character that is not a vowel, the space or -. For this problem assume the vowels are aeiou. Ignore case. "a" and "A" are both vowels. "b" and "B" are both consonants. You can have only one if statement in the method and the if condition cannot have either && or ||. Do not use the switch statement, which is basically the same as an if statement with multiple alternatives. Hint: call method contains().
• public String initials() Gets the initials of the name. Do not use nested loops for the method. Hint for initials(): Each word after the first is preceded by a space. You can use the String method indexOf (" ", fromIndex) to control a while loop and to determine where a new word starts. This version of indexOf() returns the index of the first space starting at the fromIndex. The call of indexOf (" ", fromIndex) returns -1 if the space is not found in the string. Remember that the name does not have 2 consecutive spaces and does not end in a space.
CODE CHECK:
public class NameTester { public static void main(String[] args) { Name name = new Name("Allison Chung"); System.out.println(name.getName()); System.out.println("Expected: Allison Chung"); System.out.println(name.consonants()); System.out.println("Expected: 8"); name = new Name("a-abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ"); System.out.println(name.getName()); System.out.println("Expected: a-abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ"); System.out.println(name.consonants()); System.out.println("Expected: 42"); name = new Name("Alhambra Cohen"); System.out.println(name.initials()); System.out.println("Expected: AC"); name = new Name("George H W Bush"); System.out.println(name.initials()); System.out.println("Expected: GHWB"); name = new Name("John Jacob Jingleheimer Schmidt"); System.out.println(name.initials()); System.out.println("Expected: JJJS"); name = new Name("Zorro"); System.out.println(name.initials()); System.out.println("Expected: Z"); } }
Hi. I have answered a very similar question before. Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. If not, PLEASE let me know before you rate, I’ll help you fix whatever issues. Thanks
//Name.java
public class Name {
private String name;
// constructor taking and storing a name
public Name(String name) {
this.name = name;
}
// returns the name
public String getName() {
return name;
}
// returns the count of consonants
public int consonants() {
int count = 0;
// defining a string containing non consonant values
String non_consonants = "aeiou -";
// looping through the name
for (int i = 0; i < name.length(); i++) {
// if char at i on name variable is not present in non_consonants
// string, incrementing count
if (!non_consonants.contains("" + name.toLowerCase().charAt(i))) {
count++;
}
}
return count;
}
public String initials() {
String result = "";
// if name is not empty, appending first char to result. assuming name
// always start with a letter
if (!name.isEmpty()) {
result += name.toUpperCase().charAt(0);
}
// finding first index of space
int index = name.indexOf(" ");
// looping until index is -1
while (index != -1) {
// appending char at index+1 to result (after converting to upper
// case)
result += name.toUpperCase().charAt(index + 1);
// finding next index of space after index
index = name.indexOf(" ", index + 1);
}
// returning result
return result;
}
}
/*OUTPUT*/
Allison Chung
Expected: Allison Chung
8
Expected: 8
a-abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ
Expected: a-abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ
42
Expected: 42
AC
Expected: AC
GHWB
Expected: GHWB
JJJS
Expected: JJJS
Z
Expected: Z