Flash Website Builder- Trendy Site Builder is a Flash Site Building tool that helps users build stunning websites. Check Out Custom Custom Logo Design by LogoBee. Website Design and Free Logo Templates available.

Before you post, read our: Rules & Posting Guidelines

Enjoy an ad free experience by logging in. Not a member yet? Register.
 10-20-2011, 07:27 AM PM User | #1 zabjade New to the CF scene   Join Date: Sep 2011 Posts: 9 Thanks: 2 Thanked 0 Times in 0 Posts Polynomial linked list Unlike my last post, this one is homework. I really wish I could just take a beginners java class instead of trying to do this based on C++ programming classes a year ago. Unfortunately, I'm not allowed to take "extra" classes that don't count towards my degree and still keep my financial aid. Anyway, here's what I have: Code: ```class Node { private int exponent, coefficient; private Node next; public Node() { exponent = 0; coefficient = 0; next = null; } public void setCoefficient (int coeff) { coefficient = coeff; } public int getCoefficient() { return coefficient; } public void setExponent(int exp) { exponent = exp; } public int getExponent() { return exponent; } public void setNext(Node poly) { next = poly; } public Node getNext() { return next; } } class Polynomial { private Node head = new Node(); public Polynomial() { head = null; } public void Insert(int coefficient, int exponent) { Node current = null, previous = null; boolean inserted = false; Node newNode = new Node(); newNode.setCoefficient(coefficient); newNode.setExponent(exponent); newNode.setNext(null); if (head == null) head = newNode; else if (exponent > head.getExponent() ) { newNode.setNext(head); head = newNode; } else { inserted = false; previous = head; current = head.getNext(); } while(inserted == false && current != null ) { if (exponent > current.getExponent() ) { previous.setNext(newNode); newNode.setNext(current); inserted = true; } else { current = current.getNext(); previous = previous.getNext(); } } if (current == null) previous.setNext(newNode); } }``` For the assignment, we're supposed to create a linked list for polynomials that sorts them based on the exponent. The sorting is supposed to take place in the Polynomial class. I'm pretty sure the Insert function is very wrong. The professor sent it to me as help in something that seemed to be part pseudocode, part java, and part C++. I have to comment out the last if statement or the program has conniptions. I'm also supposed to have a class called Term, which I believe is meant to determine where the X in the polynomials goes, and if it's shown at all. I have a vague idea on that one that popped into my head while I was taking a bath at two in the morning. Could anyone help me figure out what I'm doing wrong with the Polynomial class, and if I have anything wrong in the node class?
 10-20-2011, 05:33 PM PM User | #2 Fou-Lu God Emperor     Join Date: Sep 2002 Location: Saskatoon, Saskatchewan Posts: 16,595 Thanks: 4 Thanked 2,597 Times in 2,566 Posts Something feels wrong about both classes. LinkedLists like other collections tend to favour non-specific datatypes. The Node class you have here is designed specifically for the polynomial. The polynomial itself is simply a collection of Term. Since you have been given an insert method, that would indicate that this is to be developed from scratch instead of by using a builtin collection, which now means your Node should probably just be labeled as your Term. So you now have a Polynomial pseudocollection of type Term. I don't see anything wrong with the insert method as defined specifically for the polynomial. As for the Term class, I'm not sure what you need to do with it either. Given that this is a custom list, my perception of a polynomial is simply that it is a collection of Term (or more accurately, I'd call it a collection of ITerm since they can be either constant or variable). If you were to use a builtin collection, it would be tremendously easier (but I think the creation is the actual assignment :/) How I perceive it: PHP Code: ``` public interface ITerm {     public final Comparator SORT_BY_EXP_DESC = new Comparator()     {         @Override         public int compare(ITerm t1, ITerm t2)         {             return t2.getExponent() - t1.getExponent();         }              };     public int getExponent();      } public class Term implements ITerm {     private int coefficient, exponent;     public Term()     {         this(0, 0);     }          public Term(int coefficient, int exponent)     {         this.coefficient = coefficient;         this.exponent = exponent;     }          public String toString()     {         return "Term[coefficient=" + this.coefficient + ", exponent=" + this.exponent + "]";     }          @Override     public int getExponent()     {         return this.exponent;     }     public static void main(String[] argv)     {         java.util.PriorityQueue pq = new java.util.PriorityQueue(10, ITerm.SORT_BY_EXP_DESC);         pq.add(new Term(5, 2));         pq.add(new Term(5, 7));         pq.add(new Term(5, 5));         ITerm tCurrent = pq.poll();         while (tCurrent != null)         {             System.out.println(tCurrent);             tCurrent = pq.poll();         }     } }  ``` Unfortunately with the Java PriorityQueue, the only time you are guaranteed the order specified is during the pq.poll(). I just ran a few tests and found that neither a print of it, nor an iteration of it provided the proper ordering. Checking the api confirmed that this is normal behaviour in a priority queue for java. You can however sort them by converting to an array instead (according to the API).

 Bookmarks

 Thread Tools Rate This Thread Rate This Thread: 5 : Excellent 4 : Good 3 : Average 2 : Bad 1 : Terrible

 Posting Rules You may not post new threads You may not post replies You may not post attachments You may not edit your posts BB code is On Smilies are On [IMG] code is On HTML code is Off Forum Rules
 Forum Jump User Control Panel Private Messages Subscriptions Who's Online Search Forums Forums Home :: Client side development     JavaScript programming         DOM and JSON scripting         Ajax and Design         JavaScript frameworks         Post a JavaScript     HTML & CSS     XML     Flash & ActionScript         Adobe Flex     Graphics and Multimedia discussions     General web building         Site reviews         Building for mobile devices :: Server side development     Apache configuration     Perl/ CGI     PHP         Post a PHP snippet     MySQL         Other Databases     Ruby & Ruby On Rails     ASP     ASP.NET     Java and JSP     Other server side languages/ issues         ColdFusion         Python :: Computing & Sciences     Computer Programming     Computer/PC discussions     Geek News and Humour Web Projects and Services Marketplace     Web Projects         Small projects (quick fixes and changes)         Medium projects (new script, new features, etc)         Large Projects (new web application, complex features etc)         Unknown sized projects (request quote)         Vacant job positions         Looking for work/ for hire         Project collaboration/ partnership         Paid work offers and requests (Now CLOSED)     Career, job, and business ideas or advice     Domains, Sites, and Designs for sale         Domains for sale         Websites for sale         Design templates and graphics for sale :: Other forums     Member Offers     Forum feedback and announcements

All times are GMT +1. The time now is 11:19 AM.