4 Replies - 670 Views - Last Post: 30 November 2009 - 02:14 PM Rate Topic: -----

#1 dustinm  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 30-November 09

Recursive method not giving me expected results

Posted 30 November 2009 - 01:29 PM

I'm working on an assignment in which I have to write a class with one instance variable, and int. The constructor should take an int as its only parameter. I had to write a recursive method that checks if that int is a multiple of 5, and also have to write a client class to test the class.

I have everything written, but my recursive method will not give me the correct result when the given number is _not_ a multiple of 5. I've tried restructuring the if/else statement, and also using a flag variable, with no luck. I'm assuming it's probably something little that I'm missing, but I've sat here looking at it for a while now and still can't figure it out.

Here is the class, MultipleOfFive.java:
/**
 * @(#)multipleOfFive.java
 *
 *
 * @author Dustin 
 * @version 1.00 2009/11/30
 */




public class MultipleOfFive 
{
	private int num;
	
	/**	overloaded constructor
	 *	@param	num	number to be tested
	 */
	public MultipleOfFive( int number ) 
	{
		num = number;		
	}
	
	/** minusFive method
	 *	@param	size	the number to be tested to see if it is a multiple of five
	 *	@return	string indicating if the number is a multiple of five
	 */
	public String minusFive( int size)
	{		
		//boolean flag = false;
		
		if( (size == 0) || (size >= 5) )
		{
			System.out.println( "Checking the number " + size + "\n" );
			minusFive( size - 5 ); 
			//flag = true;
			return num + " is a multiple of five";
		} 
		else
		{
			//flag = false;
			System.out.println( "Checking the number " + size + "\n" );
			return num + " is not a multiple of five";					
		}
		
		//return num + " is a multiple of five";
			
		//if ( flag == false )
		//{
		//	return num + " is not a multiple of five";
		//}
		//else 
		//{
		//	return num + " is a multiple of five";  
		//}	
	}
	
	/**	accessor method
	 *	@return number to be tested
	 */
	public int getNumber( )
	{
		return num;
	}
	
	/** mutator method
	 *	@param	newNumber	new number to be tested
	 */
	public void setNumber( int newNumber )
	{
		num = newNumber;
	}
	
	/**	toString method
	 *	@return	prints the number to be tested
	 */
	public String toString( )
	{	
		return "";	
	}
	
	/** equals method
	 * @param o1 another ArraySum object
	 * @return a boolean, true if this object
	 * 		has the same field values as the parameter o1
	 */
	 public boolean equals( Object o1 )
	 {
	 	if ( ! ( o1 instanceof MultipleOfFive ) )
	 		return false;
	 	else
	 	{
	 		MultipleOfFive objMultFive = ( MultipleOfFive ) o1;
	 		return ( num == objMultFive.num ); 
	 	}	 	
	 }	 
}

The problem, I think, is in the recursive method, specifically lines 32-44. there's some stuff commented out, ignore it, it was other things I had tried.

Here's the client, in case it's needed:
/**
 * @(#)MultipleOfFiveClient.java
 *
 *
 * @author Dustin 
 * @version 1.00 2009/11/30
 */
import java.util.Scanner;

public class MultipleOfFiveClient {

	public static void main ( String [] args )
	{
		Scanner scan = new Scanner( System.in );
		
		System.out.println( "This program will use recursion to test a number to see"
									+ "\nif it is a multiple of five by repeatedly subtracting five."
									+ "\n\nIf the process does not eventually end at zero, then the "
									+ "\nnumber is not a multiple of five."
									+ "\n\nEnter a number larger than 4 to test >" );
		int testNumber = scan.nextInt( );
		
		MultipleOfFive m1 = new MultipleOfFive( testNumber );
		//MultipleOfFive.minusFive( MultipleOfFive.getNumber( ) );		
		
		System.out.println( "Results of test:\n" + m1.minusFive( m1.getNumber( ) ) );
	}
	
	
}


No matter what number is given, it always returns that the number _is_ a multiple of 5.

Thanks in advance for any advice on it.

Is This A Good Question/Topic? 0
  • +

Replies To: Recursive method not giving me expected results

#2 japanir  Icon User is offline

  • jaVanir
  • member icon

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

Re: Recursive method not giving me expected results

Posted 30 November 2009 - 01:40 PM

try this method:
public static String minusFive(int number){
		if(number == 0){
			return "Yes";
		}
		else if(number < 0){
			return "No";
		}
		else{
			return minusFive(number - 5);
		}
	}



however it wont handle the case of numbers smaller than 0, as if number = -10 or number = -5
i suggest than you use the Math.abs() method before using the method.
Was This Post Helpful? 1
  • +
  • -

#3 dustinm  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 30-November 09

Re: Recursive method not giving me expected results

Posted 30 November 2009 - 01:52 PM

View Postjapanir, on 30 Nov, 2009 - 12:40 PM, said:

try this method:
public static String minusFive(int number){
		if(number == 0){
			return "Yes";
		}
		else if(number < 0){
			return "No";
		}
		else{
			return minusFive(number - 5);
		}
	}



however it wont handle the case of numbers smaller than 0, as if number = -10 or number = -5
i suggest than you use the Math.abs() method before using the method.

Thanks! That way is much cleaner looking, as opposed to how I had it before. I got it working correctly using Math.abs() also. I'm also curious, though, any idea what it was specifically that was causing my way to not work?
Was This Post Helpful? 0
  • +
  • -

#4 japanir  Icon User is offline

  • jaVanir
  • member icon

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

Re: Recursive method not giving me expected results

Posted 30 November 2009 - 02:07 PM

the problem inside this condition.
if( (size == 0) || (size >= 5) )
here you call your method only once as
minusFive( size - 5 );
but you should return the method in order for it to be recursive.
by only calling it, it executes what it should once, and then continue doing other stuff.

This post has been edited by japanir: 30 November 2009 - 02:08 PM

Was This Post Helpful? 0
  • +
  • -

#5 dustinm  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 30-November 09

Re: Recursive method not giving me expected results

Posted 30 November 2009 - 02:14 PM

View Postjapanir, on 30 Nov, 2009 - 01:07 PM, said:

the problem inside this condition.
if( (size == 0) || (size >= 5) )
here you call your method only once as
minusFive( size - 5 );
but you should return the method in order for it to be recursive.
by only calling it, it executes what it should once, and then continue doing other stuff.

Ok, I got it. I was thinking that since it was printing each number out as it subtracted that it was working correctly and never paid attention it needed to return the method to work.

Thanks again.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1