Results 1 to 2 of 2
Like Tree1Likes
  • 1 Post By jim829

Thread: Code continues on for a long time - Recursion Issues

  1. #1
    Captain Forge is offline Member
    Join Date
    Nov 2012
    Posts
    14
    Rep Power
    0

    Default Code continues on for a long time - Recursion Issues

    I used the following code in my class with the intention of doing one round of recursion (specifically creating an object within an object of the same type). Well, that one round of recursion is now like 200 rounds of recursion... So that messes a lot of stuff up. The following code is where I call the recursion:

    Java Code:
    //Find Solute
    		try{if(iterations == 0){ //RECONDITION::: iterations is equal to zero at start of program and is static!
    			remaining = Whitespace.removePreceding(remaining);
    			String unused = remaining.substring(0);
    			InterpretInput solute = new InterpretInput(remaining);
    			solute.begin();
    			solute.fixSoluteAmount();
    			soluteAmount = solute.getSolventAmount();
    			isSolution = true;
    			++iterations;
    		}}catch(Exception ex){
    		}
    		finally{
    			System.out.println("Debugging point D");
    			findNumber();
    			fixSolventAmount();
    			fixSoluteAmount();
    		}
    You'll find "Debugging point D" above, this is printed a ton of times so it apparently is going after the recursion with a bunch of objects, and the rest of the code is screwed up because of this. I just need someone experienced to point out how this is flawed as one iteration of recursion.

    If you need the entire class, I'll also copy / paste that bellow, but it's almost 200 lines... so yeah... (I know I shouldn't make classes that long but this object needed a lot of stuff in it).

    Java Code:
    import java.util.ArrayList;
    
    public class InterpretInput {
    
    	/**
    	 * @param remaining - The string that was input, what's left to analyze
    	 */
    	
    	/**  Variables  */
    	
    	private String remaining; //The string input by the user, containing what's left to analyze
    	private static int iterations = 0;
    	
    	//Solvent Info
    	private double solventAmount; //The amount of the solvent expressed as final in MOLES
    	private M solventAmountMeas; //The measurement used in solventAmount
    	private double solventConc; //The concentration of the solvent
    	private M solventConcMeas; //The measurement used in solventConc
    	private E[] solventCompound; //The compound of the solvent
    	private E[] water = {E.H, E.H, E.O};
    	
    	//Solute Info
    	private double soluteAmount; //The amount of solute in the solution
    	
    	//Type of Data
    	private boolean isElement = false; //Determines if the information input is only an element
    	private boolean hasAmount = false; //Determines if the information input has an amount of solvent
    	private boolean isSolution = false; //determines if the information input is a solution
    	
    	private int identificationNumber;
    	
    	/**  Constructor  */
    	
    	public InterpretInput (String remain){
    		remaining = remain;
    	}
    	
    	
    	/**  Mutator Methods  
    	 * @throws Exception */
    	
    	public void begin() throws Exception{
    		
    		//Find Measurement
    		FindMeasurements measureObject = new FindMeasurements(remaining);
    		while (measureObject.exists() == true){
    			measureObject.determineNumber();
    			measureObject.determineMeasurement();
    			double solventAmountTemp = measureObject.getAmount();
    			M solventAmountMeasTemp = measureObject.getMeasurement();
    			if( (solventAmountMeasTemp.getType()) == 3 ){
    				isSolution = true;
    				solventConc = solventAmountTemp;
    				solventConcMeas = solventAmountMeasTemp;
    			}else{		
    				hasAmount = true;
    				solventAmount = solventAmountTemp;
    				solventAmountMeas = solventAmountMeasTemp;
    			}
    			remaining = measureObject.getRemaining();
    	
    
    		}
    		
    		//Find Compound
    		FindCompound comp = new FindCompound(remaining);
    		comp.getCompound();
    		solventCompound = comp.getValue();
    		remaining = comp.getRemaining();
    		if (solventCompound.length == 1)
    			isElement = true;
    		
    		//Find Solute
    		try{if(iterations == 0){
    			remaining = Whitespace.removePreceding(remaining);
    			String unused = remaining.substring(0);
    			InterpretInput solute = new InterpretInput(remaining);
    			solute.begin();
    			solute.fixSoluteAmount();
    			soluteAmount = solute.getSolventAmount();
    			isSolution = true;
    			++iterations;
    		}}catch(Exception ex){
    		}
    		finally{
    			System.out.println("Debugging point D");
    			findNumber();
    			fixSolventAmount();
    			fixSoluteAmount();
    		}
    	}
    	
    	public void fixSoluteAmount() throws Exception {
    		fixSolventAmount();
    	}
    	
    	public void fixSolventAmount() throws Exception {
    		switch (identificationNumber){ //VIEW findNumber TO SEE INDEX OF THESE CASES
    		
    			case 1:{
    				//In this situation, there would be nothing to change to begin with
    				break;
    			}
    		
    			case 2:{
    				//In this situation, there would be nothing to change to begin with
    				break;
    			}
    		
    			case 3:{
    				solventAmount *= solventAmountMeas.ofBase();
    				switch (solventAmountMeas.getType()){
    					case 1:{ //volume
    						if (!solventCompound.equals(water))
    							throw new Exception();
    						else{
    							solventAmount *= 1000; //Convert 1000g for every 1L
    							double molarMass = 0;
    							for (E e : solventCompound)
    								molarMass += e.atomicMass();
    							solventAmount /= molarMass; //convert to moles
    						}
    					}
    					
    					case 2:{ //mass
    						double molarMass = 0;
    						for (E e : solventCompound)
    							molarMass += e.atomicMass();
    						solventAmount /= molarMass; //convert to moles
    					}
    				}
    			}
    			
    			case 4:{
    				if(solventAmountMeas.equals(M.m)){
    					throw new Exception(); //I AM TAKING OUT THIS FEATURE, IT WILL BE TOO DIFFICULT TO IMPLEMENT
    										   //BASICALLY, YOU CANNOT USE MOLALITY IN THIS PROGRAM ANYMORE
    				}
    			}
    			
    			case 5:{
    				if(solventAmountMeas.equals(M.m))
    					throw new Exception(); //I AM TAKING OUT THIS FEATURE, IT WILL BE TOO DIFFICULT TO IMPLEMENT
    										   //BASICALLY, YOU CANNOT USE MOLALITY IN THIS PROGRAM ANYMORE
    				double molarMass = 0;
    				for (E e : solventCompound)
    					molarMass += e.atomicMass();
    				solventAmount /= molarMass; //convert to moles
    			}
    		}
    	}
    	
    	public void findNumber(){
    		/**
    		 * 1 = Element
    		 * 2 = Compound
    		 * 3 = measured amount of compound
    		 * 4 = specific concentration of solution
    		 * 5 = Measured amount of specific concentration of solution
    		 * */
    		
    		if(isElement==true)
    			identificationNumber = 1;
    		else if(isSolution == false && hasAmount == false)
    			identificationNumber = 2;
    		else if(isSolution == false && hasAmount == true)
    			identificationNumber = 3;
    		else if(isSolution == true && hasAmount == false)
    			identificationNumber = 4;
    		else
    			identificationNumber = 5;
    	}
    	
    	
    	/**  Accessory Methods  */
    	
    	public double getSolventAmount(){
    		return solventAmount;
    	}
    	
    	public double getSoluteAmount(){
    		return soluteAmount;
    	}
    	
    	public double getConcentration(){
    		return solventConc;
    	}
    	
    	public E[] returnCompound(){
    		return solventCompound;
    	}
    
    }
    Last edited by Captain Forge; 03-30-2013 at 05:41 PM.

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,033
    Rep Power
    6

    Default Re: Code continues on for a long time - Recursion Issues

    It looks like you are incrementing your iteration counter after you call begin(). So iterations will always be zero at that point. A couple other observations.

    you can combine case 1 and case 2 together.
    Java Code:
    case 1:
    case 2:
       // do something
       break;
    Also, typically single letter capitals like E,T,K,V,S are used by convention for formal generic types. So you might want to use a different symbol for your arrays.

    Regarding recursion, it has been my experience that a recursive program should not rely on an external symbol to govern its completion. Usually there is something in the return value which when applied to the algorithm successively, results in a condition to cause the method to return. In your case, it appears you just want to use a loop (although I have not tried to follow your logic in detail).

    Regards,
    Jim
    Last edited by jim829; 03-30-2013 at 05:55 PM.
    Captain Forge likes this.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. Replies: 3
    Last Post: 05-08-2012, 06:26 PM
  2. Replies: 1
    Last Post: 03-10-2010, 02:53 PM
  3. Replies: 1
    Last Post: 12-05-2009, 08:28 AM
  4. Replies: 3
    Last Post: 01-23-2009, 09:53 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •