• Post Reply Bookmark Topic Watch Topic
  • New Topic

Stuck on creating a Name class  RSS feed

 
Daniel Hoang
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, im a new java programmer just starting his first semester. I am very lost when it comes to what to do in this project. I have to make a Name class that will allow the second class I posted below to execute properly. The book does not help me very much, so i came to this site in order to look for advice on how to get moving. For my Name class, i have the methods required, but i have no idea how to get line 10 of the main class to show my name. So far, i just use Eclipse's suggested corrections to see where i could work from there, but with those, i am only able to get "null null null", or blanks. Any tips on how to move forward with this project? Im also aware that my Name class probably has many mistakes. Thank you




 
drac yang
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i think the first thing you should do for Name class is to add the required constructors which the second class is using.
after that, try to find out other errors that might occur later.

 
Saif Asif
Ranch Hand
Posts: 440
Eclipse IDE Hibernate Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Morning Daniel ,
Well there are multiple alterations that can be made to your class. For starters , to print out the full name you have already defined a method in your Name class as below

Why are you not using this method ? Instead you have defined another method that prints out the name from the Name object ( am talking about the printNameOut( Name ) method here ) . Theres no point in defining this separate method , Use the method you defined in your Name class instead.

Also your toString() method can be used to achieve this . The toString() method is usually overridden to provide a String representation of your object in human readable format. In your case , the object is of type Name so why not use the toString() method directly as below



Also can you please specify at what line number are you getting the nulls ? As far as I have gone through the first few lines of your code, the method call at line 11 ( printNameOut ( myName ) ) should print out the name properly.
 
Campbell Ritchie
Marshal
Posts: 56518
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
drac yang wrote: . . .
No, no, no, no, no.

You need to realise that constructors exist for two purposes:-
  • 1: To initialise fields when the object is created.
  • 2: To prevent object creation.
  • I bet you were surprised by No 2. Yes, you want to ensure that your Name class is only instantiated with “real” values for all its fields. To do so, you make sure it has one constructor, which takes the three Strings. You get rid of all other constructors, particularly that no‑arguments constructor.
     
    Campbell Ritchie
    Marshal
    Posts: 56518
    172
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Saif Asif wrote: . . . at what line number are you getting the nulls ? . . .
    That is what happens if you don’t write the correct number of constructors. If you don’t write a constructor yourself, the compiler will add a default constructor, and all your fields will remain null.

    What does your changeNameTo method do? I do not think it does what you think it does.
    Your equals method is written incorrectly. It should take Object as a parameter. I suggest you leave out the equals method for the time being, because it is notoriously difficult to write. You also have a serious error in it, because you used the == operator.
     
    drac yang
    Ranch Hand
    Posts: 78
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Campbell Ritchie wrote:
    drac yang wrote: . . .
    No, no, no, no, no.

    You need to realise that constructors exist for two purposes:-
  • 1: To initialise fields when the object is created.
  • 2: To prevent object creation.
  • I bet you were surprised by No 2. Yes, you want to ensure that your Name class is only instantiated with “real” values for all its fields. To do so, you make sure it has one constructor, which takes the three Strings. You get rid of all other constructors, particularly that no‑arguments constructor.


    yes, if we want to ensure all the needed values be set when invoking the constructor, there's no need to explicitly make the default constructor.
    otherwise, we can use setters.
     
    Campbell Ritchie
    Marshal
    Posts: 56518
    172
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    That is not a default constructor.
     
    Daniel Hoang
    Ranch Hand
    Posts: 44
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Sorry, i havent responded in a while. So i got my first, middle, and last name to show up. Right now im trying to figure out how to capitalize my name for the clone. Ive been able to do it before, but its not working for me this time. What am i doing wrong with my code? My clones name is still lower cased.


     
    Tony Docherty
    Bartender
    Posts: 3271
    82
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I notice your Name class still has a no args constructor even though Campbell gave you very good advice on why you shouldn't have one.

    Right now im trying to figure out how to capitalize my name for the clone

    What code are you using to create a capitalized clone? and by capitalize do you mean all letters or just the first letter of each name.
     
    Daniel Hoang
    Ranch Hand
    Posts: 44
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Sorry, im not very good with the vocab yet. Im not sure exactly what he is asking for. Also i think im supposed to use
    to capitalilze the clones name for this part
     
    Tony Docherty
    Bartender
    Posts: 3271
    82
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Yes but how are you cloning the object?
    Are you using the clone() method or a constructor to create a new instance that's a clone of the object passed in or some other method.
     
    Daniel Hoang
    Ranch Hand
    Posts: 44
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Im using a constructor. Im supposed to input my name, then my clone has the same name as me, and then it wants me to put the clones name in all caps. Im not sure if im helping you at all.
     
    drac yang
    Ranch Hand
    Posts: 78
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Campbell Ritchie wrote:That is not a default constructor.

    hi Campbell,

    you mean only if there're no explicitly declared constructors in the class, can the implicit constructor be called default constructor?
    otherwise it can only be called no argument constructor, right?

    Thanks.
     
    Tony Docherty
    Bartender
    Posts: 3271
    82
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Im using a constructor. Im supposed to input my name, then my clone has the same name as me, and then it wants me to put the clones name in all caps. Im not sure if im helping you at all.

    When you clone an object you do it as a single step operation eg you call the clone() method and get a fully constructed copy of the object or you have a constructor that takes a type Name as a parameter and it constructs an object with the same values as the object you passed in etc. It's not considered cloning if you create a new object and then write code to extract each value from the original object and put it into the new object and it's certainly not a clone operation if you capitalize the names during the process. The cloning should be done by the object that is being cloned (or by it's class).

    I don't know what the actual question is so it's hard to suggest the correct approach. If I just wanted a capitalized version of the object then I'd add a toUpperCase() method that returned a copy of the object with capital letters but that might not satisfy your project's requirements. Can you post the actual question or the bit of it that deals with this issue.
     
    Campbell Ritchie
    Marshal
    Posts: 56518
    172
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    drac yang wrote: . . . you mean only if there're no explicitly declared constructors in the class, can the implicit constructor be called default constructor?
    otherwise it can only be called no argument constructor, right?

    Thanks.
    Correct
     
    Campbell Ritchie
    Marshal
    Posts: 56518
    172
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Why are you trying to clone the Name object in the first place? I would prefer TD’s suggestion of using a copy constructor.
     
    Daniel Hoang
    Ranch Hand
    Posts: 44
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Sorry if i wasnt being clear before. I did not mention that i MUST use all of the methods in the Name class. So i have to clone my name by calling to a method and then differentiate myself with the clone by calling to the toUpperCase method. I might be able to figure this out myself, ill post back.

    Here is my slightly fixed Name class
     
    Daniel Hoang
    Ranch Hand
    Posts: 44
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    So heres the method im trying to use to capitalize my name but it just isnt capitalizing and i havent figured out why yet.


    Edit: Nvm it is working but for this part of the test, cloneName.ToUpperCase() is capitalized, but when it asks for if(myName.equals(cloneName)), cloneName itself isnt capitalized. How can i make cloneName capitalized?
     
    Tony Docherty
    Bartender
    Posts: 3271
    82
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Look at what the ToUpperCase() method is doing (btw it should be toUpperCase()). It is getting the full name (which is a concatenation of each part of the name), converting it to uppercase and then returning the converted string. It doesn't change any of the values in the object.

    BTW I'm still not sure about how you 'clone' your original object as you have no methods/constructor to do so (but you do still have that no-args constructor which you should remove).
     
    Daniel Hoang
    Ranch Hand
    Posts: 44
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Then how do you suggest i clone myself? Ive been given specific directions that i dont want to stay too far from. I also have no idea what a no-args constructor is, so i cant remove it.
     
    Campbell Ritchie
    Marshal
    Posts: 56518
    172
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    A no‑arguments constructor is easy to recognise. It has no return value, exactly the same name as the class and empty () following. In this case it looks like public Name(){…}
     
    Daniel Hoang
    Ranch Hand
    Posts: 44
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Ah that, ok sorry. If i get rid of the no-args constructor, i can an error in the test here. Thats why i felt like i needed the no-args in the first place.
     
    Campbell Ritchie
    Marshal
    Posts: 56518
    172
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Have you been told to use the clone() method?
    What do the add method and the two changeNameTo methods do? What are they supposed to do?
    The equals method has been incorrectly implemented. Have you been told to implement an equals() method? If not, don’t because it is notoriously difficult to write.
     
    Daniel Hoang
    Ranch Hand
    Posts: 44
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Nope, i was told to clone myself by calling the .toString here

    The add method is something i dont quite understand completely yet, its used here,

    The changeNameTo(String String String) method is to combine my brides name with my name here,

    The second changeNameTo(String) is to change my old name with a completely new name here,

    And yes, i have been told to use the equals method to test if my clones name is still the same as mine after being capitalized, if it IS equal, then ive done the capitalization wrong, here


    Sorry, im very new to java, so i apologize if i cant explain things as well as everyone likes.
     
    Tony Docherty
    Bartender
    Posts: 3271
    82
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Nope, i was told to clone myself by calling the .toString here

    That is not a way to clone an object, it just gets a string representation of the object.

    Sorry, im very new to java, so i apologize if i cant explain things as well as everyone likes.

    Don't worry we understand that but (as I requested earlier) please post the question. Without seeing what problem you are trying to solve it's impossible to give you accurate advice.
     
    Daniel Hoang
    Ranch Hand
    Posts: 44
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Here is the question: Create a Name class that encapsulates a person’s name. It will have three private instance variables: firstName, middleName, and lastName. The class will have a read method, getFirstName(), getMiddleName(), getLastName(), setFirstName(String), setMiddleName(String), setLastName(String), changeNameTo(String, String, string), changeNameTo(Name), Name add(name), getFullName(), ToUpperCase(), toString() and equals(Name). The add method returns a Name instance. The returned instance first name is the parameter’s first name, the returned instance middle name is the parameter’s middle name, and the returned instance last name is this.lastName hyphenated with the parameter’s last Name. Use the test I provided to test your class.

    This is why i didnt want to use the clone() method, im not sure if im allowed to.
     
    Campbell Ritchie
    Marshal
    Posts: 56518
    172
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    That add method is inappropriately named. Is it supposed to take a woman’s name and return what it would be if she married the person in the Name object? It does not sound difficult to implement, from the description given.

    The instruction to implement an equals(Name) method is inconsistent with standard Java practice, and you should query it.
     
    Tony Docherty
    Bartender
    Posts: 3271
    82
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Where in that question does it say you need to clone the object?
    Why are there no return types given for the methods you have been told to implement?
     
    Daniel Hoang
    Ranch Hand
    Posts: 44
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    i think the add method should do what you said, Campbell, and then i print out the name using newWife.getFullName(). I think the main issue is that some of my returns are incorrect, possibly why my clone name wont capitalize. What do you mean by the equals method being inconsistent?


    Tony Docherty wrote:Where in that question does it say you need to clone the object?
    Why are there no return types given for the methods you have been told to implement?

    The cloning is in the test which i posted, here it is again. There are no return types because I have to figure them out myself, which is what im having trouble with.


     
    Daniel Hoang
    Ranch Hand
    Posts: 44
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Heres what im working with now, im using the print to see if the method is calling correctly, which it is. But cloneName itself isnt capitalized which is where im stumped.


    EDIT: I finally figured it out i think! This is what i ended up doing to get the cloneName to capitalize. Now onto the brides name
     
    Tony Docherty
    Bartender
    Posts: 3271
    82
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    The cloning is in the test which i posted, here it is again.

    Can you post the whole of the test code you have been given, I want to see how the test code creates a clone because the methods you have been told to implement do not include any sensible way of cloning/copying an object.

    Heres what im working with now, im using the print to see if the method is calling correctly, which it is. But cloneName itself isnt capitalized which is where im stumped.

    I suspect your toUpperCase() method should change each of the individual values in the object to uppercase and then return the concatenated values.
     
    Daniel Hoang
    Ranch Hand
    Posts: 44
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Tony Docherty wrote:
    The cloning is in the test which i posted, here it is again.

    Can you post the whole of the test code you have been given, I want to see how the test code creates a clone because the methods you have been told to implement do not include any sensible way of cloning/copying an object.

    Heres what im working with now, im using the print to see if the method is calling correctly, which it is. But cloneName itself isnt capitalized which is where im stumped.

    I suspect your toUpperCase() method should change each of the individual values in the object to uppercase and then return the concatenated values.


    Ive actually figured out the cloning process in the test, and yes you were right about what i had to do! The full test is in my first post if you would like to see it. Im now trying to figure out how to print myName as: First Middle BridesLast-Last.
    EX: myName is D D H, bridesName is 1 2 3. After being married myName will be D D 3-H, and bridesName will be 1 2 3-H
     
    Daniel Hoang
    Ranch Hand
    Posts: 44
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I have figured everything out. Thanks everyone for the tips and help!
     
    Tony Docherty
    Bartender
    Posts: 3271
    82
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I have figured everything out. Thanks everyone for the tips and help!

    Congratulations and it was our pleasure.
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!