Hw2Poly - /* / A sample java "program" which...

Info iconThis 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())
Background image of page 1

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

View Full DocumentRight 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()) { System.out.println("0 (the zero polynomial)");
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 6

Hw2Poly - /* / A sample java &quot;program&quot; which...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online