Tmcclain
Nov 8th, 2006, 04:11 PM
I try to do as much as I can.
1. Write a class Name that stores a person's first, middle, and last names and provides the following methods:
· public Name(String first, String middle, String last)—constructor. The name should be stored in the case given; don't convert to all upper or lower case.
· public String getFirst()—returns the first name
· public String getMiddle()—returns the middle name
· public String getLast()—returns the last name
· public String firstMiddleLast()—returns a string containing the person's full name in order, e.g., "Mary Jane Smith".
· public String lastFirstMiddle()—returns a string containing the person's full name with the last name first followed by a comma, e.g., "Smith, Mary Jane".
· public boolean equals(Name otherName)—returns true if this name is the same as otherName. Comparisons should not be case sensitive. (Hint: There is a String method equalsIgnoreCase that is just like the String method equals except it does not consider case in doing its comparison.)
· public String initials()—returns the person's initials (a 3-character string). The initials should be all in upper case, regardless of what case the name was entered in. (Hint: Instead of using charAt, use the substring method of String to get a string containing only the first letter—then you can upcase this one-letter string. See Figure 3.1 in the text for a description of the substring method.)
· public int length()—returns the total number of characters in the full name, not including spaces.
2. Now write a program TestNames.java that prompts for and reads in two names from the user (you'll need first, middle, and last for each), creates a Name object for each, and uses the methods of the Name class to do the following:
a. For each name, print
· first-middle-last version
· last-first-middle version
· initials
· length
b. Tell whether or not the names are the same.
Here is my name.java and testname.java program
// ****************************************************************
// Names.java
// Tanesha McClain
// A name that stores a person's first, middle, and last names
//
// ****************************************************************
public class Name{
String first, middle, last, fullName, fullName2;
//constructor
public Name(String firstName, String middleName, String lastName, String firstMiddleLast, String lastFirstMiddle) {
first = firstName;
middle = middleName;
last = lastName;
fullName = firstMiddleLast;
fullName2 = lastFirstMiddle;
}
public String getFirstName() {
return first;
}
public String getMiddleName() {
return middle;
}
public String getLastName() {
return last;
}
public String getfirstMiddleLast(){
return fullName;
}
public String getlastFirstMiddle(){
return fullName2;
}
public String toString() {
return first + ' ' + middle + ' ' + last + "\n";
}
} // end class
// ****************************************************************
// TestNames.java
//
// Tests out the Names class
// ****************************************************************
public class TestNames
{
public static void main(String[] args)
{
Name name1 = new Name("Tanesha");
Name name2 = new Name("Nicole");
Name name3 = new Name("McClain");
Name name4 = new Name("Tanesha Nicole McClain");
Name name5 = new Name("McClain, Tanesha Nicole");
System.out.println(name1.first());
System.out.println(name2.middle());
System.out.println(name3.last());
System.out.println("Initials: " + name1.initials());
System.out.println("Length: " + name1.length());
System.out.println();
System.out.println(name2.firstMiddleLast());
System.out.println(name2.lastFirstMiddle());
System.out.println("Initials: " + name2.initials());
System.out.println("Length: " + name2.length());
System.out.println();
if (name1.equals(name2))
{
System.out.println("They are the same name.");
}
else
{
System.out.println("They are not the same name.");
}
}
}
1. Write a class Name that stores a person's first, middle, and last names and provides the following methods:
· public Name(String first, String middle, String last)—constructor. The name should be stored in the case given; don't convert to all upper or lower case.
· public String getFirst()—returns the first name
· public String getMiddle()—returns the middle name
· public String getLast()—returns the last name
· public String firstMiddleLast()—returns a string containing the person's full name in order, e.g., "Mary Jane Smith".
· public String lastFirstMiddle()—returns a string containing the person's full name with the last name first followed by a comma, e.g., "Smith, Mary Jane".
· public boolean equals(Name otherName)—returns true if this name is the same as otherName. Comparisons should not be case sensitive. (Hint: There is a String method equalsIgnoreCase that is just like the String method equals except it does not consider case in doing its comparison.)
· public String initials()—returns the person's initials (a 3-character string). The initials should be all in upper case, regardless of what case the name was entered in. (Hint: Instead of using charAt, use the substring method of String to get a string containing only the first letter—then you can upcase this one-letter string. See Figure 3.1 in the text for a description of the substring method.)
· public int length()—returns the total number of characters in the full name, not including spaces.
2. Now write a program TestNames.java that prompts for and reads in two names from the user (you'll need first, middle, and last for each), creates a Name object for each, and uses the methods of the Name class to do the following:
a. For each name, print
· first-middle-last version
· last-first-middle version
· initials
· length
b. Tell whether or not the names are the same.
Here is my name.java and testname.java program
// ****************************************************************
// Names.java
// Tanesha McClain
// A name that stores a person's first, middle, and last names
//
// ****************************************************************
public class Name{
String first, middle, last, fullName, fullName2;
//constructor
public Name(String firstName, String middleName, String lastName, String firstMiddleLast, String lastFirstMiddle) {
first = firstName;
middle = middleName;
last = lastName;
fullName = firstMiddleLast;
fullName2 = lastFirstMiddle;
}
public String getFirstName() {
return first;
}
public String getMiddleName() {
return middle;
}
public String getLastName() {
return last;
}
public String getfirstMiddleLast(){
return fullName;
}
public String getlastFirstMiddle(){
return fullName2;
}
public String toString() {
return first + ' ' + middle + ' ' + last + "\n";
}
} // end class
// ****************************************************************
// TestNames.java
//
// Tests out the Names class
// ****************************************************************
public class TestNames
{
public static void main(String[] args)
{
Name name1 = new Name("Tanesha");
Name name2 = new Name("Nicole");
Name name3 = new Name("McClain");
Name name4 = new Name("Tanesha Nicole McClain");
Name name5 = new Name("McClain, Tanesha Nicole");
System.out.println(name1.first());
System.out.println(name2.middle());
System.out.println(name3.last());
System.out.println("Initials: " + name1.initials());
System.out.println("Length: " + name1.length());
System.out.println();
System.out.println(name2.firstMiddleLast());
System.out.println(name2.lastFirstMiddle());
System.out.println("Initials: " + name2.initials());
System.out.println("Length: " + name2.length());
System.out.println();
if (name1.equals(name2))
{
System.out.println("They are the same name.");
}
else
{
System.out.println("They are not the same name.");
}
}
}