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 method:
• 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.
NameTester.java
/** * A Java tester program for class Name. * * @author Kathleen O'Brien, Qi Yang * @version 2020-07-25 */ 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: 0"); 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: 0"); name = new Name("Alhambra Cohen"); System.out.println(name.initials()); System.out.println("Expected: null"); name = new Name("George H W Bush"); System.out.println(name.initials()); System.out.println("Expected: null"); name = new Name("John Jacob Jingleheimer Schmidt"); System.out.println(name.initials()); System.out.println("Expected: null"); name = new Name("Zorro"); System.out.println(name.initials()); System.out.println("Expected: null"); } }
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
Note: NameTester class was not correctly implemented, I have modified that class also. Attached at the bottom, use it.
//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), followed by a space
result += name.toUpperCase().charAt(index + 1) + " ";
// finding next index of space after index
index = name.indexOf(" ", index + 1);
}
// returning result after removing trailing space.
return result.trim();
}
}
//NameTester.java (corrected)
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: 0");
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: A C");
name = new Name("George H W Bush");
System.out.println(name.initials());
System.out.println("Expected: G H W B");
name = new Name("John Jacob Jingleheimer Schmidt");
System.out.println(name.initials());
System.out.println("Expected: J J J S");
name = new Name("Zorro");
System.out.println(name.initials());
System.out.println("Expected: Z");
}
}
/*OUTPUT*/
Allison Chung
Expected: Allison Chung
8
Expected: 0
a-abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ
Expected: a-abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ
42
Expected: 42
A C
Expected: A C
G H W B
Expected: G H W B
J J J S
Expected: J J J S
Z
Expected: Z