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

    Default Method return type problem

    Here is my program. It is supposed to take the derivative of a given function and display it.

    Java Code:
    import java.util.Scanner;
    public class Derivative {
    	
    	private double power;
    	private double coefficient;
    	private String variable;
    	public Scanner scanner;
    	static Derivative d = new Derivative();
    	String[] terms;
    	String[] derivative;
    	
    	public static void main(String args[]) {
    			System.out.println("The derivative of " + args[1] + " is\n");
    			System.out.println(d.getDerivative(args[1]));
    	}
    
    	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 = "";
    		differentiate(function);	//differentiate the function
    		for(int i = 0; i < derivative.length; i++)
    			answer = answer + derivative[i];
    		return answer;
      }
    	
    	public void analyze(String function) {
    		scanner = new Scanner(function);	//pass the function to the scanner
    		int x = 1;
    		String[] terms = new String[x];
    		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 function after first double 
    			if(scanner.hasNextDouble())	{
    				setPower(scanner.nextDouble());	//set the exponent power as the next double		
    			}
    			if(scanner.next() == "+") {
    				terms[x - 1] = "" + d.getCoefficient() + d.getVariable() + d.getPower();	//set the term into the array
    				x++;						//make the array one slot bigger
    			}
    		}		
    		
    		
    		
    	}
    	public String[] differentiate(String analyzedFunction) {
    		String[] derivative = new String[terms.length];
    		for(int i = 0; i < terms.length; i++) {
    			double newCoefficient = d.getPower() * d.getCoefficient();	//exponent times coefficient
    			double newPower = d.getPower() - 1.0;					//exponent power minus 1
    			derivative[i] = newCoefficient + d.getVariable() + " ^" + newPower;
    			if(i < terms.length)
    				derivative[i] = derivative + " + ";
    		}
    		return derivative[];			//HERE IS A PROBLEM
    		
    	}
    		
    }
    My compiler is telling me
    "Syntax error on "[", expression expected after this token"
    "Type mismatch: cannot convert from String to String[]"

    Why am I not able to return the whole array? Eclipse's suggestion is change the return type to just a String, but I want to return a String array with each element being a differentiated term. How can I fix this?

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

    Default

    derivative is an array. derivative[i] is a String in the array. derivative[] is a syntax error.

    Arrays cannot be dynamically resized once declared. x++ increases the value of x, not the size of the terms array. If you want a growable array, use ArrayList
    Arrays (The Java Tutorials)

    Declaring local variables overrides the instance variables. Thus your methods make no changes to the instance variables, but just discard their results.
    Variables (The Java Tutorials)
    Last edited by OrangeDog; 05-05-2009 at 05:21 AM.
    Don't forget to mark threads as [SOLVED] and give reps to helpful posts.
    How To Ask Questions The Smart Way

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    This is your method:
    Java Code:
      public String[] differentiate(String analyzedFunction)
      {
        String[] derivative = new String[terms.length];
        for (int i = 0; i < terms.length; i++)
        {
          double newCoefficient = d.getPower() * d.getCoefficient(); //exponent times coefficient
          double newPower = d.getPower() - 1.0; //exponent power minus 1
          derivative[i] = newCoefficient + d.getVariable() + " ^" + newPower;
          if (i < terms.length) derivative[i] = derivative + " + ";
        }
        return derivative; //HERE IS A PROBLEM
    
      }
    Note that the method is supposed to return a String array.

    Also note that the derivative variable is already a String array. Thus you can simply call

    return derivative;

    not

    return derivative[];

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

    Default

    oh okay, I get it thanks. I have a question after changing it up.
    Java Code:
    import java.util.Scanner;
    import java.util.ArrayList;
    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 = "";
    		differentiate(function);	//differentiate the function
    		for(int i = 0; i < derivative.size(); i++) {
    			answer = answer + derivative.get(i);
    			if(i < derivative.size())
    				answer = answer + " + ";
    		}
    		return answer;
      }
    	
    	public void analyze(String function) {
    		scanner = new Scanner(function);	//pass the function to the scanner
    		terms = new ArrayList<String>();
    		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(d.getCoefficient() + d.getVariable() + d.getPower());	//set the term into the arraylist
    			if(scanner.next() == "+") {			//***HERE***
    				scanner.skip("+");				//***HERE***
    			}									//***HERE***			
    		}		
    		
    		
    		
    	}
    	public ArrayList<String> differentiate(String analyzedFunction) {
    		derivative = new ArrayList<String>();
    		for(String term: terms) {
    			double newCoefficient = d.getPower() * d.getCoefficient();	//exponent times coefficient
    			double newPower = d.getPower() - 1.0;					//exponent power minus 1
    			derivative.add(newCoefficient + d.getVariable() + " ^" + newPower);
    		}
    		return derivative;			
    		
    	}
    		
    }
    The problem is the last if statement in analyze(String function). If i erase the if statement and enter in only one term, the program works fine. But if I leave that if statement in there and try to enter in more than one, I get a statement saying
    Exception in thread "main" java.util.NoSuchElementException
    at java.util.Scanner.throwFor(Unknown Source)
    at java.util.Scanner.next(Unknown Source)
    at Derivative.analyze(Derivative.java:70)
    at Derivative.getDerivative(Derivative.java:46)
    at Derivative.main(Derivative.java:16)
    I'm assuming that the problem is with the if statement I pointed out. I want to just skip over the + symbols. Like if the next token is a + or - symbol, I want to just skip over it and begin scanning the next term. I looked in the Scanner class page and ArrayList class page at Java 2 Platform SE 5.0, but I didn't see anything. Maybe I overlooked it in my java noobness. I really just need a way to skip over the addition and/or subtraction symbols. Or if the problem is something else, could you point it out to me and direct me to a place of info? Thanks.

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

    Default

    to the top

  6. #6
    xcallmejudasx's Avatar
    xcallmejudasx is offline Senior Member
    Join Date
    Oct 2008
    Location
    Houston, TX & Flint, MI
    Posts
    609
    Rep Power
    6

    Default

    you can just add a delimiter to your scanner that checks for + or - and it will automatically skip them.
    Liberty has never come from the government.
    Liberty has always come from the subjects of government.
    The history of liberty is the history of resistance.
    The history of liberty is a history of the limitation of governmental power, not the increase of it.

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

    Default

    Am I able to have two delimiters for a scanner? I put in the code
    Java Code:
    scanner.useDelimiter("\\s+");
    in analyze after I intialize the scanner. However the program is displaying some odd results for an answer. I'm assuming it's because now the only delimiter is a +, and not white space? I would like both of them to be skipped over or find a way to type in like "3x2" and have it be read as 3 x squared instead of having to type it in as "3 x 2"

  8. #8
    xcallmejudasx's Avatar
    xcallmejudasx is offline Senior Member
    Join Date
    Oct 2008
    Location
    Houston, TX & Flint, MI
    Posts
    609
    Rep Power
    6

    Default

    I'm sure it can. You just have to set the pattern up properly or attach multiple delimiters(if possible)
    Java 2 Platform SE 5.0
    check the useDelimiter method of Scanner
    Pattern (Java 2 Platform SE 5.0)
    how to put together your delimiting pattern
    Liberty has never come from the government.
    Liberty has always come from the subjects of government.
    The history of liberty is the history of resistance.
    The history of liberty is a history of the limitation of governmental power, not the increase of it.

Similar Threads

  1. Replies: 3
    Last Post: 06-05-2010, 08:05 PM
  2. Replies: 4
    Last Post: 11-25-2008, 05:53 AM
  3. return type determines override/overload?
    By hedefalk in forum Advanced Java
    Replies: 4
    Last Post: 07-11-2007, 01:48 PM
  4. The return type
    By Marcus in forum New To Java
    Replies: 1
    Last Post: 07-05-2007, 06:28 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
  •