• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how to get it work, sample prog

 
g Peshone
Ranch Hand
Posts: 44
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sample program, how would I get it to work?
Thanks.



 
Ravikanth kolli
Ranch Hand
Posts: 179
Chrome Eclipse IDE Mac
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can you actually mention the error message you get or the areas that you are stuck at?
 
g Peshone
Ranch Hand
Posts: 44
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It gets overflow because of the �this� in the second constructor. It loops between the first and second constructor until it bails out with overflow.
It does not want to advance to from the first:

to the next!?
Thanks.
 
Ravikanth kolli
Ranch Hand
Posts: 179
Chrome Eclipse IDE Mac
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
this one doesnot work because,
The 2 argument constructor is calling the no arg constructor which is inturn creating a new instance of the class with the argument constructor.

starting from the main:
SolBody earth = new SolBody("Earth ", null);

creates an instance of SolBody with the 2 argument constructor.
This constructor calls "this" which inturn creates 4 instances of the same class using the 2 argument constructor.

This process continues and thus gives a stackoverflowexception.
 
g Peshone
Ranch Hand
Posts: 44
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Lay back and enjoy it! That�s a good comment!
Anyway, how do you loop thru the 4 instances to get the right one? Or do I have it messed up all and not doing the wrong implementation? I just follow what the freaking book says.
Thanks.
 
Campbell Ritchie
Sheriff
Pie
Posts: 47229
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by g Peshone:
I just follow what the freaking book says.


Careful, we have sensitive ears

You need to move the object creation out of the constructor, otherwise that guarantees some sort of out of memory or overflow error. You often find there is a go() method or something like that which collects all the objects together and does something with them, and you have somehow got that code into a constructor.
 
g Peshone
Ranch Hand
Posts: 44
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Ritchie, I�ll investigate and see but I think that is what it said to do.
 
g Peshone
Ranch Hand
Posts: 44
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK, where would I then initialize these variables if not within constructor?
 
Campbell Ritchie
Sheriff
Pie
Posts: 47229
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by g Peshone:
OK, where would I then initialize these variables if not within constructor?
But you will have to design your classes properly, losing the public fields, and initialising them in the constructor.
 
g Peshone
Ranch Hand
Posts: 44
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Last night I initialized the instances within the Main and I got the right output where the Earth orbits sun and Moon orbits Earth. But it was not right as you point out of how things should be initialized.
I created this but I do not get the right results � it is not correct! Where do I need to do the fixing?

Vars:




in MAIN:


That�s the output!?

Saturn 9 (null)
Sun null
Earth 27 (null)
Moon 36 (null)
 
Campbell Ritchie
Sheriff
Pie
Posts: 47229
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sounds like a beginner's question.

You need a class with the instance fields idNum, name and orbits.
You set all three up in the constructor; if you have the static field nextID you can say

idNum = nextId++;

in the constructor rather than passing it as a parameter. Note the way I have written it, the first idNum is liable to be 0.

Your main method, or go or whatever, needs some way of getting the name for the planets (etc) and what they orbit. You can read them from a file, or from the keyboard, or hard-code them. Hard-coding is probably easiest at this stage.To get anything sensible from the System.out.println() call you have to override the toString() method.

You need to keep things simple; I have this suspicious streak which looks at people's code and says, "That looks complicated. You can simplify it." Your working solution will be much simpler, not needing things like loadSolar.
 
g Peshone
Ranch Hand
Posts: 44
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So, what happened to this line?

How does it go with the last "Go" implementation?
Thanks
 
g Peshone
Ranch Hand
Posts: 44
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The �Go� implementation is then inside the �bodySun{}� class and not inside the SolBody{} class. And that�s because it gets initialized every time its called from the main() by the new instances, that�s just plain wrong! And so the fields cannot be private!

 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic