so I am trying do this problem involving linked lists where i need to basically make 2 linked lists each representing a polynomial.
I got all that down... now I need to make a function to add the two polynomials and this is where i got stuck. I have the code but I keep getting the "cannot find symbol" error when I compile... any tips?

//Class for polynomial linked list
import java.util.Scanner;

class Node {
    public int numC;
	 public int numE;
    public Node next;

    // elementary constructor
    public Node() {
        numC = 0;
		  numE = 0;
        next = null;
    }

    // constructor with two argument
    public Node(int c, int e) {
        numC = c;
		  numE = e;
        next = null;
    }

    // primary constructor
    public Node(int c, int e, Node n) {
        numC = c;
		  numE = e;
        next = n;
    }

    // accessor method
    public int getCo() {
        return numC;
    }
	 
	 public int getEx(){
	     return numE;
	}

    // accessor method
    public Node getNext() {
        return next;
    }

	// mutator method
	public void setCof(int c){
		numC = c;
	}
	
	public void setExp(int e){
		numE = e;
	}
    // mutator method for next
    public void setNext(Node nextNode) {
        next = nextNode;
    }
} // end of class Node



class Polynomial {
	private Node front;
	public Polynomial(){
		front = null;
	}
	
	//read coefficient fx
	public int readCoFx(){
	Scanner scan = new Scanner(System.in);
	int coef = 0;
	
	System.out.print("Enter Coefficient or 0 to skip to next equation: ");
	coef = scan.nextInt();
	return coef;
	}
	//read coefficient gx
	public int readCoGx(){
	Scanner scan = new Scanner(System.in);
	int coef = 0;
	
	System.out.print("Enter Coefficient or 0 to end program: ");
	coef = scan.nextInt();
	return coef;
	}
	//read exponent
	public int readEx(){
	Scanner scan = new Scanner(System.in);
	int exp = 0;
	
	System.out.print("Enter Exponent: ");
	exp = scan.nextInt();
	return exp;
	}
	
	//insert Node
	public void insert(int coef, int exp){
		Node temp = new Node(coef, exp, null);
		//insert temp to 1st if empty
		if (front == null)
			front = temp;
		else {
			Node p = null;
			Node q = front;
			//insert in front if exponent is higher
			if (temp.getEx() > q.getEx()){
			temp.setNext(front);
			front = temp;
			} else {//insert at middle or end
				while (q != null && q.getEx() > temp.getEx()){
					p = q;
					q = q.getNext();
				}
				p.setNext(temp);
				temp.setNext(q);
			}
		}
	}
	//print function for the equations
	public void print(){
		Node p = front;
		while(p != null){
			System.out.print("("+p.getCo()+ "x" + p.getEx()+")");
			p = p.getNext();
		}
		System.out.println("");
	}	
	
	
	//function to add polynomials
	public static Polynomial addPoly(Polynomial poly1, Polynomial poly2){
		Polynomial tempPoly = new Polynomial();
		//if poly1 exp is > poly 2 exp then power = poly 1 exp else power = poly 2 exp
		int power = (poly1.getEx() > poly2.getEx()) ? poly1.getEx() : poly2.getEx();
		
		while (power >=0){
			Node n1 = poly1.setNext(front);
			while (n1 != null){
				if (n1.getEx() == power)
				break;
				n1 = n1.getNext();
			}
			
			Node n2 = poly2.setNext(front);
			while (n2 != null){
				if (n2.getEx() == power)
				break;
				n2 = n2.getNext();
			}
			
			if((n1 != null) && (n2 != null))
				tempPoly.insert((n1.getCo() + n2.getCo()), n1.getEx());
			else if (n1 != null)
				tempPoly.insert(n1.getCo(), n1.getEx());
			else if (n2 != null)
				tempPoly.insert(n2.getCo(), n2.getEx());
			power--;
		}
		return tempPoly;
	}
}

//driver
public class PolyDrive {
	public static void main(String [] args){
		Polynomial poly1 = new Polynomial();
		Polynomial poly2 = new Polynomial();
		Polynomial poly3 = new Polynomial();
		int co;
		int ex;
		
		do {
			co = poly1.readCoFx();
				if(co != 0){
				ex = poly1.readEx();
				poly1.insert(co, ex);
				}
			//poly1.print();
		} while (co != 0);
		
		do {
			co = poly2.readCoGx();
				if(co != 0){
				ex = poly1.readEx();
				poly2.insert(co, ex);
				}
			//poly2.print();
		} while (co != 0);
		poly1.print();
		poly2.print();
		poly3.addPoly(poly1,poly2);
		
	}
}
PolyDrive.java:132: cannot find symbol
symbol  : method getEx()
location: class Polynomial
		int power = (poly1.getEx() > poly2.getEx()) ? poly1.getEx() : poly2.getEx();
		                  ^
