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...
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 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...
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 C++ a program that shows a class called gamma that keeps track of how many...
Write C++ a program that shows a class called gamma that keeps track of how many objects of itself there are. Each gamma object has its own identification called ID. The ID number is set equal to total of current gamma objects when an object is created. Test you class by using the main function below. int main()    {    gamma g1;    gamma::showtotal();    gamma g2, g3;    gamma::showtotal();    g1.showid();    g2.showid();    g3.showid();    cout <<...
Write a C++ program that creates a base class called Vehicle that includes two pieces of...
Write a C++ program that creates a base class called Vehicle that includes two pieces of information as data members, namely: wheels (type int) weight (type float) Program requirements (Vehicle class): Provide set and a get member functions for each data member. Your class should have a constructor with two parameters (one for each data member) and it must use the set member functions to initialize the two data members. Provide a pure virtual member function by the name displayData()...
Write a program with class name ForLoops that uses for loops to perform the following steps:1....
Write a program with class name ForLoops that uses for loops to perform the following steps:1. Prompt the user to input two positive integers: firstNum and secondNum (firstNum must be smallerthan secondNum).2. Output all the even numbers between firstNum and secondNum inclusive.3. Output the sum of all the even numbers between firstNum and secondNum inclusive.4. Output all the odd numbers between firstNum and secondNum inclusive.5. Output the sum of all the odd numbers between firstNum and secondNum inclusive. Java programming
Write a class called Account that contains: • Three privateinstance variables: name (String), account (String),...
Write a class called Account that contains: • Three private instance variables: name (String), account (String), account balance (double). • One constructor, which constructs all instances with the values given. • Getters and setters for all the instance variables. • Debit function that takes an amount from the user and subtract the balance (make sure the balance will not drop below zero after the operation, if this was the case, the function should return false else it should return true)...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT