Question

In: Computer Science

Write a class called Name. A tester program is provided in Codecheck, but there is no...

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");
    }
}

Solutions

Expert Solution

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

Related Solutions

Write a class called Name. A tester program is provided in Codecheck, but there is no...
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()...
Task: Write a program that creates a class Apple and a tester to make sure the...
Task: Write a program that creates a class Apple and a tester to make sure the Apple class is crisp and delicious. Instructions: First create a class called Apple  The class Apple DOES NOT HAVE a main method  Some of the attributes of Apple are o Type: A string that describes the apple. It may only be of the following types:  Red Delicious  Golden Delicious  Gala  Granny Smith o Weight: A decimal value representing...
3. write a program that uses a class called "garment" that is derived from the class...
3. write a program that uses a class called "garment" that is derived from the class "fabric" and display some values of measurement. 20 pts. Based on write a program that uses the class "fabric" to display the square footage of a piece of large fabric.           class fabric            {                private:                    int length;                    int width;                    int...
AskInfoPrintInfo Write a program called AskInfoPrintInfo. It should contain a class called AskInfoPrintInfo that contains the...
AskInfoPrintInfo Write a program called AskInfoPrintInfo. It should contain a class called AskInfoPrintInfo that contains the method main. The program should ask for information from the user and then print it. Look at the examples below and write your program so it produces the same input/output. Examples (the input from the user is in bold face) % java AskInfoPrintInfo enter your name: jon doe enter your address (first line): 23 infinite loop lane enter your address (second line): los angeles,...
Write a program that utilizes a Professor class. A professor has a first name, last name,...
Write a program that utilizes a Professor class. A professor has a first name, last name, affiliation, easiness grade, and a helpfulness grade. Grades range from 1 (lowest) to 5 (highest). The Professor class has two constructors. The first constructor initiates the object with just the first name, the last name, and the affiliation. The default value for easiness and helpfulness grade is 3. The second constructor initiates the object using the first name, the last name, the affiliation, and...
Write a program called whilebun.py, in which the user is asked to guess the name of...
Write a program called whilebun.py, in which the user is asked to guess the name of your favorite DBVac vacuum cleaner model. Your program should: · Include a comment in the first line with your name. · Include comments describing each major section of code. · Set a variable called vacname to be equal to the name of your favorite DBVac vacuum cleaner model. · Ask the user to guess the name. Allow the user to make up to three...
in C++ Requirements: Write a program that creates a new class called Customer. The Customer class...
in C++ Requirements: Write a program that creates a new class called Customer. The Customer class should include the following private data: name - the customer's name, a string. phone - the customer's phone number, a string. email - the customer's email address, a string. In addition, the class should include appropriate accessor and mutator functions to set and get each of these values. Have your program create one Customer objects and assign a name, phone number, and email address...
Java program Write a class called Animal that contains a static variable called count to keep...
Java program Write a class called Animal that contains a static variable called count to keep track of the number of animals created. Your class needs a getter and setter to manage this resource. Create another variable called myCount that is assigned to each animal for each animal to keep track of its own given number. Write a getter and setter to manage the static variable count so that it can be accessed as a class resource
Write a class called Person that has two private data members - the person's name and...
Write a class called Person that has two private data members - the person's name and age. It should have an init method that takes two values and uses them to initialize the data members. It should have a get_age method. Write a separate function (not part of the Person class) called std_dev that takes as a parameter a list of Person objects and returns the standard deviation of all their ages (the population standard deviation that uses a denominator...
write program in java Create a class named PersonalDetails with the fields name and address. The...
write program in java Create a class named PersonalDetails with the fields name and address. The class should have a parameterized constructor and get method for each field.  Create a class named Student with the fields ID, PersonalDetails object, major and GPA. The class should have a parameterized constructor and get method for each field. Create an application/class named StudentApp that declare Student object. Prompts (GUI input) the user for student details including ID, name, address, major and GPA....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT