PDA

Click to See Complete Forum and Search --> : Representing Names (Need Help)


tmlucky14
Nov 7th, 2006, 01:20 PM
Can somebody help me with this program. Thanks

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.

kfcSmitty
Nov 7th, 2006, 01:29 PM
Sorry. We usually don't just hand out homework assignments.


Attempt to do it yourself, and if you have problems, post your code. We will attempt to help you out

CornedBee
Nov 7th, 2006, 04:47 PM
(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.)
This hint is stupid. Use Character.toUpperCase() to make a single character upper-case.

Of course, that's under the assumption that all names start with letters that can be upper-cased into a single target character. In international settings, that's not necessarily true.
On the other hand, I don't have a middle name, so there.

eranga262154
Nov 7th, 2006, 10:06 PM
This hint is stupid. Use Character.toUpperCase() to make a single character upper-case.

I'm confusing with this statement. Can you explain little bit more. Thanks.

CornedBee
Nov 8th, 2006, 04:08 AM
The hint tells the student to use substring() instead of charAt() to extract a string of length one and uppercase it with the String class's toUpperCase() method. I'm saying that this stupid: it needs to allocate two new string objects (the substring and the uppercased version of the substring). Instead, use charAt() to get a single character and use the static Character.toUpperCase() to make that character uppercase. Because char is a primitive, this is slightly faster.

eranga262154
Nov 8th, 2006, 08:03 AM
Thanks for your explanation.

lunchboxtheman
Nov 9th, 2006, 12:19 PM
The hint tells the student to use substring() instead of charAt() to extract a string of length one and uppercase it with the String class's toUpperCase() method. I'm saying that this stupid: it needs to allocate two new string objects (the substring and the uppercased version of the substring). Instead, use charAt() to get a single character and use the static Character.toUpperCase() to make that character uppercase. Because char is a primitive, this is slightly faster.

Doesn't need to make new string objects.

public String initials ()
{
return (firstName.toUpperCase ().substring (0,1) + middleName.toUpperCase () .substring (0,1) +
lastName.toUpperCase().substring (0,1));
}

You don't need to make any new explicit string objects, don't need any temp vars, or anything. You are correct about chars being faster though, and that's the way it should be. Just wanted to clear it up that you don't have to declare String objects like some people might have taken your post.

CornedBee
Nov 9th, 2006, 01:02 PM
You create no less than 7 String objects and one StringBuilder object (or 9 String objects an no StringBuilder objects, depending on the way the compiler works), including the String that is finally returned.

None of these may be explicit, but they're no less there.

Oh, and by the way, your order of calls is inefficient. First you upper case the complete strings, then you throw away everything but the first character of each. Do it the other way round: call substring first.

lunchboxtheman
Nov 9th, 2006, 01:41 PM
Yeah, you're right, I already said that. I just clarified the explicit variables arn't necessary. Your second post made it sound like they were, and because it seems the OP is utterly confused, I just didn't want to him to do that.

CornedBee
Nov 9th, 2006, 02:43 PM
The entire thread doesn't make a reference to explicit variables. I don't have any idea where you get the idea from.

Neither is the OP confused - or at least there's no way of knowing. He hasn't said so much as peep since his original post.

lunchboxtheman
Nov 9th, 2006, 06:22 PM
EDIT: Sorry, mis-post :-(

lunchboxtheman
Nov 9th, 2006, 06:23 PM
EDIT: Sorry, mis-post. :-(
and it appears I double posted. My apologies.