Results 1 to 17 of 17
  1. #1
    Izabela is offline Member
    Join Date
    Aug 2010
    Location
    Sweden, Stockholm
    Posts
    12
    Rep Power
    0

    Default where the stuff goes?

    Java Code:
    class Uppg2{
            public static void main(String[]args){
                   Fnyrr[] obj = new Fnyrr[4];
                   obj[0]=new Hocke(1);
                   obj[1]=new Smatte();
                   obj[2]=new Smatte(); 
                   obj[3]=new Hocke(3);
                   for (int x=0; x<obj.length; x++)
                      obj[x].skriv(4);
          }
    }
    Java Code:
    class Fnyrr{
           public static String[]färg={"Rod","Bla","Gul","Vit","Gron"};
           public static int nummer=0;
           public String minFärg="Svart", djur="Bjorn";
         
           public Fnyrr(){
                minFärg=färg[nummer++];
           }
    
           public Fnyrr(int tal, String djur){
                minFärg=färg[tal];
                this.djur=djur;
           }
    
           public void skriv(int antal){[B][I]// what is "antal" here for first time for Hocke(1)[/I][/B]
                for (int x=nummer; x<antal; x++)
                      System.out.println(minFärg+" "+djur);
           }
    }
    Java Code:
    class Hocke extends Fnyrr{
            public static String[] alla={"Katt","Hund","Gris","Elefant"};
    
            public Hocke(int tal){
                   super(tal, alla[tal]); //[I][B] is it here goes 1 from Hocke(1)? to "tal " and to "all [tal]"[/B][/I]
            }
    }
    Java Code:
    class Smatte extends Fnyrr{
            public String grej="Igelkott";
    
            public void skriv(int antal){
                   System.out.println(grej);
                   super.skriv(3);                  [B][I]//where 3 goes ?[/I][/B]
            }
    }
    [/I]Answer:[/I]
    Bla Hund two times ?? why
    Bla Hund
    Igelkott
    Rod Bjorn
    Igelkott
    Bla Bjorn
    Vit Elefant
    Vit Elefant
    Last edited by Eranga; 08-30-2010 at 12:37 PM. Reason: code tags added

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default

    public void skriv(int antal){// what is "antal" here for first time for Hocke(1)
    You need to read about how methods receive arguments. antal is an int argument to the skriv method.
    super(tal, alla[tal]); // is it here goes 1 from Hocke(1)? to "tal " and to "all [tal]"
    What is your question?
    This code passes two arguments to the constructor of the super class.

    super.skriv(3); //where 3 goes ?
    This code passes one arg to the constructor of the super class.

    two times ?? why
    Because of the logic of the program. That is what the program is coded to do.

    Try debugging the code by adding more println()s to show how variables are set and their values changed as the program executes.

  3. #3
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    Take a piece of paper and write down the on each line, just running the code on your mind. Or else as Norm suggest you, debug the code and check values in each steps.

  4. #4
    Izabela is offline Member
    Join Date
    Aug 2010
    Location
    Sweden, Stockholm
    Posts
    12
    Rep Power
    0

    Default

    sorry guys but to early for me to get it what do you talking about...anyways ..I did debugging, well at least I did try to do that.
    Still can not get it why program writes 2 times Bla Hund but not 4 times? 4 times because is 4 places in array so the "antal" is 4.
    confusing what goes first and where.
    i know the passing from constructor to super class but in what order in this case?
    is it any other way to write debugging than by for example : System.out.println(x);

  5. #5
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    Another thing that I want to highlight here. Please use code tags when you are posting again. Unformated codes are really hard to read.

  6. #6
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    Did you write this code in an IDE or on simple editor such as notepad and so on? If you are working on an IDE then add few breakpoints and debug the code, it's most suitable way.

    If not take a piece of paper and write down the output in each line, just by running your code from your mind, and check the logic.

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default

    any other way to write debugging than by for example : System.out.println(x);
    Yes. What you show is not a good way to do it. You need to add an id for the output:
    System.out.println("x=" + x);

  8. #8
    Izabela is offline Member
    Join Date
    Aug 2010
    Location
    Sweden, Stockholm
    Posts
    12
    Rep Power
    0

    Default

    hmm... weired but is not working even with your ,NORM,nicer version of code. I could not see x. :(

    Well....
    in this part at the very beginning
    public void skriv(int antal){
    for (int x=nummer; x<antal; x++)
    System.out.println(minFärg+" "+djur);
    }

    x is 0 taken from "nummer=0; " in super class, right?
    and
    "antal "is 4 as 4 places in array,
    so how come For-loop is stopped after two times? with result Bla Hund, Bla Hund?

  9. #9
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default

    Change this:
    public void skriv(int antal){
    for (int x=nummer; x<antal; x++)

    to this:

    public void skriv(int antal){
    System.out.println("antal=" + antal + ", nummer=" + nummer); // show values
    for (int x=nummer; x<antal; x++)

  10. #10
    Izabela is offline Member
    Join Date
    Aug 2010
    Location
    Sweden, Stockholm
    Posts
    12
    Rep Power
    0

    Default

    thanks, Norm, it's good to see this x=nummer; finaly....so how this x increases from 0 to 2?
    I do not see any nummer++ ..... where this nummer=0; which I think is x in For-loop changes to 2? Where I understand x is starting with this value 2 and go through For-loop. Seems, all the times x is starting with value 2.

  11. #11
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default

    Did you add the following to your code:
    System.out.println("antal=" + antal + ", nummer=" + nummer); // show values
    What was the value of nummer that printed?

    all the times x is starting with value 2
    x starts with the value in nummer? Which you printed out on the println() above.

  12. #12
    Izabela is offline Member
    Join Date
    Aug 2010
    Location
    Sweden, Stockholm
    Posts
    12
    Rep Power
    0

    Default

    ye , nummer is 2.

  13. #13
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default

    Does that answer your question?

  14. #14
    Izabela is offline Member
    Join Date
    Aug 2010
    Location
    Sweden, Stockholm
    Posts
    12
    Rep Power
    0

    Default

    Partly, yes but I want to know how did happen that x is 2.... where is it in this code ? what I am missing?
    this is so confusing...where this 2 came from?

    p.s.

    Thanks for all you answers...:)

  15. #15
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default

    This will change the value of nummer:
    minFärg=färg[nummer++];

    This will set x to the value of nummer:
    x=nummer

    Add some more println()s to show the value of nummer.

  16. #16
    Izabela is offline Member
    Join Date
    Aug 2010
    Location
    Sweden, Stockholm
    Posts
    12
    Rep Power
    0

    Default

    hi...well is it not that "nummer " has value 0 at the beginnig and let say ok...changes by "minFärg=färg[nummer++]; " ,but is it not that volue of "nummer" is 1? why is it 2 than? How it happens that" nummer "has value 2 when become x, in For-loop?
    How to think here???

    Do I ever understand java logic?...Gosh..ufff ;)

  17. #17
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default

    All I can recommend is: Add some more println()s to show the value of nummer.

Similar Threads

  1. Replies: 6
    Last Post: 05-26-2010, 01:17 AM
  2. Table, beginner stuff
    By drwk in forum New To Java
    Replies: 1
    Last Post: 01-24-2010, 08:19 PM
  3. Looking for help on drawing stuff in a jPanel
    By Gatts79 in forum AWT / Swing
    Replies: 3
    Last Post: 08-28-2009, 07:00 PM
  4. Anyone selling SCJA training material? Your OLD stuff?
    By KMN in forum Reviews / Advertising
    Replies: 2
    Last Post: 08-24-2009, 06:58 PM
  5. Simple Stuff 0.1
    By Java Tip in forum Java Software
    Replies: 0
    Last Post: 07-19-2008, 05:27 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
  •