12 Replies - 1375 Views - Last Post: 31 August 2010 - 02:02 PM Rate Topic: -----

#1 mon mon   User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 128
  • Joined: 31-December 09

error in sum functions method

Posted 31 August 2010 - 08:47 AM

/** This class is Sum Functions for 
  * sum of two real functions
  * such as f1(x) + f2(x)
  */
import java.util.*;

public class SumFunctions implements RealFunction {
  
  /** This constructor sum two functions
    * and save them
    * @param f1 & f2 are two
    * objects of Real Functions
    */
  public SumFunctions ( RealFunction f1 , RealFunction f2 )
  {
    RealFunction f3  = new SumFunctions(f1 , f2);
    String sum = "f3" +" = " + f1 + "+" + f2;
  }
  
  
  
  /** This method for sum two
    * real functions
    * @param
    * @return
    */
  public double valueAt(double x)
  {
    //double sum = 0.0;
    return SumFinction(f1 , f2);
  }
}
/*********************************************************************/

/** This interface RealFunction for 
  * define some simple
  * mathematical functions
  */

public interface RealFunction {
  /** This method for make 
    * functionthat take value and return it
    * after make calculate in this function
    * @param x is variable in function 
    * @return the value after calculation 
    * in function*/
  
  public double valueAt(double x);
}





i have error in cannot find symbol
symbol : variable f1

cannot find symbol
symbol : variable f2
location: class SumFunctions


i want to make code such as example but make error

RealFucntion f1 = new QuadraticFunction(1); // a = 1
RealFunction f2 = new CosineFunction();
RealFunction f3 = new LinearFunction(2, -1);// a = 2, b = -1
RealFunction g = new SumFunctions(f1,new
SumFunctions(f2,f3));

// Now print the value of the function g(x)
// g(x) = f1(x) + f2(x) + f3(x) = x^2 + 2x - 1 + cos(x)
// for x = 1.2
System.out.println(g.valueAt(1.2));

Is This A Good Question/Topic? 0
  • +

Replies To: error in sum functions method

#2 guido-granobles   User is offline

  • D.I.C Addict
  • member icon

Reputation: 171
  • View blog
  • Posts: 617
  • Joined: 02-December 09

Re: error in sum functions method

Posted 31 August 2010 - 08:52 AM

This is wrong:
public double valueAt(double x)
{
    //double sum = 0.0;
    return SumFinction(f1 , f2);
}




Because there is not any attribute f1 and f2 in the SumFunctions class . So you will need pass these values as parameters of the method valueAt.

This post has been edited by guidojavier: 31 August 2010 - 08:55 AM

Was This Post Helpful? 0
  • +
  • -

#3 KYA   User is offline

  • Wubba lubba dub dub!
  • member icon

Reputation: 3202
  • View blog
  • Posts: 19,235
  • Joined: 14-September 07

Re: error in sum functions method

Posted 31 August 2010 - 08:53 AM

And SumFinction is misspelled.
Was This Post Helpful? 0
  • +
  • -

#4 mon mon   User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 128
  • Joined: 31-December 09

Re: error in sum functions method

Posted 31 August 2010 - 09:02 AM

View Postguidojavier, on 31 August 2010 - 07:52 AM, said:

This is wrong:
public double valueAt(double x)
{
    //double sum = 0.0;
    return SumFinction(f1 , f2);
}




Because there is not any atributte from class named f1 and f2. So you will need pass these values as parameters of the method valueAt.

but i see it false to make it so i want such as
RealFucntion f1 = new QuadraticFunction(1); // a = 1
RealFunction f2 = new CosineFunction();
RealFunction f3 = new LinearFunction(2, -1);// a = 2, b = -1
RealFunction g = new SumFunctions(f1,new
SumFunctions(f2,f3));

// Now print the value of the function g(x)
// g(x) = f1(x) + f2(x) + f3(x) = x^2 + 2x - 1 + cos(x)
// for x = 1.2
System.out.println(g.valueAt(1.2));

