In: Computer Science
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
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"))); } }
===================================================================