Results 1 to 12 of 12

Thread: Representing Names (Need Help)

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Sep 2006
    Posts
    19

    Representing Names (Need Help)

    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.

  2. #2
    PowerPoster kfcSmitty's Avatar
    Join Date
    May 2005
    Posts
    2,248

    Re: Representing Names (Need Help)

    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

  3. #3
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594

    Re: Representing Names (Need Help)

    (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.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  4. #4
    PowerPoster eranga262154's Avatar
    Join Date
    Jun 2006
    Posts
    2,201

    Wink Re: Representing Names (Need Help)

    Quote Originally Posted by CornedBee
    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.
    “victory breeds hatred, the defeated live in pain; happily the peaceful live giving up victory and defeat” - Gautama Buddha

  5. #5
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594

    Re: Representing Names (Need Help)

    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.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  6. #6
    PowerPoster eranga262154's Avatar
    Join Date
    Jun 2006
    Posts
    2,201

    Re: Representing Names (Need Help)

    Thanks for your explanation.
    “victory breeds hatred, the defeated live in pain; happily the peaceful live giving up victory and defeat” - Gautama Buddha

  7. #7
    Lively Member
    Join Date
    Dec 2005
    Posts
    68

    Re: Representing Names (Need Help)

    Quote Originally Posted by CornedBee
    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.

    Code:
    	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.
    Last edited by lunchboxtheman; Nov 9th, 2006 at 12:37 PM.

  8. #8
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594

    Re: Representing Names (Need Help)

    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.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  9. #9
    Lively Member
    Join Date
    Dec 2005
    Posts
    68

    Re: Representing Names (Need Help)

    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.
    Last edited by lunchboxtheman; Nov 9th, 2006 at 01:54 PM.

  10. #10
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594

    Re: Representing Names (Need Help)

    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.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  11. #11
    Lively Member
    Join Date
    Dec 2005
    Posts
    68

    Re: Representing Names (Need Help)

    EDIT: Sorry, mis-post :-(

  12. #12
    Lively Member
    Join Date
    Dec 2005
    Posts
    68

    Re: Representing Names (Need Help)

    EDIT: Sorry, mis-post. :-(
    and it appears I double posted. My apologies.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width