Question

In: Computer Science

Write a program that adds and subtracts two polynomials. It creates an array of nodes and...

Write a program that adds and subtracts two polynomials. It creates an array of nodes and connects them into the freeStore. This implementation uses one array to store multiple array to store multiple polynomial instances and the free store. I need help to finish the LinkedListInArrayPolynomial class.

Output should look like below:

Forth test is linked list of terms in an array.
linkInArray1 = 3x^11+4x^10+4x^4
linkInArray2 = 4x^19+5x^14-3x^12-78
sum of linkInArray1 and linkInArray2 = 4x^19+5x^14-3x^12+3x^11+4x^10+4x^4-78
linkInArray1 minus linkInArray2 = -4x^19-5x^14+3x^12+3x^11+4x^10+4x^4+78

The classes done are given below:

public interface PolynomialInterface {

PolynomialInterface add(PolynomialInterface other); // Effect: Adds value to owner of addPolynomial method.// Postcondition: Return value = this + value.

   PolynomialInterface subtract( PolynomialInterface other);

   // Effect: Subtracts value from owner of addPolynomial method. // Postcondition: Return value = this - value. void readPolynomial(); // Postcondition: polynomial read.

   String toString(); // Postcondition: polynomial converted to string. } }

public class PlynomialDemo {
   public static void main(String[] args) {
       // example strings constructor must handle
       // String s = "44";
       // String s = "44x";
       // String s = "4x^4+3x^3-3";
       // String s = "4x^3-3x^11";
       // String s = "44x^6-3x^10+4x^4";
       // String s = "25x^5-3x^13+4x^12-78";
       // String s ="34x^15-44x^14-3x^12+4x^31-78";
       // String s1 = "44";
       // String s2 = "44x-78";
       // String s1 = "4x^4+3x^3-3";
       // String s2 = "4x^6-3x^12";
//       String s1 = "4x^14-3x^12+4x^4+78";
//       String s2 = "-4x^4-3x^12+4x^17-78";
       String s1 = "4x^4+3x^11+4x^10";
       String s2 = "5x^14-3x^12+4x^19-78";
//       String s1 = "4x^5+4x^4-3x^12-4x^41-78";
//       String s2 = "-4x^4+3x^12+4x^41+78";
       System.out.println();
       PolynomialInterface linkInArray1 = new LinkedListInArrayPolynomial(s1);
       PolynomialInterface linkInArray2 = new LinkedListInArrayPolynomial(s2);
       PolynomialInterface linkInArray3 = new LinkedListInArrayPolynomial();
       System.out.println("Forth test is linked list of terms in an array.");
       // System.out.println("linkInArray1 string is " + s1);
       System.out.println("linkInArray1 = " + linkInArray1);
       // System.out.println("linkInArray2 string is " + s2);
       System.out.println("linkInArray2 = " + linkInArray2);
       linkInArray3 = linkInArray1.add(linkInArray2);
       System.out.println("sum of linkInArray1 and linkInArray2 = " + linkInArray3);
       linkInArray3 = linkInArray1.subtract(linkInArray2);
       System.out.println("linkInArray1 minus linkInArray2 = " + linkInArray3); } }

public class LLInArrayNode implements Comparable {
   private int NUL = -1;
   private String value;
   private int next;

   public LLInArrayNode() {
   }

   public LLInArrayNode(LLInArrayNode node) {
       this.value = node.value;
       this.next = node.next;
   }

   public LLInArrayNode(String value) {
       this.value = value;
       this.next = NUL;
   }

   public LLInArrayNode(String value, int next) {
       this.value = value;
       this.next = next;
   }

   public int compareTo(LLInArrayNode other) {
//calls the String compareToIgnoreCase() method
       return this.value.compareToIgnoreCase(other.value);
   }

   public String getValue() {
       return value;
   }

   public void setValue(String value) {
       this.value = value;
   }

   public int getNext() {
       return next;
   }

   public void setNext(int next) {
       this.next = next;
   }

