Advertisement
Guest User

CS275 hw

a guest
Nov 12th, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.38 KB | None | 0 0
  1. /**
  2. * File Name: Polynomial.java
  3. * Email Address: domingu9@pnw.edu
  4. * Program: This program implements a polynomial class using a LinkedList defined in Java
  5. * @author Enrique Dominguez
  6. * @version 11/13/18
  7. */
  8.  
  9.  
  10. import java.util.LinkedList;
  11. import java.util.ListIterator;
  12.  
  13. class TermNode{
  14. private double coefficient;
  15. private int exponent;
  16. private TermNode next;
  17.  
  18. public TermNode(int exp, double coeff, TermNode nextTerm )
  19. {
  20. coefficient= coeff;
  21. exponent = exp;
  22. next = nextTerm;
  23. }
  24.  
  25. //
  26. public double getCoefficient()
  27. {
  28. return coefficient;
  29. }
  30.  
  31. public int getExponent()
  32. {
  33. //
  34. return 0;
  35. }
  36.  
  37. public TermNode getNext()
  38. {
  39.  
  40. return null;
  41. }
  42. public void setCoefficient(double newCoeff)
  43. {
  44.  
  45. }
  46.  
  47. public int setExponent()
  48. {
  49. return 0;
  50. }
  51.  
  52. public void setNext(TermNoode newNext)
  53. {
  54.  
  55. }
  56.  
  57. // other methos.
  58. }
  59.  
  60. public class Polynomial {
  61.  
  62.  
  63. // instance variables of Polynomial
  64. // first is the term of the Polynomial with the highest degree
  65. // except the constant term, only non-zero terms are stored.
  66. private TermNode first;
  67.  
  68.  
  69. /**Postcondition: Creates a polynomial which is 0.
  70. * **/
  71. public Polynomial() {
  72. first = new TermNode(0,0, null);
  73. }
  74.  
  75. /**Postcondition: Creates a polynomial which has a single term a0*x^0
  76. * @param a0 The value to be set as the coefficient of the constant (x^0) term.
  77. * **/
  78.  
  79. public Polynomial(double a0) {
  80. first = new TermNode(0,a0,null);
  81. }
  82.  
  83. /** Postcondition: Creates a copy of Polynomial p
  84. * @param p the Polynomial which is to be copied.
  85. * **/
  86.  
  87. public Polynomial(Polynomial p) {
  88. TermNode copyHead;
  89. TermNode copyTail;
  90. copyHead = new TermNode(p.first.coefficient, p.first.exponent, null);
  91. copyTail = copyHead;
  92. while (p.first.next != null) {
  93. p.first = p.first.next;
  94. copyTail = new TermNode(p.first.coefficient, p.first.exponent, p.first.next);
  95. copyTail = copyTail.next;
  96. }
  97. first = copyHead;
  98. }
  99.  
  100. /** Postcondition: Adds the given amount to the coefficient of the specified exponent.
  101. * @param amount The amount to be added to the coefficient.
  102. * @param exponent The degree of the term whose coefficient is to be modified.
  103. * (1) Note that the exponent can be arbitrary
  104. * (2) If you want, you can assume the amount is not 0, however, it is possible that
  105. * after you add the amount, the coefficient becomes 0, in which case, you should delete the TermNode
  106. * **/
  107.  
  108. public void add_to_coef(double amount, int exponent) {
  109. if (amount == 0)
  110. {
  111. return;
  112. }
  113. if (exponent > first.exponent)
  114. {
  115. first = new TermNode(exponent, amount, first);
  116. }else if (exponent == first.exponent){
  117. first.coefficient += amount;
  118. }else {
  119. TermNode cur = first;
  120. while (cur != null)
  121. {
  122. cur = cur.next;
  123. if (exponent > cur.exponent)
  124. {
  125. first.next = new
  126. TermNode(exponent, amount, first.next);
  127. break;
  128. }else{
  129. cur = cur.next;
  130. }
  131. }
  132. }
  133. }
  134.  
  135. /** Postcondition: Sets the coefficient of a specified term to a specified value.
  136. * @param coefficient The new value of the coefficient.
  137. * @param exponent The degree of the term whose coefficient is to be modified.
  138. * (1) Note that the exponent can be arbitrary
  139. * (2) The coefficient may be 0
  140. * **/
  141.  
  142. public void assign_coef(double coefficient, int exponent) {
  143.  
  144.  
  145.  
  146.  
  147. }
  148.  
  149. /** Postcondition: Returns coefficient at specified exponent of this polynomial.
  150. * @param exponent The exponent of the term whose coefficient is sought.
  151. * @return The coefficient of the term.
  152. * @throws Exception if the degree of the activating polynomial is less than that of the requested term.
  153. * **/
  154.  
  155. public double coefficient(int exponent) {
  156. if (exponent > this.function.getExponent()) {
  157. throw new IllegalArgumentException("Coefficient is greater than teh highest exponent within polynomial");
  158. } else {
  159. if (this.function.getExponent() == exponent) {
  160. return this.function.getCoefficient();
  161. } else {
  162. TermNode index = TermNode.expSearch(this.function, exponent);
  163. if (index != null) {
  164. return index.getCoefficient();
  165. } else {
  166. return 0;
  167. }
  168. }
  169. }
  170. }
  171.  
  172. /** @return The value of this Polynomial with the given value for the variable x.
  173. * @param x The value at which the Polynomial is to be evaluated.
  174. * using Horner's method to evaluation
  175. * see the link here
  176. * https://en.wikipedia.org/wiki/Horner%27s_method
  177. *
  178. ***/
  179.  
  180. public double eval(double x) {
  181. TermNode t = first;
  182. double sum = t.coefficient;
  183.  
  184. int curExponent = t.exponent;
  185. while(t.next!=null)
  186. {
  187.  
  188.  
  189.  
  190.  
  191. }
  192.  
  193. return sum;
  194. }
  195.  
  196.  
  197.  
  198. /**@return Returns a string representing the polynomial expression with coefficients displayed to the tenths place,
  199. * omitting any coefficients that are zero.
  200. * If all coefficients are 0, then the zero function is reported.
  201. *
  202. **/
  203. public String toString() {
  204.  
  205. return null;
  206. }
  207.  
  208. /**@return Returns a Polynomial that is the sum of p and this Polynomial.
  209. * @param p The Polynomial to be added to the activating Polynomial.
  210. * **/
  211. public Polynomial add(Polynomial p) {
  212.  
  213.  
  214. return p;
  215. }
  216.  
  217.  
  218.  
  219.  
  220. /** Postcondition: Returns a new polynomial obtained by multiplying this term and p. For example, if this polynomial is
  221. 2x^2 + 3x + 4 and p is 5x^2 - 1x + 7, then at the end of this function, it will return the polynomial 10x^4 + 13x^3 + 31x^2 + 17x + 28.
  222. @param p The polynomial to be multiplied.
  223. @return The product of the activating Polynomial and p.
  224. **/
  225.  
  226. public Polynomial multiply(Polynomial p) {
  227.  
  228. return p;
  229. }
  230. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement