Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Methodology to find bugs

  1. #1
    Member
    Join Date
    Sep 2011
    Posts
    63
    My Mood
    Confused
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Methodology to find bugs

    Ive writen the following class. The point of the code can be found here Problem 12 - Project Euler.

    The code doesn't work!!! But before anybody points out why, I wanted to ask about technics and methods that people use to try to find bugs. I have learnt about 'asserts' but I can't quite get my head around how I could use them in this case.

    Currently the code just outputs the answer 1... no matter what I enter as the args!!!

    public class TriangleFinder implements Runnable {
    	public long target;		// number to divisors
    	public long outputValue;
    	public boolean finished = false;
    	private Thread runner;
     
    	TriangleFinder(long inTarget) {
    		target = inTarget;
    		if (runner == null) {
    			runner = new Thread(this);
    			runner.start();
    		}
    	}
     
    	public void run() {
    		long numOfDivisors = 0;
    		for (long i = 1; i < target; i++) {
    			outputValue = getDivisors(getTriangle(i));
    		}
    		finished = true;
    	}
     
    	public long getTriangle(long checkNum) {
    		long triangleValue = 0;
    		for (long i = checkNum; i > 0; i--) {
    			triangleValue += i;
    		}
    		return triangleValue;
    	}
     
    	public long getDivisors(long checkDiv) {
    		int divisorCount = 0;
    		for (long i = 1; i <= checkDiv; i++) {
    			if (i % checkDiv == 0) {
    				divisorCount++;
    			}
    		}
    		return divisorCount;
    	}
    }

    Thanks
    Last edited by tarkal; October 9th, 2011 at 03:13 PM.


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Methodology to find bugs

    I wanted to ask about technics and methods that people use to try to find bugs.
    Debugging with the debugger supplied via jdb or any number of IDE's available. You can step through the code to evaluate variable values. In addition, using assertions or writing Test cases help you break problems down and make sure pieces of code work as they are expected to work. Throwing runtime exceptions for things such as illegal parameters is also valuable.

    Currently the code just outputs the answer 1... no matter what I enter as the args!!!
    Since there is no main method, hence no args, it is difficult to advise you. Please provide an SSCCE that demonstrates the problem.

  3. #3
    Member
    Join Date
    Sep 2011
    Posts
    63
    My Mood
    Confused
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Re: Methodology to find bugs

    Hi sorry for the late reply. here is the class with the main method that instantiates the above class.

    public class TriangleThreads {
    	public static void main(String[] args) {
    		TriangleThreads tt = new TriangleThreads(args);
    	}
     
    	public TriangleThreads(String[] args) {
    		TriangleFinder[] finder = new TriangleFinder[args.length];
     
    		for (int i = 0; i < args.length; i++ ) {
    			try {
    				long count = Long.parseLong(args[i]);
    				finder[i] = new TriangleFinder(count);
    				System.out.println("Looking...");
    			} catch (NumberFormatException nfe) {
    				System.out.println("Error " + nfe.getMessage());
    			}
    		}
     
    		boolean complete = false;
    		while (!complete) {
    			complete = true;
    			for (int j = 0; j < finder.length; j++) {
    				if (finder[j] == null) continue;
    				if (!finder[j].finished) {
    					complete = false;
    				} else {
    					displayResults(finder[j]);
    					finder[j] = null;
    				}
    			}
     
    			try {
    				Thread.sleep(1000);
    			} catch (InterruptedException ie) {
    				// do nothing
    			}
    		}
    	}
     
    	private void displayResults(TriangleFinder finder) {
    		System.out.println("The first triangle number to have " + finder.target + " divisors is " + finder.outputValue );
    	}
    }
    Last edited by tarkal; October 10th, 2011 at 02:20 PM. Reason: Entered wrong code

  4. #4
    Member
    Join Date
    Sep 2011
    Posts
    63
    My Mood
    Confused
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Re: Methodology to find bugs

    I'm now 90% sure that the problem is coming from the run()...

  5. #5
    Member
    Join Date
    Sep 2011
    Posts
    63
    My Mood
    Confused
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Re: Methodology to find bugs

    aha... I have found that a good way of isolating a problem is to ad in tempory variables and print them out to see what is happening. Is this generaly an accepted technique?

  6. #6
    Member
    Join Date
    Sep 2011
    Posts
    63
    My Mood
    Confused
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Re: Methodology to find bugs

    I've managed to solve the problem and fix the code (below) but for numbers larger than 400 it becomes very very slow. Any suggestions on how this could be enhanced for larger numbers?
    public class TriangleFinder implements Runnable {
    	public long target;		// number to divisors
    	public long outputValue;
    	public boolean finished = false;
    	private Thread runner;
     
    	TriangleFinder(long inTarget) {
    		target = inTarget;
    		if (runner == null) {
    			runner = new Thread(this);
    			runner.start();
    		}
    	}
     
    	public void run() {
    			for (long i = 0; getDivisors(getTriangle(i)) < target; i++) {
    				outputValue = getTriangle(i + 1);
    			}
    		finished = true;
    	}
     
    	public long getTriangle(long checkNum) {
    		long triangleValue = 0;
    		for (long i = checkNum; i > 0; i--) {
    			triangleValue += i;
    		}
    		return triangleValue;
    	}
     
    	public long getDivisors(long checkTri) {
    		long divisorCount = 0;
    		for (long i = 1; i <= checkTri; i++) {
    			if (checkTri % i == 0) {
    				divisorCount++;
    			}
    		}
    		return divisorCount;
    	}
    }
    Last edited by tarkal; October 10th, 2011 at 04:07 PM.

Similar Threads

  1. [SOLVED] Cannot Find Constructor
    By jrookie in forum What's Wrong With My Code?
    Replies: 4
    Last Post: December 8th, 2010, 02:54 PM
  2. find LCS
    By Anemone_ in forum Algorithms & Recursion
    Replies: 1
    Last Post: November 28th, 2010, 02:03 PM
  3. Code bugs
    By heptix in forum What's Wrong With My Code?
    Replies: 4
    Last Post: September 30th, 2010, 03:32 AM
  4. Replies: 1
    Last Post: May 8th, 2009, 08:55 AM