Question

In: Computer Science

a.A method that accepts no parameters and returns the length of the name, i.e., the sum...

a.A method that accepts no parameters and returns the length of the name, i.e., the sum of the lengths of the three parts of the name.

b.A method that accepts no parameters and returns a String consisting of the three initials IN UPPERCASE.

c.A method that accepts an integer n and returns the nth character in the full three part name.

d.A method that accepts no parameters and returns a String consisting of the last name followed by a comma followed by the first name followed by the middle name. Remember to put spaces between the names.

e.A method that accepts a String and returns true if the String is equal to the first name (use the equals method in the String class!). This is not quite the proper way to define an equals method, as we will learn later.

f.A method that accepts a Name object and returns true if the three parts of the name object are the same as the three parts of “this” Name object.

Create a class called NameDriverwhich contains a main method. Inside the main method, instantiate a Name object using your name and test your methods to make sure they work.Does it work for all name

Solutions

Expert Solution

Thanks for the question. Below is the code you will be needing. Let me know if you have any doubts or if you need anything to change. 

If you are satisfied with the solution, please leave a +ve feedback : ) Let me know for any help with any other questions.

Thank You!
===========================================================================


public class Name {

    private String firstName;
    private String middleName;
    private String lastName;

    public Name(String firstName, String lastName) {
        this.firstName = firstName;
        middleName = "";
        this.lastName = lastName;
    }

    public Name(String firstName, String middleName, String lastName) {
        this.firstName = firstName;
        this.middleName = middleName;
        this.lastName = lastName;
    }

    //a.A method that accepts no parameters and returns the length of
    // the name, i.e., the sum of the lengths of the three parts of the name.
    public int length() {

        return firstName.length() + middleName.length() + lastName.length();
    }

    //b.A method that accepts no parameters and returns a String
    // consisting of the three initials IN UPPERCASE
    public String initials() {
        String initials = "";
        if (firstName.length() != 0) initials = String.valueOf(firstName.charAt(0));
        if (middleName.length() != 0) initials += String.valueOf(middleName.charAt(0));
        if (lastName.length() != 0) initials += String.valueOf(lastName.charAt(0));

        return initials.toUpperCase();
    }

    //c.A method that accepts an integer n and returns the nth character in the full three part name.
    public char nthCharacter(int n) {

        if (n < 0 || n >= length()) {
            System.out.println("Error: Invalid index: " + n);
            return ' ';
        } else {
            return (firstName + middleName + lastName).charAt(n);
        }
    }

    //  d.A method that accepts no parameters and returns a
    // String consisting of the last name followed by a comma
    // followed by the first name followed by the middle name. Remember to put spaces between the names.
    public String shortName() {
        return lastName + ", " + firstName + " " + middleName;
    }

    //e.A method that accepts a String and returns true if the String is equal to the first name
    public boolean isFirstNameSame(String firstName) {
        return firstName.equals(this.firstName);
    }


    public boolean equals(Name name) {
        if (this == name) return true;

        return firstName.equals(name.firstName) &&
                middleName.equals(name.middleName) &&
                lastName.equals(name.lastName);
    }
}

===================================================================

public class NameDriver {


    public static void main(String[] args) {

        Name name = new Name("George", "Washington", "Bush");
        System.out.println("name.length() = " + name.length());
        System.out.println("name.initials() = " + name.initials());
        System.out.println("name.shortName() = " + name.shortName());
        System.out.println("name.isFirstNameSame(\"George\") = " + name.isFirstNameSame("George"));
        System.out.println("name.nthCharacter(10) = " + name.nthCharacter(10));
        System.out.println("name.equals(new Name(\"Barack\",\"Obama\")) = " + name.equals(new Name("Barack", "Obama")));

    }


}

===================================================================


Related Solutions