PolyDrive.java:132: cannot find symbol
symbol  : method getEx()
location: class Polynomial
		int power = (poly1.getEx() > poly2.getEx()) ? poly1.getEx() : poly2.getEx();
		                                  ^
PolyDrive.java:132: cannot find symbol
symbol  : method getEx()
location: class Polynomial
		int power = (poly1.getEx() > poly2.getEx()) ? poly1.getEx() : poly2.getEx();
		                                                   ^
PolyDrive.java:132: cannot find symbol
symbol  : method getEx()
location: class Polynomial
		int power = (poly1.getEx() > poly2.getEx()) ? poly1.getEx() : poly2.getEx();
		                                                                   ^
PolyDrive.java:135: cannot find symbol
symbol  : method setNext(Node)
location: class Polynomial
			Node n1 = poly1.setNext(front);
			               ^
PolyDrive.java:142: cannot find symbol
symbol  : method setNext(Node)
location: class Polynomial
			Node n2 = poly2.setNext(front);
			               ^
6 errors

Your Polynomial class does not have a getEx() method. In order to call the getEx() method it must exist.

edit: It's because your Polynomial class has a Node, and your Node class has a getEx() method. Therefore you must say

poly1.front.getEx()

(assuming your Polynomial is called poly1 and your Node inside that Polynomial is called front)

Edited 6 Years Ago by BestJewSinceJC: n/a

hrm...so how can i access the values that are in the linked lists to use for the addPoly method?

Ok I got it to compile and run.. however when i go to print the new polynomial that is added, it comes out as blank.

Here is my revised addPoly

//function to add polynomials
	public static Polynomial addPoly(Polynomial poly1, Polynomial poly2){
		Polynomial tempPoly = new Polynomial();
		//if poly1 exp is > poly 2 exp then power = poly 1 exp else power = poly 2 exp
		int power = (poly1.front.getEx() > poly2.front.getEx()) ? poly1.front.getEx() : poly2.front.getEx();
		
		while (power >=0){
			Node n1 = poly1.front;
			while (n1 != null){
				if (n1.getEx() == power)
				break;
				n1 = n1.getNext();
			}
			
			Node n2 = poly2.front;
			while (n2 != null){
				if (n2.getEx() == power)
				break;
				n2 = n2.getNext();
			}
			
			if((n1 != null) && (n2 != null))
				tempPoly.insert((n1.getCo() + n2.getCo()), n1.getEx());
			else if (n1 != null)
				tempPoly.insert(n1.getCo(), n1.getEx());
			else if (n2 != null)
				tempPoly.insert(n2.getCo(), n2.getEx());
			power--;
		}
		return tempPoly;
	}
}

Here is my output
Enter Coefficient or 0 to skip to next equation: 3
Enter Exponent: 2
Enter Coefficient or 0 to skip to next equation: 4
Enter Exponent: 3
Enter Coefficient or 0 to skip to next equation: 3
Enter Exponent: 1
Enter Coefficient or 0 to skip to next equation: 0
Enter Coefficient or 0 to end program: 4
Enter Exponent: 2
Enter Coefficient or 0 to end program: 7
Enter Exponent: 3
Enter Coefficient or 0 to end program: 5
Enter Exponent: 1
Enter Coefficient or 0 to end program: 0
(4x3)(3x2)(3x1)
(7x3)(4x2)(5x1)


----jGRASP: operation complete.

I can't seem to figure out where I went wrong and why it won't print. I called the same print function that I used to print the first 2 linked lists.

This line

poly3.addPoly(poly1,poly2);

will return the result of adding the two parameters - but you aren't storing that result in anything.
To use the result you would use the function like so

Polynomial result = Polynomial.addPoly(poly1,poly2);

and you would then want to print that result.
(Note the different syntax for calling a static method. You use the class name - not an instance.)

Edited 6 Years Ago by Ezzaral: n/a

This question has already been answered. Start a new discussion instead.