Polynomial

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Sep 2009
Posts: 6
Reputation: Hanyouslayer is an unknown quantity at this point 
Solved Threads: 0
Hanyouslayer Hanyouslayer is offline Offline
Newbie Poster

Polynomial

 
0
  #1
26 Days Ago
Ok, before everyone gets mad at me for not looking around the board for these topics, I have, but they weren't my problem. My problem I am having is calling the Polynomial class. I know how to call classes in a different class, but this is a bit different. I also realize that this is a homework assignment, and I don't want anyone to do my assignment for me. This is just a step that I am confused about. In addition, I can't just call the Polynomial class because I need to add two polynomials, then in another case subtract, and the other math-type stuff.
Thank you for looking at my thread, if you helped, thanks a lot, if not, thank you for your time to look.

This is my driver class, which all it does so far is basically the menu system.
  1. //Driver
  2. import java.io.*;
  3. import java.util.*;
  4. public class Project6
  5. {
  6. private static Scanner console = new Scanner(System.in);
  7. public static void main(String[] args)
  8.  
  9. {
  10. //3 instances of the class Polynomial
  11. Polynomial polyAdd = new Polynomial();
  12. Polynomial polySub = new Polynomial();
  13. Polynomial polyMul = new Polynomial();
  14.  
  15. //Call to menu
  16. menu();
  17. }
  18. public static void menu()
  19.  
  20. {
  21. //Decalre Variable to record the user's number entered to navigate menu
  22. int choice;
  23. String polynomial1;
  24. String polynomial2;
  25.  
  26. //Menu print
  27. System.out.println("1. Create a Polynomial from the keyboard.");
  28. System.out.println("2. Add two Polynomials and Display Result.");
  29. System.out.println("3. Subtract two Polynomials and Display Result.");
  30. System.out.println("4. Multiply two Polynomials and Display Result.");
  31. System.out.println("5. Terminate the program");
  32. System.out.println("");
  33.  
  34. //Take in choice variable to navigate the menu
  35. choice = console.nextInt();
  36. System.out.println("");
  37.  
  38. //Switch controlling the menu system
  39. switch (choice)
  40. {
  41. //Take in two polynomails
  42. case 1:
  43. System.out.println("Enter the first polynomial.");
  44. polynomial1 = console.next();
  45. System.out.println("Enter the second polynomial.");
  46. polynomial2 = console.next();
  47. System.out.println("");
  48. break;
  49.  
  50. //Add two Polynomials and Display result
  51. case 2:
  52. System.out.println("");
  53. break;
  54.  
  55. //Subtract two Polynomials and Display result
  56. case 3:
  57. System.out.println("");
  58. break;
  59.  
  60. //Multiply two Polynomials and Display result
  61. case 4:
  62.  
  63. System.out.println("");
  64. break;
  65.  
  66.  
  67. //Terminate the program
  68. case 5:
  69. System.exit(0);
  70. break;
  71.  
  72. //Default
  73. default:
  74. choice = console.nextInt();
  75. console.nextLine();
  76. }
  77. menu();
  78. }
  79.  
  80. }
