File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes how to get it work, sample prog Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "how to get it work, sample prog" Watch "how to get it work, sample prog" New topic
Author

how to get it work, sample prog

g Peshone
Ranch Hand

Joined: Oct 20, 2008
Posts: 44
Sample program, how would I get it to work?
Thanks.



Ravikanth kolli
Ranch Hand

Joined: Feb 10, 2008
Posts: 179

can you actually mention the error message you get or the areas that you are stuck at?


-kolli
g Peshone
Ranch Hand

Joined: Oct 20, 2008
Posts: 44
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

Joined: Feb 10, 2008
Posts: 179

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

Joined: Oct 20, 2008
Posts: 44
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

Joined: Oct 13, 2005
Posts: 40052
    
  28
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

Joined: Oct 20, 2008
Posts: 44
Thanks Ritchie, I�ll investigate and see but I think that is what it said to do.
g Peshone
Ranch Hand

Joined: Oct 20, 2008
Posts: 44
OK, where would I then initialize these variables if not within constructor?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
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

Joined: Oct 20, 2008
Posts: 44
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

Joined: Oct 13, 2005
Posts: 40052
    
  28
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

Joined: Oct 20, 2008
Posts: 44
So, what happened to this line?

How does it go with the last "Go" implementation?
Thanks
g Peshone
Ranch Hand

Joined: Oct 20, 2008
Posts: 44
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!

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how to get it work, sample prog