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

    Default Take a derivative

    I want to take and display the derivative of a given input.

    Java Code:
    import java.util.Scanner;
    public class Derivative {
    	
    	private int power;
    	private int coefficient;
    	private String variable;
    	
    	public static void main(String args[]) {
    		Derivative d = new Derivative();
    		System.out.println(d.getDerivative(args[0])); 	//LINE 10
    		
    	}
    
    	public void setPower(int power) {
    		this.power = power;
    	}
    
    	public int getPower() {
    		return power;
    	}
    
    	public void setCoefficient(int coefficient) {
    		this.coefficient = coefficient;
    	}
    
    	public int getCoefficient() {
    		return coefficient;
    	}
    
    	public void setVariable(String variable) {
    		this.variable = variable;
    	}
    
    	public String getVariable() {
    		return variable;
    	}
    	
    	public String getDerivative(String term)
    	{
    		String derivative;
    		Scanner scanner = new Scanner(term);	//pass the term to the scanner
    		if(scanner.hasNextInt())	
    			setCoefficient(scanner.nextInt());		//set the coefficient as the first integer		
    		setVariable(scanner.next());			//set the variable as the next term after first integer 
    		if(scanner.hasNextInt())
    			setPower(scanner.nextInt());	//set the exponent power as the next integer		
    		int newCoefficient = power * coefficient;	//differentiate
    		int newPower = power - 1;
    		derivative = newCoefficient + variable + "^" + newPower;			
    		return derivative;
      }
    }
    I tried to input 2x2, which really is 2x squared. Obviously the answer should be 4x^1, but I keep getting 02x2^-1
    as a result. Whenever I call a hasNext... method with scanner, does the scanner move up a token? So that whenever I check, it moves, then I assign, and it moves, and I assign and check and etc etc. I feel like I'm doing something obviously wrong with the scanner, but I don't know how to fix it.

    I also tried
    Java Code:
    	public String getDerivative(String term)
    	{
    		String derivative;
    		Scanner scanner = new Scanner(term);	//pass the term to the scanner
    		setCoefficient(scanner.nextInt());		//set the coefficient as the first integer		
    		setVariable(scanner.next());			//set the variable as the next term after first integer 
    		setPower(scanner.nextInt());	//set the exponent power as the next integer		
    		int newCoefficient = power * coefficient;	//differentiate
    		int newPower = power - 1;
    		derivative = newCoefficient + variable + "^" + newPower;			
    		return derivative;
      }
    But that kept telling me mismatched input.

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

    Default

    You haven't configured the delimiters on your scanner. With default setup, your string has one token, '2x2', which cannot be parsed as an integer. hasNextInt() will return false, next() will return the whole token and the results will be as you see them.
    Don't forget to mark threads as [SOLVED] and give reps to helpful posts.
    How To Ask Questions The Smart Way

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

    Default

    Awesome thanks, you've been a big help in pointing out mistakes for me today. Hopefully I won't need to ask any more questions haha.

Posting Permissions

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