Login x
User Name:
Password:
Social Links Facebook Twitter YouTube Steam RSS News Feeds

Members Online

»
0 Active | 8 Guests
Online:

LATEST FORUM THREADS

»
warfare
CoD4 Map + Mod Releases
Voting menu on maps
CoD+UO General
Hauling 911
CoDBO3 General

Forums

»

Welcome to the MODSonline.com forums. Looking for Frequently Asked Questions? Check out our FAQs section or search it out using the SEARCH link below. If you are new here, you may want to check out our rules and this great user's guide to the forums and the website.
For more mapping and modding information, see our Wiki: MODSonWiki.com

Jump To:
Forum: All Forums : Soldier of Fortune
Category: SoFII General
General game questions, comments and chat.
Moderators: foyleman, Foxhound, Mystic, StrYdeR, batistablr, Welshy, DrBiggzz, supersword
Latest Posts
Subscribed Posts
Search
Restricted Access Topic is Locked
Page
Next Page
subscribe
Author Topic: Ok question about java...
MoNoXBooGie~rb
General Member
Since: Dec 23, 2006
Posts: 3176
Last: Dec 23, 2006
[view latest posts]
Level 9
Category: SoFII General
Posted: Monday, Aug. 11, 2003 09:39 pm
I have this thing and I would like people who know a lot about java to try and see who's right.


Ok here's the deal....


I had a test today with the course 'datastructures & algebra' uses java as the main language. This one problem was like this:

You have to write a class in java that should build a binary tree, with each node having 2 children with height n by only using the constructor and no other methods. The tree should have a String variable saying "left" for the left child en "right" for the right node. Also it should have a reference to it's parent. So calling Tree(4) should construct a decission tree with height 4.

Well the only logic way I can think of (the only way they taught us to construct a tree) is using Recursion (letting a method call itself again until the tree is completely constructed).

One of my friends who did the same exam said he asked the teacher and that also the teacher agreed that recursion can't be used with only a constructor, but adding a private method would help a lot.

I didn't use a private method, and I just made a little test program in 2 minutes to see if recursion works.... this is what it looks like:


public class TestRec{

            public int x;
            public TestRec kind;

            public TestRec(int i){
                        if (i != 0){
                                     kind = new TestRec(i - 1);
                                     x = i;
                                     System.out.println(i + "");
                        }
            }
}

public class Test{
            public static void main(String[] args){
                        TestRec test = new TestRec(6);
            }
}

and it produces a column with numbers from 1 to 6..... this is recursion, right? With only a constructor?  :s9:
Share |
MoNoXBooGie~rb
General Member
Since: Dec 23, 2006
Posts: 3176
Last: Dec 23, 2006
[view latest posts]
Level 9
Category: SoFII General
Posted: Tuesday, Aug. 12, 2003 09:05 pm
I guess everyone here is java noobs? :P
Share |
^MOB^ tkekd538~rb
General Member
Since: Dec 23, 2006
Posts: 184
Last: Dec 23, 2006
[view latest posts]
Level 4
Category: SoFII General
Posted: Tuesday, Aug. 12, 2003 11:37 pm
not a Java n00b at all, but trying to understand what your output is going to look like.  Do you want something like this:

  L----X----R
         |
  L----X----R
         |
  L----X----R
         |
  L----X----R

X being your nodes, L and R being Left and Right.  

K, I deleted a lot of rambling now lol...  I ran your code, and yes it prints out what you need, and it has an archaic way to progress down the tree.  I know there is a reason why i dont like the recursive call to its own constructor, I just dont remember what the pitfall is right now...  Knew I shoulda paid more attention and the JavaOne conference a few months ago lol...  #### them for having Unreal Tournament lan'd there...
Share |
MoNoXBooGie~rb
General Member
Since: Dec 23, 2006
Posts: 3176
Last: Dec 23, 2006
[view latest posts]
Level 9
Category: SoFII General
Posted: Wednesday, Aug. 13, 2003 02:37 am
The actual problem I had at the exam isn't really something that has to be explained here. It's just the idea of recursion in the constructor, whether it works or not.

Anyway the idea of the tree was kinda like this:

                             Root
                            /      \
                          L         R
                        /   \     /   \
                      L      R  L     R

...etc.

That friend who's being stubborn said there was a problem with using the constructor like that.... I can't think of a problem that could occur though.
Share |
Fuz@2die4.com~rb
General Member
Since: Dec 23, 2006
Posts: 145
Last: Dec 23, 2006
[view latest posts]
Level 4
Category: SoFII General
Posted: Wednesday, Aug. 13, 2003 12:01 pm
Your example doesn't really make the tree...