Write a method sumTo that accepts an integer parameter n and returns the sum of the...
Write a method sumTo that accepts an integer parameter n and returns the sum of the first n reciprocals. In other words: sumTo(n) returns: 1 + 1/2 + 1/3 + 1/4 + ... + 1/n For example, the call of sumTo(2) should return 1.5. The method should return 0.0 if passed the value 0 and should print an error message and return -1 if passed a value less than 0. Include a loop. Please help for Java programming.
Oracle - Create a procedure that accepts product ID as a parameter and returns the name...
Oracle - Create a procedure that accepts product ID as a parameter and returns the name of the product from ProductTable table. Add exception handling to catch if product ID is not in the table. Table to use: CREATE TABLE ProductTable(     ProductID INTEGER NOT NULL primary key,     ProductName VARCHAR(50) NOT NULL,     ListPrice NUMBER(10,2),     Category INTEGER NOT NULL ); / INSERT INTO ProductTable VALUES(299,'Chest',99.99,10); INSERT INTO ProductTable VALUES(300,'Wave Cruiser',49.99,11); INSERT INTO ProductTable VALUES(301,'Megaland Play Tent',59.99,11); INSERT INTO...
Write code for a short method that does the following: accepts two strings as parameters, first...
Write code for a short method that does the following: accepts two strings as parameters, first name, and last name; Outputs the following message, concatenated together in one line of output:
Code in Java Write a recursive method, reverseString, that accepts a String and returns the String...
Code in Java Write a recursive method, reverseString, that accepts a String and returns the String reversed. Write a recursive method, reverseArrayList, that accepts an ArrayList of Strings and returns the ArrayList in reserve order in reserve order of the input ArrayList. Write a main method that asks the user for a series of Strings, until the user enters “Done” and puts them in an ArrayList. Main should make use to reverseArrayList and reverseString to reverse each String in the...
(java) Part 1 Write a method named compare that accepts two String arrays as parameters and...
(java) Part 1 Write a method named compare that accepts two String arrays as parameters and returns a boolean. The method should return true if both arrays contain the same values (case sensitive), otherwise returns false. Note - the two arrays must be the same length and the values must be in the same order to return true. Part  2 Write a method named generateArray that accepts an int size as its parameter and returns an int[] array where each element...
java/netbeans Write a recursive method, reverseString, that accepts a String and returns the String reversed. Write...
java/netbeans Write a recursive method, reverseString, that accepts a String and returns the String reversed. Write a recursive method, reverseArrayList, that accepts an ArrayList of Strings and returns an ArrayList in reserve order of the input ArrayList. Write a main method that asks the user for a series of Strings, until the user enters “Done” and puts them in an ArrayList. Main should make use to reverseArrayList and reverseString to reverse each String in the ArrayList and then reverse the...
CS 209 Data Structure 2. Create a method that takes a HashMap and returns the sum...
CS 209 Data Structure 2. Create a method that takes a HashMap and returns the sum of the keys of the HashMap. 3. Create a method that takes a HashMap and returns the sum of all keys and values of the HashMap. For example, if the input is [1=9, 3=6, 4=9, 6=8, 7=6] then the method should return 59.
Method: ArrayList<Integer> diff(ArrayList<Integer> list1, ArrayList<Integer> list2) diff() method accepts two ArrayLists of Integer and returns the...
Method: ArrayList<Integer> diff(ArrayList<Integer> list1, ArrayList<Integer> list2) diff() method accepts two ArrayLists of Integer and returns the union of elements in two lists. For example: list1 contains elements [1, 2, 3, 4, 5] list2 contains elements [3, 4, 5, 6, 7] Diff() method should return an array list with elements [1, 2, 3, 4, 5, 6, 7].
Language: Python Function name : sort_by_rating Parameters : file (.csv file), category (string), order (boolean) Returns:...
Language: Python Function name : sort_by_rating Parameters : file (.csv file), category (string), order (boolean) Returns: None Description : You want to see what order the items of a particular category in your file would be in if you sorted them by rating. Given a file, a category of clothing, and an order boolean (True for ascending order, False for descending order), create a function that writes a file called “sorted_items.csv” that includes all of the items in the specified...
Write a method, remove, that takes three parameters: an array of integers, the length of the array, and an integer called removeItem.
Java programming:Write a method, remove, that takes three parameters: an array of integers, the length of the array, and an integer called removeItem. The method should find and delete the first occurrence of removeItem in the array. If the value does not exist or the array is empty, output an appropriate message. (Note that after deleting the element, the array size is reduced by 1.) You may assume that the array is unsorted.Now re-do this exercise and name it ExInsertionSort....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT