Results 1 to 17 of 17
Thread: where the stuff goes?
- 08-29-2010, 04:35 PM #1
Member
- Join Date
- Aug 2010
- Location
- Sweden, Stockholm
- Posts
- 12
- Rep Power
- 0
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] } }
Bla Hund two times ?? why
Bla Hund
Igelkott
Rod Bjorn
Igelkott
Bla Bjorn
Vit Elefant
Vit ElefantLast edited by Eranga; 08-30-2010 at 12:37 PM. Reason: code tags added
- 08-29-2010, 06:14 PM #2public void skriv(int antal){// what is "antal" here for first time for Hocke(1)
super(tal, alla[tal]); // is it here goes 1 from Hocke(1)? to "tal " and to "all [tal]"
This code passes two arguments to the constructor of the super class.
super.skriv(3); //where 3 goes ?
two times ?? why
Try debugging the code by adding more println()s to show how variables are set and their values changed as the program executes.
- 08-30-2010, 05:30 AM #3
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,370
- Blog Entries
- 1
- Rep Power
- 25
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.
- 08-30-2010, 10:37 AM #4
Member
- Join Date
- Aug 2010
- Location
- Sweden, Stockholm
- Posts
- 12
- Rep Power
- 0
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);
- 08-30-2010, 12:38 PM #5
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,370
- Blog Entries
- 1
- Rep Power
- 25
Another thing that I want to highlight here. Please use code tags when you are posting again. Unformated codes are really hard to read.
- 08-30-2010, 12:41 PM #6
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,370
- Blog Entries
- 1
- Rep Power
- 25
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.
- 08-30-2010, 02:43 PM #7any other way to write debugging than by for example : System.out.println(x);
System.out.println("x=" + x);
- 08-30-2010, 03:33 PM #8
Member
- Join Date
- Aug 2010
- Location
- Sweden, Stockholm
- Posts
- 12
- Rep Power
- 0
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?
- 08-30-2010, 04:10 PM #9
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++)
- 08-30-2010, 06:08 PM #10
Member
- Join Date
- Aug 2010
- Location
- Sweden, Stockholm
- Posts
- 12
- Rep Power
- 0
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.
- 08-30-2010, 06:32 PM #11
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
- 08-30-2010, 08:11 PM #12
Member
- Join Date
- Aug 2010
- Location
- Sweden, Stockholm
- Posts
- 12
- Rep Power
- 0
ye , nummer is 2.
- 08-30-2010, 08:25 PM #13
Does that answer your question?
- 08-30-2010, 08:33 PM #14
Member
- Join Date
- Aug 2010
- Location
- Sweden, Stockholm
- Posts
- 12
- Rep Power
- 0
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...:)
- 08-30-2010, 08:43 PM #15
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.
- 08-31-2010, 10:00 AM #16
Member
- Join Date
- Aug 2010
- Location
- Sweden, Stockholm
- Posts
- 12
- Rep Power
- 0
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 ;)
- 08-31-2010, 03:00 PM #17
Similar Threads
-
JDK, SDK, JDT, JRE, what is all stuff? why do i need it?
By sonny in forum New To JavaReplies: 6Last Post: 05-26-2010, 01:17 AM -
Table, beginner stuff
By drwk in forum New To JavaReplies: 1Last Post: 01-24-2010, 08:19 PM -
Looking for help on drawing stuff in a jPanel
By Gatts79 in forum AWT / SwingReplies: 3Last Post: 08-28-2009, 07:00 PM -
Anyone selling SCJA training material? Your OLD stuff?
By KMN in forum Reviews / AdvertisingReplies: 2Last Post: 08-24-2009, 06:58 PM -
Simple Stuff 0.1
By Java Tip in forum Java SoftwareReplies: 0Last Post: 07-19-2008, 05:27 PM
Bookmarks