{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

Hw2Poly - A sample java"program which contains a Polynomial...

Info icon This preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
//** // A sample java "program" which contains a Polynomial class named // Hw2Poly, and a main() method that creates a few polynomial objects // and demonstrates insertion, deletion, isEqual, add, and print // operations. The program requires four other java classes, Term.java, // LinkedList.java, LinkedListItr.java, and ListNode.java, to compile. // // Date: 2/07/01 (revised as a partial solution key for homework #2) //** public class Hw2Poly // the polynomial class { private LinkedList lst; private int numTerms; private int degree; // a constructor which calls LinkedList's constructor and sets // other fields to default values, effectively creating // a zero polynomial Hw2Poly() { lst = new LinkedList(); numTerms = 0; degree = 0; } // the "access" methods for the private instance variables numTerms // and degree public LinkedList getList() { return lst; } public int getDegree() { return degree; } public int getNumTerms() { return numTerms; } public void setDegree(int d) { degree = d; } public void setNumTerms(int t) { numTerms = t; } //** // test if two polynomials have equal terms assuming both store the // non-zero terms in descending order of exponents //** public boolean isEqual(Hw2Poly pp) { if (numTerms != pp.getNumTerms()|| degree != pp.getDegree())
Image of page 1

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
return false; LinkedListItr itr1 = lst.first(); LinkedListItr itr2 = lst.first(); for (int i=0; i<numTerms; i++) { Term tm1 = (Term)(itr1.retrieve()); Term tm2 = (Term)(itr2.retrieve()); if(!tm1.equals(tm2)) return false; itr1.advance(); itr2.advance(); } return true; } //** // print the terms of a polynomial in a readable format //** public void printPoly() { if (lst.isEmpty())
Image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern