Results 1 to 9 of 9
  1. #1
    McChill is offline Member
    Join Date
    Feb 2009
    Posts
    47
    Rep Power
    0

    Default [SOLVED] Loop(?) problem

    The program is not displaying the response I am looking for. The terms are getting added into the Arraylists, and are being differentiated then added into the other ArrayList properly. However, whenever I run the program it only displays the last term. For example, if I put in "3 x 3 + 2 x 2", it will display--

    The derivative of 3 x 3 + 2 x 2 is

    The term 3.0x3.0was added to terms.
    The term 9.0x ^2.0was added to derivative.
    The term 2.0x2.0was added to terms.
    The term 4.0x ^1.0was added to derivative.
    4.0x ^1.0 +

    I want it to display this --

    The derivative of 3 x 3 + 2 x 2 is

    The term 3.0x3.0was added to terms.
    The term 9.0x ^2.0was added to derivative.
    The term 2.0x2.0was added to terms.
    The term 4.0x ^1.0was added to derivative.
    9.0x ^2.0 + 4.0x ^1.0 +

    Java Code:
    import java.util.Scanner;
    import java.util.ArrayList;
    import java.util.regex.Pattern;
    public class Derivative {
    	
    	private double power;
    	private double coefficient;
    	private String variable;
    	public Scanner scanner;
    	public ArrayList<String> terms;
    	public ArrayList<String> derivative;
    	static Derivative d = new Derivative();
    
    	
    	public static void main(String args[]) {
    			System.out.println("The derivative of " + args[0] + " is\n");
    			System.out.println(d.getDerivative(args[0]));
    	}
    
    	public void setPower(double power) {
    		this.power = power;
    	}
    
    	public double getPower() {
    		return power;
    	}
    
    	public void setCoefficient(double coefficient) {
    			this.coefficient = coefficient;
    	}
    
    	public double getCoefficient() {
    		return coefficient;
    	}
    
    	public void setVariable(String variable) {
    		this.variable = variable;
    	}
    
    	public String getVariable() {
    		return variable;
    	}
    	
    	
    	
    
    	public String getDerivative(String function)
    	{
    		analyze(function);	//analyze the function given
    		String answer = "";
    		for(String each: derivative) {
    			answer = answer + each;			//put each term in derivative into the answer String
    			answer = answer + " + ";		//add the + signs...adds an extra one right now until I find a better way.
    		}
    		return answer;
       }
    	
    	public void analyze(String function) {
    		scanner = new Scanner(function);	//pass the function to the scanner
    		scanner.useDelimiter("\\W+");
    		terms = new ArrayList<String>();
    		int x = 0;
    		while(scanner.hasNext()) {		//while loop through the function
    			if(scanner.hasNextDouble())	{
    				setCoefficient(scanner.nextDouble());		//set the coefficient as the first double
    			}
    			setVariable(scanner.next());			//set the variable as the next token after first double 
    			if(scanner.hasNextDouble())	{
    				setPower(scanner.nextDouble());	//set the exponent power as the next double	
    			}
    			else	setPower(1);			//if the next token wasn't a double, set the power to 1
    			terms.add(getCoefficient() + getVariable() + getPower());	//set the term into the arraylist	
    			System.out.println("The term " + getCoefficient() + getVariable() + getPower() + "was added to terms.");	//just for testing purposes
    			differentiate(terms.get(x));
    			x++;
    		}		
    		
    		
    		
    	}
    	public ArrayList<String> differentiate(String analyzedFunction) {
    		derivative = new ArrayList<String>();
    		double newCoefficient = getPower() * getCoefficient();	//exponent times coefficient
    		double newPower = getPower() - 1.0;					//exponent power minus 1
    		derivative.add(newCoefficient + getVariable() + " ^" + newPower);
    
    		System.out.println("The term " + newCoefficient + getVariable() + " ^" + newPower + "was added to derivative.");	//just for testing purposes
    		
    		return derivative;			
    		
    	}
    
    		
    }
    Any help is appreciated...been trying to get this to work for too long now..

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,569
    Rep Power
    12

    Default

    How big is the array list derivative when you call getDerivative()? How big did you expect it would be?

    Where - and how often - do you set the value (not the contents) of derivative?
    Last edited by pbrockway2; 05-06-2009 at 12:48 AM.

  3. #3
    Tenn is offline Member
    Join Date
    Nov 2008
    Posts
    30
    Rep Power
    0

    Default

    Looking at your code I'm not real sure its a testing class or a class of information sense you have main but you also have a bunch of methods that are set up to do things.

    Now the way i understand a test program inside main you have yoru veriables and the method calls. I know that Arrays only work with a designated size, sense they can't grow. So you to determine the size before you load/search make it do fancy things.

  4. #4
    McChill is offline Member
    Join Date
    Feb 2009
    Posts
    47
    Rep Power
    0

    Default

    Quote Originally Posted by pbrockway2 View Post
    How big is the array list derivative when you call getDerivative()? How big did you expect it would be?

    Where - and how often - do you set the value (not the contents) of derivative?

    Well when getDerivative() first gets called, derivative is null. But once analyze() gets called, and differentiate() gets called, it should add elements to the array list equal to how many terms the function has. Does using the add method count as setting the value or the contents? The add() method in differentiate() is the only time derivative is changed. Two elements are being added to derivative, but for some reason it's the same value. I changed a line in differentiate() to make sure it was the element was being added.
    Java Code:
    	public ArrayList<String> differentiate(String analyzedFunction) {
    		derivative = new ArrayList<String>();
    		double newCoefficient = getPower() * getCoefficient();	//exponent times coefficient
    		double newPower = getPower() - 1.0;					//exponent power minus 1
    		derivative.add(newCoefficient + getVariable() + " ^" + newPower);
    		System.out.println("The term " + derivative.get(0) +  "was added to derivative.");	//******changed calling variables into calling the array list element!!!!
    		x++;
    		return derivative;			
    		
    	}
    If I enter in "3 x 3 + 2 x 2" I get--
    The derivative of 3 x 3 + 2 x 2 is

    The term 3.0x3.0was added to terms.
    The term 9.0x ^2.0was added to derivative.
    The term 2.0x2.0was added to terms.
    The term 4.0x ^1.0was added to derivative.
    4.0x ^1.0 +

    So the values are being set but I don't see why the line that should be the whole derivative is just the last term.

  5. #5
    Tenn is offline Member
    Join Date
    Nov 2008
    Posts
    30
    Rep Power
    0

    Default

    Mmm that looks like it shoudl be used with vectors or linked list as opposed to arrays, because an array isn't dynamic...it'd have to know how many elements you want to put in it before hand. like if theres.... 4 elements in it before it can load.

  6. #6
    OrangeDog's Avatar
    OrangeDog is offline Senior Member
    Join Date
    Jan 2009
    Location
    Cambridge, UK
    Posts
    838
    Rep Power
    6

    Default

    ArrayLists are also dynamic
    Don't forget to mark threads as [SOLVED] and give reps to helpful posts.
    How To Ask Questions The Smart Way

  7. #7
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,569
    Rep Power
    12

    Default

    Well when getDerivative() first gets called, derivative is null. But once analyze() gets called, and differentiate() gets called, it should add elements to the array list equal to how many terms the function has
    OK, so in the example you gave you are expecting derivative to have length 2 because you entered two terms. That's half of the questions I asked in the first paragraph.

    The other half was supposed to push you see what the length of derivative really is.

    Java Code:
    public String getDerivative(String function)
    {
        analyze(function); //analyze the function given
        String answer = "";
            /*
             * This line tells you the *actual* size of derivative
             */
        System.out.println("size=" + derivative.size());
        for(String each: derivative) {
            answer = answer + each;		
            answer = answer + " + ";	
        }
        return answer;
    }
    When this code is run we see that something is rotten:

    Java Code:
    The derivative of 3 x 3 + 2 x 2 is
    
    The term 3.0x3.0was added to terms.
    The term 9.0x ^2.0was added to derivative.
    The term 2.0x2.0was added to terms.
    The term 4.0x ^1.0was added to derivative.
    size=1
    4.0x ^1.0 +
    To see why it is going wrong, return to the question I asked at the end. By the value of derivative, I meant lines that say "derivative=..." rather than lines that say "derivative.add(...)" which alter its contents. The reason why this is important is that since derivative has a strange size we are bound to wonder whether the terms are really being added to the same arraylist.

    (Since the purpose of derivative is to "accumulate" the result it is most important that it be assigned to exactly once.)

  8. #8
    McChill is offline Member
    Join Date
    Feb 2009
    Posts
    47
    Rep Power
    0

    Default

    Oh...okay. I see now I had a blatantly obvious mistake there in differentiate. It reset the value of derivative each time it was called. Thanks. How do I marked this as solved?

  9. #9
    Singing Boyo is offline Senior Member
    Join Date
    Mar 2009
    Posts
    552
    Rep Power
    6

    Default

    Use the Thread Tools menu at the top of the page.
    If the above doesn't make sense to you, ignore it, but remember it - might be useful!
    And if you just randomly taught yourself to program, well... you're just like me!

Similar Threads

  1. if else loop problem
    By Ms.Ranjan in forum New To Java
    Replies: 12
    Last Post: 04-25-2009, 10:30 AM
  2. Some while loop problem need help
    By shaggyoo7 in forum New To Java
    Replies: 4
    Last Post: 01-14-2009, 08:16 PM
  3. Loop Problem
    By jralexander in forum New To Java
    Replies: 4
    Last Post: 12-02-2008, 08:08 AM
  4. Problem to use different for loop to add up
    By matt_well in forum New To Java
    Replies: 6
    Last Post: 08-03-2008, 11:24 PM
  5. For loop problem
    By mcal in forum New To Java
    Replies: 32
    Last Post: 01-25-2008, 04:51 PM

Posting Permissions

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