The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

Please help about data structures.

Please help me, I got stuck on this one from yesterday til now. Long story short : I have to build an java apps for doing polynomial : addition, subtraction, derivative .... The data structure chosen is linked list. There are five classes

Term class : hold the exponent and coefficient for each term of the polynomial

Node class : each node have a term in it and a link to the next Node

Orderedsequences class: contains the head of the ordered sequence, the term must be arranged from high exponent to low exponent. orderedsequence also contains a cursor to show the current term, and a count to show how many term in there.

Previously, when I use array as the data structure to hold the term, everything is fine, when I change to linked list lost of thing happen. I think I have a trouble with the insert method (insert new terms into orderedsequence) and isCurrent method in the Orderedsequence class. Please help.

publicclass Term implements Comparable<Term>{privatedouble coeff;privateint exp;//constructorpublic Term (double x,int y){
coeff=x;
exp=y;}// Return the value of the coefficient of this termpublicdouble getCoeff(){return coeff;}//Return the value of the power of this termpublicint getExp(){return exp;}//modify the coefficient of a termpublicvoid setCoeff(double d){
coeff = d;}//modify the coefficient of a termpublicvoid setExp(int i){
exp = i;}//this term and term a has the same power//Return a term that is a sum of this term and apublic Term add(Term a){returnnew Term(coeff+a.coeff,exp);}//this term and term a has the same power//Return a term that is a substraction of term a from this termpublic Term subtr(Term a){returnnew Term(coeff-a.coeff,exp);}//Return the differnce of the two terms which has same powerpublic Term subtract(Term t){returnnew Term(coeff-t.coeff,exp);}//Return a term that is a multiplication of this term and term apublic Term mult(Term a){returnnew Term(coeff*a.coeff,exp+a.exp);}//Return a term that is the derivative of this termpublic Term derivative(){returnnew Term (coeff*exp,exp-1);}//Return a term that is the integral of this termpublic Term integral(){returnnew Term(coeff/(exp+1),exp+1);}//Return the value of the definite integral from a to b of this termpublicdouble defintegral(double a,double b){return(integral().eval(b)-integral().eval(a));}//Compare the exponent of the two terms, return 0 if equals, -1 < , 1 >publicint compareTo(Term t){int ans =0;if(exp>t.exp) ans =1;elseif(exp<t.exp) ans=-1;return ans;}publicboolean equals(Object obj){boolean ans =false;if(obj instanceof Term && compareTo((Term)obj)==0&&coeff==((Term)obj).coeff) ans=true;return ans;}//Return value of this term at a particulate valuepublicdouble eval(double x){double ans=coeff*Math.pow(x,exp);return ans;}//Return a string that describes this term.publicString toString(){String ans="";if(exp>1&&coeff!=0) ans=ans+coeff+"x^"+exp+" ";elseif(exp==1&&coeff>1) ans=ans+coeff+"x";elseif(exp==0&&coeff!=0) ans=ans+coeff;elseif(exp==1&&coeff==1) ans=ans+"x";return ans;}}

publicclass OrderedSequence implementsCloneable{private Node head;privateint count;private Node cursor ;//Constructor with any lengthpublic OrderedSequence(){
head=null;
count=0;cursor=head;}//insert a term in the ordered sequence. Ordered from high exponent to low exponent.When the new term has same exponen//with an existing term, add the two together, if the result exponent is zero, remove the term. publicvoid insert(Term t){if(head==null){
head=new Node(t,null);}else{if(t.getExp()>head.getTerm().getExp()){
head=new Node (t,head);
count++;}else{
start();while(cursor != null&&t.getExp()!=cursor.getTerm().getExp()){
advance();}if(cursor!=null)//the power of t is the same as one of the exp in the os{
cursor.setTerm(new Term(t.getCoeff()+cursor.getTerm().getCoeff(),t.getExp()));
count++;if(cursor.getTerm().getCoeff()==0){
removeCurrent();
count--;}}else//cursor is null, there is no term in os has the same power as t{
cursor.addNodeafter(t);count++;}}}}//restart the cursor to the beginning positionpublicvoid start(){
cursor = head;}// return the number of terms in the arraypublicint getCount(){return count;}//return the current position in the sequencepublic Term getCurrent(){return cursor.getTerm();}//check if cursor is in the current positions in this ordered sequencepublicboolean isCurrent(){boolean answer =false;if(cursor.getTerm()!=null)
answer =true;return answer;}//move the cursor to the next termpublicvoid advance(){if(isCurrent()==true)
cursor=cursor.getLink();}//remove the term at current publicboolean removeCurrent(){boolean ans=false;if(isCurrent()==true){
cursor.setTerm(cursor.getLink().getTerm());
cursor.setLink(cursor.getLink().getLink());
ans=true;}return ans;}//merge two ordered sequence togetherpublic OrderedSequence merge(OrderedSequence os){
OrderedSequence ans=clone();start();while(cursor!=null){
ans.insert(os.getCurrent());
advance();}return ans;}public OrderedSequence clone(){
OrderedSequence ans=new OrderedSequence();
start();while(cursor!=null){
ans.insert(getCurrent());
advance();}return ans;}/* public OrderedSequence clone(){
OrderedSequence ans ;
try
{
ans=(OrderedSequence)super.clone();
}
catch (CloneNotSupportedException e)
{
throw new RuntimeException("This class does not implement Cloneable");
}
ans.head = head.clone();
return ans;} */}

publicclass TestLab2x{publicstaticvoid main (String arg[]){//Make a new term 3.5x^4
Term t1=new Term(2,4);
Term tt=new Term(2,4);
Term t2=new Term(2.5,4);
Term t3=t1.add(t2);
Term t4=new Term(1,1);Term t5=new Term(2,2);Term t6=new Term(3,3);Term t7=new Term(5,5);Term t8=new Term(10,0);System.out.println("This is the term t1 "+t1);//Compare term t1 and ttSystem.out.println("Compare t1 and tt "+t1.equals(tt));System.out.println("Compare t1 and t2 "+t1.equals(t2));System.out.println("Compare t1 and 1000 "+t1.equals(1000));//Extract the coefficient of t1System.out.println("The coefficient of t1 is "+t1.getCoeff());//Extract the exponent of t1System.out.println("The exponent of t1 is "+t1.getExp());System.out.println("This is the term t3 "+t3);//Extract the coefficient of t1System.out.println("The coefficient of t3 is "+t3.getCoeff());//Extract the exponent of t1System.out.println("The exponent of t3 is "+t3.getExp());//Derivative of a term
Term t9=t1.derivative();System.out.println("Display t9 "+t9.toString());
Term t10=t8.derivative();System.out.println("Display t10 "+t10.toString());//Evaluate t1 at x=2System.out.println("Value of t1 at x=2 is "+ t1.eval(2));System.out.println("Display t8 "+t8.toString()+"\n");
Polynomial p1=new Polynomial();
p1.insert(t1);p1.insert(t5);System.out.println("Polynomial p1 is : "+p1.toString());}}

Re: Please help about data structures.

This is what I got from running those codes:

java.lang.NullPointerException
at OrderedSequence.insert(OrderedSequence.java:56)
at Polynomial.insert(Polynomial.java:149)
at TestLab2x.main(TestLab2x.java:40)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Nativ e Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknow n Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Un known Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at edu.rice.cs.drjava.model.compiler.JavacCompiler.ru nCommand(JavacCompiler.java:272)