This is a worker class, and I am trying to call it.
  1. import java.util.*;
  2.  
  3. public class Polynomial extends UnorderedArrayList<Double>
  4. {
  5. //Default constructor
  6. //Postcondition: An array of the size 100, and
  7. // length and maxSize are set to 100
  8. public Polynomial()
  9. {
  10. super();
  11. length = 100;
  12. }
  13.  
  14. //Constructor with a parameter
  15. //Postcondition: An array of the size specified by
  16. // the parameter size is created, length
  17. // and maxSize are initialized to size
  18. public Polynomial(int size)
  19. {
  20. super(size);
  21. length = size;
  22. }
  23.  
  24. //Method to evaluate a polynomial at a given value
  25. //Postcondition: The polynomial is evaluated at x and
  26. // the value is returned
  27. public double evaluate(Double x)
  28. {
  29. double value = 0.0;
  30.  
  31. Double coeff;
  32.  
  33. for (int i = 0; i < length; i++)
  34. {
  35. coeff = retrieveAt(i);
  36.  
  37. if (coeff != 0.0)
  38. value = value + coeff * Math.pow(x, i);
  39. }
  40.  
  41. return value;
  42. }
  43.  
  44. //Method to add two polynomials
  45. //Postcondition: This polynomial is added with the polynomial
  46. // specified by the parameter right. A
  47. // reference of the result is returned.
  48. public Polynomial add(Polynomial right)
  49. {
  50. int size = max(length, right.length);
  51. int i;
  52. Double sumCoeff;
  53.  
  54. Double coeffP;
  55. Double coeffQ;
  56.  
  57. Polynomial temp = new Polynomial(size);
  58.  
  59. for (i = 0; i < min(length, right.length); i++)
  60. {
  61. coeffP = retrieveAt(i);
  62. coeffQ = right.retrieveAt(i);
  63.  
  64. sumCoeff = coeffP + coeffQ;
  65. temp.replaceAt(i, sumCoeff);
  66. }
  67.  
  68. if (size == length)
  69. for (i = min(length, right.length); i < length; i++)
  70. temp.replaceAt(i, retrieveAt(i));
  71. else
  72. for (i = min(length, right.length);
  73. i < right.length; i++)
  74. temp.replaceAt(i, right.retrieveAt(i));
  75.  
  76. return temp;
  77. }
  78.  
  79. //Method to subtract two polynomials
  80. //Postcondition: The polynomial specified by the
  81. // parameter right is subtracted from this
  82. // polynomial. A reference of the result
  83. // is returned.
  84. public Polynomial subtract(Polynomial right)
  85. {
  86. int size = max(length, right.length);
  87. int i;
  88. Double diffCoeff;
  89.  
  90. Double coeffP;
  91. Double coeffQ;
  92.  
  93. Polynomial temp = new Polynomial(size);
  94.  
  95. for (i = 0; i < min(length, right.length); i++)
  96. {
  97. coeffP = retrieveAt(i);
  98. coeffQ = right.retrieveAt(i);
  99.  
  100. diffCoeff = coeffP - coeffQ;
  101. temp.replaceAt(i, diffCoeff);
  102. }
  103.  
  104. if (size == length)
  105. for (i = min(length, right.length); i < length; i++)
  106. temp.replaceAt(i, retrieveAt(i));
  107. else
  108. for (i = min(length, right.length);
  109. i < right.length; i++)
  110. {
  111. Double coeff;
  112. coeff = right.retrieveAt(i);
  113. coeff = -coeff;
  114. temp.replaceAt(i, coeff);
  115. }
  116.  
  117. return temp;
  118. }
  119.  
  120. //Method to multiply two polynomials
  121. //Postcondition: This polynomial is multiplied with the
  122. // polynomial specified by the parameter right.
  123. // A reference of the result is returned.
  124. public Polynomial multiply(Polynomial right)
  125. {
  126.  
  127. Polynomial temp = new Polynomial();
  128.  
  129. System.out.println("See Programming Exercise 5 at "
  130. + "the end of the chapter.");
  131.  
  132. return temp;
  133. }
  134.  
  135. //Method to read the coefficients of a polynomial
  136. public void read()
  137. {
  138. Scanner console = new Scanner(System.in);
  139.  
  140. Double x;
  141.  
  142. System.out.println("The degree of this polynomial is: "
  143. + (length - 1));
  144. for (int i = 0; i < length; i++)
  145. {
  146. System.out.print("Enter the coefficient of x^"
  147. + i + ": ");
  148.  
  149. x = console.nextDouble();
  150. replaceAt(i, x);
  151. }
  152. }
  153.  
  154. //Method to return the string containing the polynomial
  155. public String toString()
  156. {
  157. int i;
  158. int firstNonzeroCoeff = 0;
  159. Double coeff = 0.0;
  160. String str = "";
  161.  
  162. for (i = 0; i < length; i++)
  163. {
  164. coeff = retrieveAt(i);
  165.  
  166. if (coeff != 0.0)
  167. {
  168. firstNonzeroCoeff = i;
  169. break;
  170. }
  171. }
  172.  
  173. if (firstNonzeroCoeff < length)
  174. {
  175. if (firstNonzeroCoeff == 0)
  176. str = retrieveAt(firstNonzeroCoeff) + " ";
  177. else
  178. str = retrieveAt(firstNonzeroCoeff) + "x^"
  179. + firstNonzeroCoeff + " ";
  180.  
  181. for (i = firstNonzeroCoeff + 1; i < length; i++)
  182. {
  183. coeff = retrieveAt(i);
  184.  
  185. if (coeff != 0.0)
  186. if (coeff > 0.0)
  187. str += "+ " + coeff + "x^" + i + " ";
  188. else
  189. str += "- " + (-coeff) + "x^" + i + " ";
  190. }
  191. }
  192.  
  193. return str;
  194. }
  195.  
  196. //Method to determine the smaller of x and y
  197. //Postcondition: The smaller of x and y is returned
  198. public int min(int x, int y)
  199. {
  200. if (x <= y)
  201. return x;
  202. else
  203. return y;
  204. }
  205.  
  206. //Method to determine the larger of x and y
  207. //Postcondition: The larger of x and y is returned
  208. public int max(int x, int y)
  209. {
  210. if (x >= y)
  211. return x;
  212. else
  213. return y;
  214. }
  215. }
I included the next class because it was extended in my Polynomial worker class.
  1. public class UnorderedArrayList<T> extends ArrayListClass<T>
  2. {
  3. //Default constructor
  4. public UnorderedArrayList()
  5. {
  6. super();
  7. }
  8.  
  9. //Constructor with a parameter
  10. public UnorderedArrayList(int size)
  11. {
  12. super(size);
  13. }
  14.  
  15. //Method to determine whether searchItem is in the list.
  16. //Postcondition: If searchItem is found, returns the
  17. // location in the array where searchItem
  18. // is found; otherwise, returns -1.
  19. public int seqSearch(T searchItem)
  20. {
  21. int loc;
  22. boolean found = false;
  23.  
  24. for (loc = 0; loc < length; loc++)
  25. if (list[loc].equals(searchItem))
  26. {
  27. found = true;
  28. break;
  29. }
  30.  
  31. if (found)
  32. return loc;
  33. else
  34. return -1;
  35. } //end seqSearch
  36.  
  37. //Method to insert insertItem in the list at the position
  38. //specified by location.
  39. //Postcondition: Starting at location, the elements of
  40. // the list are shifted to make room for
  41. // insertItem, list[location] = insertItem;,
  42. // and length++;
  43. // If the list is full or location is out
  44. // of range, an appropriate message is
  45. // output.
  46. public void insertAt(int location, T insertItem)
  47. {
  48. if (location < 0 || location >= maxSize)
  49. System.err.println("The position of the item to "
  50. + "be inserted is out of range.");
  51. else
  52. if (length >= maxSize) //list is full
  53. System.err.println("Cannot insert in a full "
  54. + "list.");
  55. else
  56. {
  57. for (int i = length; i > location; i--)
  58. list[i] = list[i - 1]; //move the
  59. //elements down
  60.  
  61. list[location] = insertItem;
  62. length++; //increment the length
  63. }
  64. } //end insertAt
  65.  
  66. //Method to insert insertItem at the end of the list.
  67. //Postcondition: list[length] = insertItem; and length++;
  68. // If the list is full, an appropriate
  69. // message is output.
  70. public void insertEnd(T insertItem)
  71. {
  72. if (length >= maxSize) //the list is full
  73. System.err.println("Cannot insert in a full list.");
  74. else
  75. {
  76. list[length] = insertItem; //insert the
  77. //item at the end
  78. length++; //increment the length
  79. }
  80. } //end insertEnd
  81.  
  82.  
  83. //Method to replace the element in the list at
  84. //the position specified by location with repItem.
  85. //Postcondition: list[location] = repItem
  86. // If location is out of range, an
  87. // appropriate message is output.
  88. public void replaceAt(int location, T repItem)
  89. {
  90. if (location < 0 || location >= length)
  91. System.err.println("The location of the item to "
  92. + "be replaced is out of range.");
  93. else
  94. list[location] = repItem;
  95. } //end replaceAt
  96.  
  97. //Method to remove an item from the list.
  98. //The parameter removeItem specifies the item to
  99. //be removed.
  100. //Postcondition: If removeItem is found in the list, it
  101. // is removed from the list and length is
  102. // decremented by one.
  103. public void remove(T removeItem)
  104. {
  105. int loc;
  106.  
  107. if (length == 0)
  108. System.err.println("Cannot delete from an "
  109. + "empty list.");
  110. else
  111. {
  112. loc = seqSearch(removeItem);
  113.  
  114. if (loc != -1)
  115. removeAt(loc);
  116. else
  117. System.out.println("The item to be deleted "
  118. + "is not in the list.");
  119. }
  120. } //end remove
  121. }