public class Tree {
  Tree parent, left, right;
  String where;
  public Tree(Tree parent, int level, String where) {
     this.parent = parent;
     this.where = where;
     if(level-- == 0) return;
     left = new Tree(this, level, "L");
     right = new Tree(this, level,"R");
  }
}

public static void main(String args[]) {
Tree tree = new Tree(null, 4, "Root");
}

Now u need a public method to print the tree...

As for recursive constructors... They are bad if u fail to terminate the recursion :)  

There is also a problem related to Polymorphism, how to pass on the class type you are trying to make a tree of...
Share |
sp00nraker~rb
General Member
Since: Dec 23, 2006
Posts: 67
Last: Dec 23, 2006
[view latest posts]
Level 3
Category: SoFII General
Posted: Wednesday, Aug. 13, 2003 06:01 pm
I don't know my head from my ass in java but I do know what recursive means, It's a series of numbers where you can not get the next number in the series without the one prior to it.

an = a(n-1) + 3    <--- recursive sequece...I don't know how to do subscripts though n and n-1 should be subscripts
Share |
^MOB^ tkekd538~rb
General Member
Since: Dec 23, 2006
Posts: 184
Last: Dec 23, 2006
[view latest posts]
Level 4
Category: SoFII General
Posted: Wednesday, Aug. 13, 2003 06:36 pm
kewl.  And here I was thinking out a dynamic solution when I woke up this AM where you would pass in the dimensions of the tree hehe  was gonna geek out on this at some point, but looks like you have what ya need...
Share |
MoNoXBooGie~rb
General Member
Since: Dec 23, 2006
Posts: 3176
Last: Dec 23, 2006
[view latest posts]
Level 9
Category: SoFII General
Posted: Wednesday, Aug. 13, 2003 10:32 pm
Fuz, I know it doesn't make a tree in my example. That just makes a linear line of objects. As I said it was to test whether recursion works in a constructor, not to test whether my tree works. Besides that for a tree it's not much different except that you need to make another child for each node. :)

sp00n you're right too. You just create a tree of which Tree(4) creates a tree of 4 levels, and then with recursion you call Tree(4 - 1) and the next one will call Three(4 - 1 - 1) etc which is correct cuz the left node of a tree is also a tree but with one layer less.

About recursive constructors being bad when not ending it, it's also bad when it doesnt end in a void. You'll run out of stack space either way. So that can't be the reason why it shouldn't work.


*edit*

I just thought about something.... maybe it's a bad idea to use recursive constructors because the object you're passing on hasn't been created yet?
Share |
Fuz@2die4.com~rb
General Member
Since: Dec 23, 2006
Posts: 145
Last: Dec 23, 2006
[view latest posts]
Level 4
Category: SoFII General
Posted: Thursday, Aug. 14, 2003 08:21 am
Quote
I just thought about something.... maybe it's a bad idea to use recursive constructors because the object you're passing on hasn't been created yet?


No.  When you are in the constructor, self exists. The object has been allocated and can be referenced.

The main problem about recursion in the Constructor, is the Polymorphism problem.  Lets say you make a descendant class that has additional attributes... you can't call the inherited constructor because that creates nodes of its class and not of your class - unless you can pass the class type as parameter.  So, it gets messy.  If you use a virtual function to instantiate each node, you get around that problem.
Share |
sp00nraker~rb
General Member
Since: Dec 23, 2006
Posts: 67
Last: Dec 23, 2006
[view latest posts]
Level 3
Category: SoFII General
Posted: Thursday, Aug. 14, 2003 08:24 am
Quote (MoNoXBooGie @ Aug. 13 2003, 5:32 pm)
sp00n you're right too

Wow, I honestly thought I'de never use anything I learned in Advanced Algebra class  :p
Share |
Restricted Access Topic is Locked
Page
Next Page
subscribe
MODSonline.com Forums : Soldier of Fortune : SoFII General

Latest Syndicated News

»
Codutility.com up and runn...
Nice, and there still using the logo and template for the screenshots, which...
Codutility.com up and runn...
dundy writes...Quote:Call of Duty modding and mapping is barly alive only a ...
Codutility.com up and runn...
Mystic writes...Quote:It seems to me the like the site is completely dead? ...
Codutility.com up and runn...
It seems to me the like the site is completely dead?

Partners & Friends

»