View PostKYA, on 31 August 2010 - 07:53 AM, said:

And SumFinction is misspelled.

the problem is not in this misspelled

This post has been edited by mon mon: 31 August 2010 - 09:18 AM

Was This Post Helpful? 0
  • +
  • -

#5 guido-granobles   User is offline

  • D.I.C Addict
  • member icon

Reputation: 171
  • View blog
  • Posts: 617
  • Joined: 02-December 09

Re: error in sum functions method

Posted 31 August 2010 - 09:24 AM

Althought I do not understand what exactly you want to do I can see this:
return SumFinction(f1 , f2);



SumFunctions is a class and its constructor is of course SumFunctions so this statement would be like:
return (new SumFinction(f1 , f2));



But your method is suppose to return a double value so I cannot get what you want. I wonder if you want return the result of mathematics operations or an object. Beside that what is the purpose of the constructor:
public SumFunctions ( RealFunction f1 , RealFunction f2 )
{
    RealFunction f3  = new SumFunctions(f1 , f2);
    String sum = "f3" +" = " + f1 + "+" + f2;
}



The variables f3 and sum just exist inside of the constructor. Outside of the constructor these variables does not exist. Can you be more especific about how this is intended to work.
Was This Post Helpful? 0
  • +
  • -

#6 KYA   User is offline

  • Wubba lubba dub dub!
  • member icon

Reputation: 3202
  • View blog
  • Posts: 19,235
  • Joined: 14-September 07

Re: error in sum functions method

Posted 31 August 2010 - 09:41 AM

SumFunction != SumFinction


Do you have another class or method that you haven't shown?
Was This Post Helpful? 0
  • +
  • -

#7 mon mon   User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 128
  • Joined: 31-December 09

Re: error in sum functions method

Posted 31 August 2010 - 09:45 AM

View Postguidojavier, on 31 August 2010 - 08:24 AM, said:

Althought I do not understand what exactly you want to do I can see this:
return SumFinction(f1 , f2);



SumFunctions is a class and its constructor is of course SumFunctions so this statement would be like:
return (new SumFinction(f1 , f2));



But your method is suppose to return a double value so I cannot get what you want. I wonder if you want return the result of mathematics operations or an object. Beside that what is the purpose of the constructor:
public SumFunctions ( RealFunction f1 , RealFunction f2 )
{
    RealFunction f3  = new SumFunctions(f1 , f2);
    String sum = "f3" +" = " + f1 + "+" + f2;
}



The variables f3 and sum just exist inside of the constructor. Outside of the constructor these variables does not exist. Can you be more especific about how this is intended to work.



the constructor for sum two real functions such as f1(x)+ f2(x)
and the method public double valueAt(double x)
return value of sum after pass the value of x to sum function

View PostKYA, on 31 August 2010 - 08:41 AM, said:

SumFunction != SumFinction


Do you have another class or method that you haven't shown?

you read the topic carefully
and i know that SumFunction != SumFinction but the problem not in it

This post has been edited by mon mon: 31 August 2010 - 09:43 AM

Was This Post Helpful? 0
  • +
  • -

#8 KYA   User is offline

  • Wubba lubba dub dub!
  • member icon

Reputation: 3202
  • View blog
  • Posts: 19,235
  • Joined: 14-September 07

Re: error in sum functions method

Posted 31 August 2010 - 09:51 AM

How can I recreate your working environment if I don't have all the pieces?


Either way you're calling f1 and f2 in a function where they are not defined. We can't trace your thought pattern because we don't know what SumFinctions is, what it does, or how it relates to the rest of the code present.
Was This Post Helpful? 0
  • +
  • -

#9 mon mon   User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 128
  • Joined: 31-December 09

Re: error in sum functions method

Posted 31 August 2010 - 09:55 AM

can post the Question to understand what i need i do & what i made in my code
Was This Post Helpful? 0
  • +
  • -

#10 mon mon   User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 128
  • Joined: 31-December 09

Re: error in sum functions method

Posted 31 August 2010 - 10:05 AM

public interface RealFunction 
{
  
  public double valueAt(double x);
}


this interface

and i make class SumFunctions implements Real function

import java.util.*;

public class SumFunctions implements RealFunction {
  
  public SumFunctions ( RealFunction f1 , RealFunction f2 )
  {
    RealFunction f3  = new SumFunctions(f1 , f2);
    String sum = "f3" +" = " + f1 + "+" + f2;
  }
  
  public double valueAt(double x)
  {
    
    return (new SumFinctions(f1 , f2));
  }
}



this the Question:
class SumFunctions - a function that represents the sum of two real

functions, e.g. f1(x) + f2(x). The class receives two RealFunctions as

parameters in its constructor and saves them to calculate their sum for

any given x value.

Consider the following code example:

RealFucntion f1 = new QuadraticFunction(1); // a = 1

RealFunction f2 = new CosineFunction();

RealFunction f3 = new LinearFunction(2, -1);// a = 2, b = -1

RealFunction g = new SumFunctions(f1,new SumFunctions(f2,f3));

// Now print the value of the function g(x)

// g(x) = f1(x) + f2(x) + f3(x) = x^2 + 2x - 1 + cos(x)

// for x = 1.2

System.out.println(g.valueAt(1.2));

i hope to understand what i need
Was This Post Helpful? 0
  • +
  • -

#11 guido-granobles   User is offline

  • D.I.C Addict
  • member icon

Reputation: 171
  • View blog
  • Posts: 617
  • Joined: 02-December 09

Re: error in sum functions method

Posted 31 August 2010 - 12:27 PM

I think I know what you want. If I were you I would have the RealFunction interface like this:
public interface RealFunction {
	
	// an expression is something like 5x, num is the number that is involve in the expresion like 5 
	//  operator is the operation for apply to the expression  like '5x +'  and free means 
	//  whether the number is free or has to be multiplicate by X.
	 
	public void addExpression(double num, String operator, boolean free);
	public double getNum();
	public String getOperator();
	public void setOperator(String operator);
	
	//nest is an object which aim to the next expression
	public Function getNext();
	
	// nest is an object which aim to the last expression 
	public Function getLast();
	
	public boolean isFree();
	
	//return the result of operate all the expressions
	public double valueAt(double x);
}



The class which implements this interface will have to work as a linked list where every expression will be a element from the list. For example the function 5x + 9x + 3. In this case 5x is an expression, 9x other expression and 3 the last expression. When you sum two function you will have to link the last expression from the first function with the first expression from the second function. In order to do that the class which implements the interface must to have two objects of itself. The first object is for point to the next expression and the second is for point to the last expression always. Finally when valueAt(x) method is called this method must use the value of x for calculate every expression making use of its operators (+,-,^....). So it will have iterate over the list .

This post has been edited by guidojavier: 31 August 2010 - 12:33 PM

Was This Post Helpful? 0
  • +
  • -

#12 mon mon   User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 128
  • Joined: 31-December 09

Re: error in sum functions method

Posted 31 August 2010 - 01:12 PM

View Postguidojavier, on 31 August 2010 - 11:27 AM, said:

I think I know what you want. If I were you I would have the RealFunction interface like this:
public interface RealFunction {
	
	// an expression is something like 5x, num is the number that is involve in the expresion like 5 
	//  operator is the operation for apply to the expression  like '5x +'  and free means 
	//  whether the number is free or has to be multiplicate by X.
	 
	public void addExpression(double num, String operator, boolean free);
	public double getNum();
	public String getOperator();
	public void setOperator(String operator);
	
	//nest is an object which aim to the next expression
	public Function getNext();
	
	// nest is an object which aim to the last expression 
	public Function getLast();
	
	public boolean isFree();
	
	//return the result of operate all the expressions
	public double valueAt(double x);
}