I included this class because it was extended in my UnorderedArrayList class.
  1. public abstract class ArrayListClass<T>
  2. implements ArrayListADT<T>, Cloneable
  3. {
  4. protected int length; //to store the length of the list
  5. protected int maxSize; //to store the maximum size of
  6. //the list
  7. protected T[] list; //array to hold the list elements
  8.  
  9.  
  10. //Default constructor
  11. //Creates an array of size 100
  12. //Postcondition: list points to the array, length = 0,
  13. // and maxSize = 100
  14. public ArrayListClass()
  15. {
  16. maxSize = 100;
  17.  
  18. length = 0;
  19. list = (T[]) new Object[maxSize];
  20. }
  21.  
  22. //Constructor with a parameter
  23. //Creates an array of the size specified by the
  24. //parameter size.
  25. //Postcondition: list points to the array, length = 0,
  26. // and maxSize = size
  27. public ArrayListClass(int size)
  28. {
  29. if (size <= 0)
  30. {
  31. System.err.println("The array size must be positive. "
  32. + "Creating an array of size 100. ");
  33. maxSize = 100;
  34. }
  35. else
  36. maxSize = size;
  37.  
  38. length = 0;
  39. list = (T[]) new Object[maxSize];
  40. }
  41.  
  42. //Method to determine whether the list is empty.
  43. //Postcondition: Returns true if the list is empty;
  44. // otherwise, returns false.
  45. public boolean isEmpty()
  46. {
  47. return (length == 0);
  48. }
  49.  
  50. //Method to determine whether the list is full.
  51. //Postcondition: Returns true if the list is full;
  52. // otherwise, returns false.
  53. public boolean isFull()
  54. {
  55. return (length == maxSize);
  56. }
  57.  
  58. //Method to return the number of elements in the list.
  59. //Postcondition: Returns the value of length.
  60. public int listSize()
  61. {
  62. return length;
  63. }
  64.  
  65. //Method to return the maximum size of the list.
  66. //Postcondition: Returns the value of maxSize.
  67. public int maxListSize()
  68. {
  69. return maxSize;
  70. }
  71.  
  72. //Method to output the elements of the list.
  73. //Postcondition: Elements of the list are output on the
  74. //standard output device.
  75. public void print()
  76. {
  77. for (int i = 0; i < length; i++)
  78. System.out.println(list[i]);
  79. System.out.println();
  80. }
  81.  
  82. //Returns a copy of objects data in store.
  83. //This method clones only the references stored in
  84. //the array. The objects that the array references
  85. //point to are not cloned.
  86. public Object clone()
  87. {
  88. ArrayListClass<T> copy = null;
  89.  
  90. try
  91. {
  92. copy = (ArrayListClass<T>) super.clone();
  93. }
  94. catch (CloneNotSupportedException e)
  95. {
  96. return null;
  97. }
  98.  
  99. copy.list = (T[]) list.clone();
  100.  
  101. return copy;
  102. }
  103.  
  104. //Method to determine whether item is the same as the item
  105. //in the list at the position specified by location.
  106. //Postcondition: Returns true if list[location] is the
  107. // same as item; otherwise, returns false.
  108. public boolean isItemAtEqual(int location, T item)
  109. {
  110. if (location < 0 || location >= length)
  111. {
  112. System.err.println("The location of the item to "
  113. + "be compared is out of range.");
  114. return false;
  115. }
  116.  
  117. return (list[location].equals(item));
  118. }
  119.  
  120. //Method to remove all the elements from the list.
  121. //Postcondition: length = 0
  122. public void clearList()
  123. {
  124. for (int i = 0; i < length; i++)
  125. list[i] = null;
  126.  
  127. length = 0;
  128.  
  129. System.gc(); //invoke garbage collector
  130. } //end clearList
  131.  
  132. //Method to remove the item from the list at the position
  133. //specified by location.
  134. //Postcondition: The list element at list[location] is
  135. // removed and length is decremented by 1.
  136. // If location is out of range, an
  137. // appropriate message is output.
  138. public void removeAt(int location)
  139. {
  140. if (location < 0 || location >= length)
  141. System.err.println("The location of the item to "
  142. + "be removed is out of range.");
  143. else
  144. {
  145. for (int i = location; i < length - 1; i++)
  146. list[i] = list[i + 1];
  147.  
  148. list[length - 1] = null;
  149.  
  150. length--;
  151. }
  152. } //end removeAt
  153.  
  154. //Method to retrieve the element from the list at the
  155. //position specified by location.
  156. //Postcondition: A reference of the element at the
  157. // position specified by location is
  158. // returned. If location is out of range,
  159. // an appropriate message is output and
  160. // null is returned.
  161. public T retrieveAt(int location)
  162. {
  163. if (location < 0 || location >= length)
  164. {
  165. System.err.println("The location of the item to be "
  166. + "retrieved is out of range.");
  167. return null;
  168. }
  169. else
  170. return list[location];
  171. } //end retrieveAt
  172.  
  173.  
  174. //Method to insert insertItem in the list at the position
  175. //specified by location.
  176. //Postcondition: Starting at location, the elements of
  177. // the list are shifted to make room for
  178. // insertItem, list[location] = insertItem;,
  179. // and length++;
  180. // If the list is full or location is out
  181. // of range, an appropriate message is
  182. // output.
  183. public abstract void insertAt(int location, T insertItem);
  184.  
  185. //Method to insert insertItem at the end of the list.
  186. //Postcondition: list[length] = insertItem; and length++;
  187. // If the list is full, an appropriate
  188. // message is output.
  189. public abstract void insertEnd(T insertItem);
  190.  
  191. //Method to replace the element in the list at
  192. //the position specified by location with repItem.
  193. //Postcondition: list[location] = repItem
  194. // If location is out of range, an
  195. // appropriate message is output.
  196. public abstract void replaceAt(int location, T repItem);
  197.  
  198. //Method to determine whether searchItem is in the list.
  199. //Postcondition: If searchItem is found, returns the
  200. // location in the array where searchItem
  201. // is found; otherwise, returns -1.
  202. public abstract int seqSearch(T searchItem);
  203.  
  204. //Method to remove an item from the list.
  205. //The parameter removeItem specifies the item to
  206. //be removed.
  207. //Postcondition: If removeItem is found in the list, it
  208. // is removed from the list and length is
  209. // decremented by one.
  210. public abstract void remove(T removeItem);
  211. }
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 332
Reputation: quuba is on a distinguished road 
Solved Threads: 53
quuba quuba is offline Offline
Posting Whiz
 
