Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: Constructors

  1. #1
    Member
    Join Date
    Jun 2011
    Posts
    43
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Constructors

    Hello

    I need to write a constructor for a class called myClass that takes 2 string arguments (1 called someWords and another called theList).

    The constructor needs to convert the string referenced by the argument someWords into a set of words and assign the set to an instance
    variable called setword and then assign the argument theList to an instance variable called lists.

    Any guidance here would be appreciated.


    best wishes


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Constructors

    Sounds pretty straightforward. What's your question? What have you tried? Where are you stuck?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Member
    Join Date
    Jun 2011
    Posts
    43
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Constructors

    Constructors are my weakness I am very very new to OOP and the contructors I have written in the past
    are very basic, I am not sure how to start with this one!



    rgds,

  4. #4
    Forum old-timer
    Join Date
    Nov 2008
    Location
    Faversham, Kent, UK
    Posts
    472
    My Mood
    Mellow
    Thanks
    4
    Thanked 58 Times in 54 Posts

    Default Re: Constructors

    Start with the name of the class, open parenthesis, declare the two parameters, close parenthesis, open curly brace, etc. If you've written constructors in the past, as you say, you'll find it's very basic too.

    If you get stuck, post the code you have and explain what you're having difficulty with. The more specific the question you ask, the more specific the answer you'll get.

  5. #5
    Member
    Join Date
    Jun 2011
    Posts
    43
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Constructors

    public myClass(String someWords, String theList )
    {

    super();


    }

    I'm sorry I'm not sure how to convert the string referenced by the argument someWords into a set of words and assign the set to an instance
    variable called setword and then assign the argument theList to an instance variable called lists.

    If there is anyway someone can show me code on how this constructor is done with a layman explanation, I will learn a great deal from it, thankyou very much.
    I'm finding the books quite tough to follow as a total novice.

  6. #6
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Constructors

    Quote Originally Posted by av8 View Post
    I'm sorry I'm not sure how to convert the string referenced by the argument someWords into a set of words
    What do you mean by "set"? Aer you supposed to use one of the classes in the Java Collections Framework or something else. For starters you can look at the String.split method.

    and assign the set to an instance variable
    You would assign it just like you assign anything else in Java.

    If there is anyway someone can show me code on how this constructor is done with a layman explanation
    No, it is your job to write the code not ours. Make an attempt and when you get stuck post your code and ask a specific question. Remember that all your code does not have to be crammed into a single method or constructor. Your constructor can call 1 or more methods to help do the work.
    Improving the world one idiot at a time!

  7. #7
    Forum old-timer
    Join Date
    Nov 2008
    Location
    Faversham, Kent, UK
    Posts
    472
    My Mood
    Mellow
    Thanks
    4
    Thanked 58 Times in 54 Posts

    Default Re: Constructors

    The Java Tutorial is a good first place to look for basic information. Try Providing Constructors and Passing Information To A Method or Constructor.

Similar Threads

  1. constructors in servlets
    By the light in forum JavaServer Pages: JSP & JSTL
    Replies: 4
    Last Post: June 29th, 2011, 10:32 AM
  2. constructors in servlets
    By the light in forum Java Servlet
    Replies: 3
    Last Post: June 27th, 2011, 04:13 AM
  3. [SOLVED] Overloading constructors(Multiple Constructors)
    By chronoz13 in forum Java Theory & Questions
    Replies: 2
    Last Post: May 11th, 2011, 12:55 PM
  4. Write methods and constructors from Javadocs
    By smashX in forum What's Wrong With My Code?
    Replies: 3
    Last Post: April 20th, 2011, 10:23 PM
  5. Array object and constructors
    By TarunN in forum Collections and Generics
    Replies: 14
    Last Post: May 6th, 2010, 04:04 PM