Results 1 to 6 of 6

Thread: Constructor

  1. #1
    theoneroo is offline Member
    Join Date
    Jan 2011
    Posts
    3
    Rep Power
    0

    Default Constructor

    Trying to create a constructor that creates a character array with the contents of java.lang.String. And if the string is larger then INITITIAL_CAPACITY, the array should be doubled greater then the length of the string.

    I got this, does it look right? And then I want to know how to test it?

    public Magic(java.lang.String tr)
    {
    int newCap;
    this.data = new char[INITIAL_CAPACITY];
    if (tr.length()>INITIAL_CAPACITY)
    do
    newCap = INITIAL_CAPACITY * 2;
    while(tr.length()>INITIAL_CAPACITY);
    }

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default

    Where do you create an array with the new capacity.
    Where do you convert the String to a char array?

    You test it by writing an appropriate main method.

  3. #3
    theoneroo is offline Member
    Join Date
    Jan 2011
    Posts
    3
    Rep Power
    0

    Default

    Alright done that, what if the parameters where (char[] tr), would it be similar to this one?

  4. #4
    m00nchile is offline Senior Member
    Join Date
    Feb 2010
    Location
    Ljubljana, Slovenia
    Posts
    470
    Rep Power
    5

    Default

    Java Code:
    if (tr.length()>INITIAL_CAPACITY)
    do
    newCap = INITIAL_CAPACITY * 2;
    while(tr.length()>INITIAL_CAPACITY);
    this here is a big no no. Go through each line separately, and say out loud what they do. You'll see there's something very wrong with this statement.
    Ever seen a dog chase its tail? Now that's an infinite loop.

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,820
    Rep Power
    19

    Default

    this.data = new char[INITIAL_CAPACITY];

    There's no need to create this until you've decide what size to make the array.

  6. #6
    nerobi15 is offline Member
    Join Date
    Jan 2011
    Posts
    1
    Rep Power
    0

    Default fds

    i have problem through this question:
    use 2 integer variables represent the private data of class.Provide a constructor function that enables and object of this class to be initialized when it is declared.The constructor shoul contain default value in case no initializers are provided.

Similar Threads

  1. Constructor
    By arefeh in forum New To Java
    Replies: 9
    Last Post: 01-11-2010, 11:38 AM
  2. Constructor
    By Sarinam in forum AWT / Swing
    Replies: 1
    Last Post: 06-19-2008, 08:03 AM
  3. Replies: 0
    Last Post: 12-19-2007, 09:10 AM
  4. Replies: 0
    Last Post: 12-19-2007, 09:01 AM
  5. Constructor Help
    By bluegreen7hi in forum New To Java
    Replies: 2
    Last Post: 11-15-2007, 05:44 AM

Posting Permissions

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