0
  #2
26 Days Ago
  1. public static void main(String[] args) {
  2. Polynomial polyAdd = new Polynomial();
  3. System.out.println(polyAdd.toString());
  4. }
run:
Exception in thread "main" java.lang.NullPointerException
at Hanyouslayer.Polynomial.toString(Polynomial.java:162)
at Hanyouslayer.Polynomial.main(Polynomial.java:213)
Java Result: 1
Why it gives error, where are defaults...
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 6
Reputation: Hanyouslayer is an unknown quantity at this point 
Solved Threads: 0
Hanyouslayer Hanyouslayer is offline Offline
Newbie Poster
 
0
  #3
26 Days Ago
defaults as in in my switch statement?
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 332
Reputation: quuba is on a distinguished road 
Solved Threads: 53
quuba quuba is offline Offline
Posting Whiz
 
0
  #4
25 Days Ago
Default polynomial:
Look athttp://en.wikipedia.org/wiki/Polynomial#Classifications
The simplest polynomial is
The polynomial 0, which may be considered to have no terms at all, is called the zero polynomial.
I would like to see that Your default Polynomial() meet the minimum requirements to be a polynomial.
Like new Point() == new Point(0,0)
//
Menu structure:
1. Create a Polynomial from the keyboard.
- User should enter as many polynomials as he wishes (You know lists) , and displays them in order.
- User should indicate which pairs of polynomials and in what order subject to mathematical operations, result should be stored
- Bringing in of a fully polynomial with a keyboard, inside constructor.
Why not? Constructor displays own menu and gets all necessary parameters.
Reply With Quote Quick reply to this message  
Reply

Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC