3 Replies - 2005 Views - Last Post: 20 March 2010 - 02:31 PM Rate Topic: -----

#1 zanyrogue   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 91
  • Joined: 16-January 09

Question on Illegal Argument Exception Code

Posted 20 March 2010 - 09:41 AM

Hi all,

I'm helping my friend with his code and he has a separate class setup for Illegal Arguments.
Am I right in thinking that these lines of code makes sure that when the tree is empty and illegal exception is not thrown for the root, left, right.


public T root() {
		throw new IllegalArgumentException("Can't do root of empty bst <T>");
	}

	public AbstractBST<T> left() {
		throw new IllegalArgumentException("Can't do left of empty bst <T>");
	}

	public AbstractBST<T> right() {
		throw new IllegalArgumentException("Can't do right of empty bst <T>");
	}

	public boolean isEmpty() {
		return true;


This post has been edited by zanyrogue: 20 March 2010 - 09:42 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Question on Illegal Argument Exception Code

#2 japanir   User is offline

  • jaVanir
  • member icon

Reputation: 1014
  • View blog
  • Posts: 3,025
  • Joined: 20-August 09

Re: Question on Illegal Argument Exception Code

Posted 20 March 2010 - 11:59 AM

I am a bit confused by the question.
the methods you posted do nothing except for throwing an exception.
you will have to add the condition to verify if the tree is empty.

the isEmpty method alwats returns true so how do you determine if a tree is empty?
Was This Post Helpful? 0
  • +
  • -

#3 zanyrogue   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 91
  • Joined: 16-January 09

Re: Question on Illegal Argument Exception Code

Posted 20 March 2010 - 01:50 PM

View Postjapanir, on 20 March 2010 - 10:59 AM, said:

I am a bit confused by the question.
the methods you posted do nothing except for throwing an exception.
you will have to add the condition to verify if the tree is empty.

the isEmpty method alwats returns true so how do you determine if a tree is empty?


Sorry that was confusing. I explained it the wrong way around.

I think what it is doing is throwing an exception so that the tree cannot be started at 0 as nothing will be smaller than the root.
So the game it's controlling has to start at a random position higher that 0 so that the moves can be stored in a node within a binary search tree.

Here is the full code
public class BSTE<T> extends AbstractBST<T> {
	private static final long serialVersionUID = -9146887206621487004L;

	public T root() {
		throw new IllegalArgumentException("Can't do root of empty bst <T>");
	}

	public AbstractBST<T> left() {
		throw new IllegalArgumentException("Can't do left of empty bst <T>");
	}

	public AbstractBST<T> right() {
		throw new IllegalArgumentException("Can't do right of empty bst <T>");
	}

	public boolean isEmpty() {
		return true;
	}

	boolean equals(AbstractBST<T> t) {
		return t.isEmpty();
	}

	public int depth() {
		return 0;
	}

	public int numnodes() {
		return 0;
	}

	public AbstractBST<T> insert(T o) {
		return new BST<T>(o, new BSTE<T>(), new BSTE<T>());
	}

	public AbstractBST<T> max(AbstractBST<T> t) {
		throw new IllegalArgumentException("Can't do max of empty bst <T>");
	}

	public T midRoot() {
		throw new IllegalArgumentException("Can't do midroot of empty bst <T>");
	}


This post has been edited by zanyrogue: 20 March 2010 - 01:51 PM

Was This Post Helpful? 0
  • +
  • -

#4 zim1985   User is offline

  • Grand Inquisitor
  • member icon

Reputation: 75
  • View blog
  • Posts: 568
  • Joined: 19-February 10

Re: Question on Illegal Argument Exception Code

Posted 20 March 2010 - 02:31 PM

View Postzanyrogue, on 20 March 2010 - 11:50 AM, said:

I think what it is doing is throwing an exception so that the tree cannot be started at 0 as nothing will be smaller than the root.
So the game it's controlling has to start at a random position higher that 0 so that the moves can be stored in a node within a binary search tree.

Why not just catch an exception in the method instead? Since I don't know exactly what is going on, here is some pseudo-code
try
    if size equals 0
catch
    print error message


This is, of course, assuming that you implement a way to test if the length or whatever is 0 at the start.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1