   public String toString() {
       return "Value = " + value + " " + " next " + next;
   }
}

public class LinkedListInArrayPolynomial implements PolynomialInterface {
   private static final int NUL = -1;
   private static int free; // *** Reference to the first node on the free list
   private static LLInArrayNode[] nodeArray = new LLInArrayNode[1000];
   private static boolean nodeArrayIsInitialized = false;
   private String polyString;
   private int polynomial = NUL; // *** Reference to the first node on the list
   public LinkedListInArrayPolynomial(){
       if (!nodeArrayIsInitialized)
           initializeStaticArray();
   }
   public LinkedListInArrayPolynomial(String polyString){
       if (!nodeArrayIsInitialized)
           initializeStaticArray();
       this.polyString(polyString);
       storePolynomial();
   }
   private void initializeStaticArray(){
       // fill array with nodes
       for (int x = 0; x < nodeArray.length; x++){
           nodeArray[x] = new LLInArrayNode();
       }
       for (int index = 1; index < nodeArray.length; index++){
           nodeArray[index - 1].setNext(index);
       }
       nodeArray[nodeArray.length - 1].setNext(NUL);
       free = 0;
       nodeArrayIsInitialized = true;
   }
   protected int getNode()
   // *** Returns the index of the next available node from the freeStore
   // *** and updates the freeStore index.
   {
       int hold;
       hold = free;
       free = nodeArray[free].getNext();
       return hold;
   }
   @Override
   public PolynomialInterface add(PolynomialInterface other) {
       // TODO Auto-generated method stub
       return null;
   }

   @Override
   public PolynomialInterface subtract(PolynomialInterface other) {
       // TODO Auto-generated method stub
       return null;
   }
}

Solutions

Expert Solution

Check it. I hope it could work for you. give feedback please.

public interface PolynomialInterface

{

PolynomialInterface add(PolynomialInterface other);

// Effect: Adds value to owner of addPolynomial method.

// Postcondition: Return value = this + value.

PolynomialInterface subtract(PolynomialInterface other);

// Effect: Subtracts value from owner of addPolynomial method.

// Postcondition: Return value = this - value. void readPolynomial();

// Postcondition: polynomial read.

String toString();

// Postcondition: polynomial converted to string.

}

#### Imp...#### It should Create and array or ArrayList of nodes, each node holding a term of the polynomial

Your code must use the Demo provided below.

public class PlynomialDemo
{
        public static void main(String[] args)
        {
                // example strings constructor must handle
                //      String s = "44";
                //      String s = "44x";
                //      String s = "4x^4+3x^3-3";
                //      String s = "4x^3-3x^11";
                //      String s = "44x^6-3x^10+4x^4";
                //      String s = "25x^5-3x^13+4x^12-78";
                //      String s ="34x^15-44x^14-3x^12+4x^31-78";
                //      String s1 = "44";
                //      String s2 = "44x-78";
                //      String s1 = "4x^4+3x^3-3";
                //      String s2 = "4x^6-3x^12";
                String s1 = "4x^14-3x^12+4x^4+78";
                String s2 = "-4x^4-3x^12+4x^17-78";
                //      String s1 = "4x^4+3x^11+4x^10";
                //      String s2 = "5x^14-3x^12+4x^19-78";
                //  String s1 = "4x^5+4x^4-3x^12-4x^41-78";
                //  String s2 = "-4x^4+3x^12+4x^41+78";


                
PolynomialInterface sortA1 = new ArraySortedPolynomial(s1);
                PolynomialInterface sortA2 = new ArraySortedPolynomial(s2);
                PolynomialInterface sortA3; 
                sortA3 = sortA1.add(sortA2);
                System.out.println("Second test is sorted array of terms.");
                //      System.out.println("sortA1 string is           " + s1);
                System.out.println("sortA1 =                   " + sortA1);
                //      System.out.println("sortA2 string is           " + s2);
                System.out.println("sortA2 =                   " + sortA2);
                System.out.println("sortA3 = sortA1.add(sortA2)      " + sortA3);
                sortA3 = sortA1.subtract(sortA2);
                //      System.out.println("sortA1 string is           " + s1);
                //System.out.println("sortA1 =                   " + sortA1);
                //      System.out.println("sortA2 string is           " + s2);
                //System.out.println("sortA2 =                   " + sortA2);
                System.out.println("sortA3 = sortA1.subtract(sortA2) " + sortA3);
                System.out.println();
       }
}

Related Solutions

JAVA - Write a program that creates an ArrayList and adds an Account object, a Date...
JAVA - Write a program that creates an ArrayList and adds an Account object, a Date object, a ClockWithAudio object, a BMI object, a Day object, and a FigurePane object. Then display all elements in the list. Assume that all classes (i.e. Date, Account, etc.) have their own no-argument constructor.
Write a program that creates a two-dimensional array initialized with test data. The program should have...
Write a program that creates a two-dimensional array initialized with test data. The program should have the following functions: Hi There I really appreciate your help with this project. ▪ getTotal . This function should accept a two-dimensional array as its argument and return the total of all the values in the array. ▪ getAverage . This function should accept a two-dimensional array as its argument and return the average of all the values in the array. ▪ getRowTotal ....
java please Write a program that creates an ArrayList and adds 5 circle objects to the...
java please Write a program that creates an ArrayList and adds 5 circle objects to the list , and display all elements in the list by invoking the object’s toString() method.
IN JAVA write a program that creates an array of strings with 8 people in it....
IN JAVA write a program that creates an array of strings with 8 people in it. Second,  Assign a random rank between 1 to 8 to each of the players. The rankings do not change throughout the tournament. Finally, Sort the players based on the rankings and print the data (show rankings of players, in square brackets, at every step after they are ranked). USING JAVA COLLECTIONS IS NOT ALLOWED
1. a. In C++, Write a program that creates an array of 20 integers and initializes...
1. a. In C++, Write a program that creates an array of 20 integers and initializes it with the even values starting from 200. i.e. 200, 202, 204…. b. Write the elements of the array to the file even.txt, each element on a separate line. Try to use a single for loop for initializing the array and writing to file. You will need a separate counter for initializing the array. 2. a. Write another program that opens the file even.txt...
Write a program that creates a two-dimensional array initialized with test data. Use any primitive data...
Write a program that creates a two-dimensional array initialized with test data. Use any primitive data type that you wish. The program should have the following methods: -getTotal. This method should accept a two-dimensional array as its argument and return the total of all the values in the array. -getAverage. This method should accept a two-dimensional array as its argument and return the average of all the values in the array. -getRowTotal. This method should accept a two-dimensional array as...
Write a program that creates a two-dimensional array initialized with test data. Use any primitive data...
Write a program that creates a two-dimensional array initialized with test data. Use any primitive data type that you wish. The program should have the following methods: fillRandom. Accepts a reference to a two-dimensional array and fills it with random integers from 0 to 99 formatPrint. This method should accept a two-dimensional array and print it out row by row getTotal. This method should accept a two-dimensional array as its argument and return the total of all the values in...
Write a Java program that creates an array with 20 random numbers between 1 and 100,...
Write a Java program that creates an array with 20 random numbers between 1 and 100, and passes the array to functions in order to print the array, print the array in reverse order, find the maximum element of the array, and find the minimum element of the array. The prototype of the methods: public static void printArray(int arr[]) public static void printArrayReverse(int arr[]) public static int searchMax(int arr[]) public static int searchMin(int arr[]) Sample output: Random Array: [17 67...
USING MatLab (Arrays) Set ASIZE to 5. Write a program that creates an array of ASIZE...
USING MatLab (Arrays) Set ASIZE to 5. Write a program that creates an array of ASIZE numeric elements. Prompt the User for ASIZE numbers and store them in the array. After storing the values, calculate the sum of all the values in the array and display the sum. Modify the program you wrote for Problem 5 such that it calculates the product of all the values instead of the sum. Modify the program you wrote in Problem 6 such that...
use c++ 1 a)Write a console program that creates an array of size 100 integers. Then...
use c++ 1 a)Write a console program that creates an array of size 100 integers. Then use Fibonacci function Fib(n) to fill up the array with Fib(n) for n = 3 to n = 103: So this means the array looks like: { Fib(3), Fib(4), Fib(5), ...., Fib[102) }. For this part of the assignment, you should first write a recursive Fib(n) function. .For testing, print out the 100 integers. b) For the second part of this assignment, you must...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT