Results 1 to 16 of 16
  1. #1
    Join Date
    Mar 2009
    Posts
    62
    Rep Power
    0

    Default Creating a constructor with arrays and arguments - Part 2

    Hey Guys / Gals,

    I thought I would start a new thread, as I have another problem with my code and I was hoping you could help.

    My original code:

    Java Code:
    /**
    * myString class
    */
    public class myString
    {
    /* instance variables */
    private char[] test;
    
    
    /**
     * Constructor
     */
       public myString(String args)
       {
          test = new char[args.length()];
          for (int i = 0; i < args.length(); i++)
          {   
              char temp = args.charAt(i);
              test[i] = temp;
              System.out.print(test[i] + ", ");
          }
       }
        
    
    }
    This works perfectly. I now need to write another constructor for myString class which has a single argument that is an array that has a type of char.

    I am told not to assign this argument to test, but instead the code should be similar to the first constructor shown above.

    So I have tried this code - I think the problem is that I am not really "thinking" about what I am doing, and just simply trying subsituting arguments etc.

    Java Code:
    public myString(char[] args)
    {
       test = new char [args.length];
       for (int i = 0; i < args.length; i++)
       {
          char[] temp = args;
          test[i] = temp;
       }
    }
    When I compile I am now getting incompatiable types - found char[] but expected char with this line: char[] temp = args;

    I am also slightly confused about what they are asking me to do, if you would like to see the whole thread for my earlier request please click here:

    Creating a constructor with arrays and arguments

    Anything I can do to help, please shout. I would like to thank you for your help in advance.

    All the best - Java Noobie FMJ.
    Last edited by fullmetaljacket; 07-03-2009 at 08:54 PM.

  2. #2
    Join Date
    Mar 2009
    Posts
    62
    Rep Power
    0

    Default

    I think the problem is this. I do not really understand the question.

    I now need to write another constructor for myString class which has a single argument that is an array that has a type of char.

    This I understand.

    I am told not to assign this argument to test, but instead the code should be similar to the first constructor shown above

    Its this bit that I find hard to understand??

    I recognise my code attempt is total giberish, but if someone could help please shout!

    All the best - FMJ.

  3. #3
    Join Date
    Mar 2009
    Posts
    62
    Rep Power
    0

    Default

    Just some further information - i'm trying to grasp this!

    [1]I now need to write another constructor for myString class which has a single argument that is an array that has a type of char.

    [2] I am told not to assign this argument to test, but instead the code should be similar to the first constructor shown above

    so with part 2 I guess they are asking me to copy the letters one at a time from the char array into test.

  4. #4
    Join Date
    Mar 2009
    Posts
    62
    Rep Power
    0

    Default

    sorry - my working constructor code in my first post was incorrect. I have replaced it.

    Cheers - FMJ.

  5. #5
    angryboy's Avatar
    angryboy is offline Senior Member
    Join Date
    Jan 2009
    Posts
    742
    Rep Power
    6

    Default

    I think the problem is this. I do not really understand the question
    no... the problem is you don't really understand arrays in java. Maybe a little review over the chapters will help.

    Actually your old code would work here if you were to reuse it, change string to char.
    Here is the old code:
    Java Code:
    public myString(String[] args)
    {
       this.test = args;
    }
    USE CODE TAGS--> [CODE]...[/CODE]
    Get NotePad++ (free)

  6. #6
    Join Date
    Mar 2009
    Posts
    62
    Rep Power
    0

    Default

    Hey Angryboy,

    I agree - I am having a hard time with arrays.

    So using your suggestion it should be:

    Java Code:
    public myString(char[] args)
    {
       this.test = args;
    }
    The only problem is that I am assigning the argument to test and I have been told not to do this, but rather "the code should be similar to the code that you wrote in the earlier constructor".

    I think they are asking me to copy the letters one at a time from the char array (argument) into the test.

    ??

    Does this mean that I need to create an inital array from the argument and if so - I do not know the length - I would then need to copy each letter into the test array.

    ??

    Cheers - FMJ.

  7. #7
    Join Date
    Mar 2009
    Posts
    62
    Rep Power
    0

    Default

    sorry - the exact line is : you should not simply assign the argument to test, rather the code for this constructor should be similar to the earlier constructor.

  8. #8
    angryboy's Avatar
    angryboy is offline Senior Member
    Join Date
    Jan 2009
    Posts
    742
    Rep Power
    6

    Default

    then you would need to use a loop. the pseudo codes are:
    Java Code:
    constructor(type arg){
      let SIZE = arg.length
      this.test = new char[] of size SIZE
    
      LOOP{
        this.test[ index ] = arg[ index ]
        index++
      }
    }
    USE CODE TAGS--> [CODE]...[/CODE]
    Get NotePad++ (free)

  9. #9
    Join Date
    Mar 2009
    Posts
    62
    Rep Power
    0

    Default

    Hey angryboy,

    So I have been working on this using the pseudocode you kindly provided.

    So far I have this, based on what you have suggested. I have tried to add comments to show you how I am working through this problem:

    Java Code:
    public myString(char[] args)
    {
       int i = args.length; /* this creates an int variable that is the same size as args*/
       this.test = new char[i] /* this creates a char array of the same size as args*/
    Then I am lost? I should create a while loop? According to my coursebooks index is an interger that is used to access the elements of an indexable collection. But if I type in say this.test[index] I get an error message no such variable index?

    Can anyone help any further?

    Cheers - FMJ.

  10. #10
    angryboy's Avatar
    angryboy is offline Senior Member
    Join Date
    Jan 2009
    Posts
    742
    Rep Power
    6

    Default

    haha ok I didn't think i needed to be so explicit on this. my fault.

    Java Code:
      Let index = 0; // 0 is the beginning, the alpha
      LOOP{
        this.test[ index ] = arg[ index ]
        index++
        IF index >= SIZE  // SIZE is the end, the omega
        THEN break out of the loop.
        ELSE continue on with this fruity loop...
      }
    Last edited by angryboy; 07-03-2009 at 11:29 PM.
    USE CODE TAGS--> [CODE]...[/CODE]
    Get NotePad++ (free)

  11. #11
    Join Date
    Mar 2009
    Posts
    62
    Rep Power
    0

    Default

    I looked up fruity loop in my coursebooks and there is no entry.


    kidding! Thanks mate - I will look into this now.

    All the best - FMJ.

  12. #12
    Join Date
    Mar 2009
    Posts
    62
    Rep Power
    0

    Default

    Now I really am lost. More lost than the people on the hit TV show "Lost", I think I should give up programming and do a degree in Electric Guitar studies.

    Angryboy without slapping me, can you expand on this further. Does index reference the amount of elements in the array? for example 0, 1, 2, 3 ?

    int i is set to the length of the argument, test creates an array of the same size (i think) I just need to copy each character one by one into the new array. I know the code to copy the characters.

    Any help before I shoot myself (its ok its only a spud gun) would be greatly appreciated.

    All the best - FMJ.

  13. #13
    Join Date
    Mar 2009
    Posts
    62
    Rep Power
    0

    Default

    I meant I don't know the code to copy the characters. a small slip but an important one.

  14. #14
    angryboy's Avatar
    angryboy is offline Senior Member
    Join Date
    Jan 2009
    Posts
    742
    Rep Power
    6

    Default

    I think I will shoot myself before you shoot yourself...

    Again, re-read the chapters on arrays and stop trying to finish this for completeness sakes. You clearly don't understand how arrays and loops work in java.

    Arrays (The Java™ Tutorials > Learning the Java Language > Language Basics)

    Control Flow Statements (The Java™ Tutorials > Learning the Java Language > Language Basics)
    USE CODE TAGS--> [CODE]...[/CODE]
    Get NotePad++ (free)

  15. #15
    Join Date
    Mar 2009
    Posts
    62
    Rep Power
    0

    Default

    I agree. I'm going to re-read the array stuff.

    Thanks again for all your help.

    Cheers - FMJ.

  16. #16
    corlettk is offline Member
    Join Date
    Apr 2009
    Location
    Brisbane
    Posts
    86
    Rep Power
    0

    Default

    FMJ,

    Don't have a cow if you don't "just pick-up this stuff" automagically. It takes most folks "a bit of time" to just absorb the basic concept of an array... I think of an array as series of book-sized cubby-holes along a book-shelf. You can put one book (any book) in any cubby-hole. You can swap books. A cubby-hole can be empty. A nice neat bookshelf is filled from the left. Books could be arranged in alphabetical order, or they could be all over the shop. Your bookshelf can be half empty, but it can never hold any more books than it can hold. This analogy just "works for me".

    Hewever... *Do* start to worry if you don't understand arrays after a couple of days of honest effort... Some folks could beat-there-heads against the wall continiously for weeks and still not understand the nuances of this stuff... and them folks won't ever be programmers, no matter how hard they try... in exactly the same way that 99.99% of the population could never be professional soccer players. That doesn't make them lesser people, it just makes them "not developers".

    You've displayed three attributes which stand will you in good stead: patience, perseverance, and a sense of humor.

    Keep trying, just don't bust a foo-foo valve over it.

    Java Code:
    package forums;
    
    class MyString
    {
      private char[] chars;
    
      public MyString(String string) {
        this(string.toCharArray());
      }
    
      public MyString(char[] chars) {
        this.chars = new char[chars.length];
        for (int i=0; i<chars.length; i++) {
          this.chars[i] = chars[i];
        }
      }
    
      public String toString() {
        return new String(this.chars);
      }
    
      public static void main(String[] args) {
        try {
          MyString s = new MyString("Hello World!");
          System.out.println(s);
        } catch (Exception e) {
          e.printStackTrace();
        }
      }
    
    }
    Cheers. Keith.

Similar Threads

  1. Creating a constructor with arrays and arguments
    By fullmetaljacket in forum New To Java
    Replies: 38
    Last Post: 07-03-2009, 06:09 AM
  2. [SOLVED] Creating an Array of Arrays?
    By xcallmejudasx in forum Advanced Java
    Replies: 5
    Last Post: 11-04-2008, 06:01 PM
  3. Replies: 0
    Last Post: 12-19-2007, 09:10 AM
  4. Replies: 0
    Last Post: 12-19-2007, 09:01 AM
  5. Variable No. of Arguments
    By Gajesh Tripathi in forum New To Java
    Replies: 2
    Last Post: 10-31-2007, 02:50 PM

Posting Permissions

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