The class which implements this interface will have to work as a linked list where every expression will be a element from the list. For example the function 5x + 9x + 3. In this case 5x is an expression, 9x other expression and 3 the last expression. When you sum two function you will have to link the last expression from the first function with the first expression from the second function. In order to do that the class which implements the interface must to have two objects of itself. The first object is for point to the next expression and the second is for point to the last expression always. Finally when valueAt(x) method is called this method must use the value of x for calculate every expression making use of its operators (+,-,^....). So it will have iterate over the list .


but this need to override in class SumFunctions
and you operate in interface RealFunction only not class SumFunction
and interface RealFunction want to be such i wrote before and implements
it in class SumFunctions
Was This Post Helpful? 0
  • +
  • -

#13 guido-granobles   User is offline

  • D.I.C Addict
  • member icon

Reputation: 171
  • View blog
  • Posts: 617
  • Joined: 02-December 09

Re: error in sum functions method

Posted 31 August 2010 - 02:02 PM

Then there is not problem just let the interface as it was:
public interface RealFunction {
	
	//return the result of operate all the expressions
	public double valueAt(double x);
}



and the SumFunctions would be:
public class SumFunctions implements  RealFunction{
  
	private double num;
	private  String operator;
	private SumFunctions next;
	private SumFunctions last;
	private boolean free;
	
	public SumFunctions(){}
	
	public SumFunctions(double n, String op, boolean f){
		num = n;
		operator = op;
		free = f;
	}
	
	
	public double getNum() {
		return num;
	}

	public String getOperator() {
		return operator;
	}

	public void setOperator(String operator) {
		this.operator = operator;
	}


        //get the next expression of the list
	public SumFunctions getNext() {
		return next;
	}


        //is this expression free of multiplications
	public boolean isFree() {
		return free;
	}


        //get the last expression of the list
	public SumFunctions getLast() {
		return last;
	}


        //set the next expression of the list
	public SumFunctions setNextExpresion(double n, String op, boolean f){
		if(next == null){
                        //is the first expression of the list;
			last = next = new  SumFunctions(n,op, f);
		}else{
                       //set the next expression and make it the last too
			last = next.setNextExpresion(n, op, f);
		}
		
		return last;
	}
	
        //This will iterate over the list
	public double getResult(double x){
		double val = 0;
		
		//free mean that num does not have to be operate with x value.
		if(!free){
			val = x*num;			
		}else{
			val = num;
		}
		
		
		
		if(operator.equals("+")){
			val += next.getResult(x);
		}else if(operator.equals("+")){
			val -= next.getResult(x);
		}
		//More operations here.........
		
		return val;
	}




	public void addExpression(double num, String operator, boolean free) {
		setNextExpresion(num, operator, free);
		
	}
	
	 public static SumFunctions sum( SumFunctions f1 , SumFunctions f2 )
	  {
	       f1.getLast().setOperator("+");
	       f1.addExpression(f2.getNum(), f2.getOperator(), f2.isFree());
	       SumFunctions nx = f2.getNext();
	       while(nx != null){
	    	   f1.addExpression(nx.getNum(), nx.getOperator(), nx.isFree());
	    	   nx = nx.getNext();
	       }
	       
	       return f1;
	       
	  }

	@Override
	public double valueAt(double x) {
			
		return getResult(x);
	}

	public static void main(String[] args){
		
		//5x + 8x + 4
		SumFunctions f1 = new SumFunctions(5, "+", false);
		f1.addExpression(8, "+", false);
		f1.addExpression(4, "", true);
		
		//10x - 9x + 6
		SumFunctions f2 = new SumFunctions(10, "+", false);
		f2.addExpression(9, "-", false);
		f2.addExpression(6, "", true);
		
		//f1(x) + f2(x)
		//(5x + 8x + 4) + (10x + 9x - 6)
		f1 =   sum(f1, f2);

		double res = f1.valueAt(2);
		
		System.out.println(res);
		  
	}
}



This is just an example. It just works for sum and substract but it is a begining. I think this can be useful for you. There is many things to improve. You can change it as you want in order to make it fit to your requirements.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1