Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Design, implement and test an object-oriented Java program for processing single

ID: 638464 • Letter: D

Question

Design, implement and test an object-oriented Java program for processing single-variable polynomials represented as lists. Consider polynomials with integer coefficients and positive integer exponents. Implement the operations of addition and subtraction of two polynomials. Read the polynomials from an input file which should be either supplied as command line parameter or the user should be invited to specify it. A polynomial is represented in the input file by its name, the equal sign, and a set of pairs of integers, each pair consisting of the exponent and its corresponding coefficient (see the Examples section below). For example, P1 = 3 5 1 -4 0 8 represents the polynomial 5X^3 -4X + 8.

After reading the polynomials, they will be displayed on the screen in human readable form (see the required format in the Examples section below). A polynomial will always be written from the highest exponent to the lowest. A term having coefficient value zero will be omitted. The operations on polynomials will be entered by the user from the console (see the Examples section below). After executing the required operation: (i) the program should store the new generated polynomial (to be further used in other operations in the same execution session), (ii) the result will be shown at the console in human readable form and (iii) the user will be allowed to specify another operation or to exit the program.

2. Examples

Example of input file content:

P1 = 3 5 1 -4 0 8

P2 = 5 6 2 -2 1 7 0 -4

Alpha = 3 -4 2 7 0 -3

Example of the console output in human readable form:

P1 = 5X^3

Explanation / Answer

Program Code:

//Headers file section
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.AbstractList;
import java.util.ArrayList;
import java.util.List;
import java.util.ListIterator;
//create a class "polyTest"
class polyTest
{
String polyName;
List expo;
List cff;
}
//main class
public class PolynomialsDemo
{
//main method
public static void main(String[] args)
{
  //Create buffer reader for input
  
  String filename;
  int i;
  List p = new ArrayList();
  String[] values;
  BufferedReader br = null,in = null;
  String name1,name2,operation;
  //Prompt and read input file name
  System.out.print("Please enter the file name: ");
  try
  {
   in = new BufferedReader(new InputStreamReader(System.in));
   filename = in.readLine();
   String sCurrentLine;
   br = new BufferedReader(new FileReader(filename));
   while ((sCurrentLine = br.readLine()) != null)
   {
    values = sCurrentLine.split(" ");
    polyTest pt1 = new polyTest();
    pt1.polyName = values[0];
    pt1.expo = new ArrayList();
    pt1.cff = new ArrayList();
    for(i=2;i<values.length;i=i+2)
    {
     pt1.expo.add(Integer.parseInt(values[i]));
     pt1.cff.add(Integer.parseInt(values[i+1]));
    }
    p.add(pt1);
   }
   //Prompt and read input
   while(true)
   {
    polyTest pt1 = null,pt2=null;
    polyTest pt3 = new polyTest();
    System.out.print("Please enter the first polynomial operand: ");
    name1 = in.readLine();
    System.out.print("Please enter the second polynomial operand: ");
    name2 = in.readLine();
    System.out.print("Please enter the operation: ");
    operation = in.readLine();
    System.out.print("Please enter the name of the new polyTest: ");
   
    pt3.polyName = in.readLine();
    pt3.expo = new ArrayList();
    pt3.cff = new ArrayList();
    System.out.println("Executing the operation ...");
    ListIterator iter = p.listIterator();
    while(iter.hasNext())
    {
     polyTest temp = (polyTest)iter.next();
     if(temp.polyName.equalsIgnoreCase(name1))
     {
      pt1 = temp;
     }
     if(temp.polyName.equalsIgnoreCase(name2))
     {
      pt2 = temp;
     }
    }
    if(operation.equals("+"))
    {
     int j=0;
     List e1 = pt1.expo;
     List e2 = pt2.expo;
     List c1 = pt1.cff;
     List c2 = pt2.cff;
     for(i=0;i<e1.size() && j<e2.size();)
     {
      if(((int)e1.get(i)) > ((int)e2.get(j)))
      {
       pt3.expo.add(e1.get(i));
       pt3.cff.add(((int)c1.get(i)));
       i++;
      }
      else
      {
       if(((int)e1.get(i)) < ((int)e2.get(j)))
       {
        pt3.expo.add(e2.get(j));
        pt3.cff.add(((int)c2.get(j)));
        j++;
       }
       else
       {
        if(e1.get(i) == e2.get(j))
        {
         if(((int)c1.get(i))+ ((int)c2.get(j)) != 0)
         {
          pt3.expo.add(e1.get(i));
          pt3.cff.add(((int)c1.get(i))+ ((int)c2.get(j)));
         }
         j++;
         i++;
        }
       }
      }
     }
     while(i<e1.size())
     {
      pt3.expo.add(e1.get(i));
      pt3.cff.add(((int)c1.get(i)));
      i++;
     }
     while(j<e2.size())
     {
      pt3.expo.add(e2.get(j));
      pt3.cff.add(((int)c2.get(j)));
      j++;
     }
     iter = p.listIterator();
     i=0;
     while(iter.hasNext())
     {
      polyTest temp = (polyTest)iter.next();
      if(temp.polyName.equalsIgnoreCase(pt3.polyName))
      {
       p.remove(i);
       break;
      }
      i++;
     }
     p.add(pt3);
    }
    if(operation.equals("-"))
    {
     int j=0;
     List e1 = pt1.expo;
     List e2 = pt2.expo;
     List c1 = pt1.cff;
     List c2 = pt2.cff;
     for(i=0;i<e1.size() && j<e2.size();)
     {
      if(((int)e1.get(i)) > ((int)e2.get(j)))

{

pt3.expo.add(e1.get(i));

pt3.cff.add(((int)c1.get(i)));

i++;

}

else

{

if(((int)e1.get(i)) < ((int)e2.get(j)))

{

pt3.expo.add(e2.get(j));

pt3.cff.add(((int)c2.get(j))*-1);

j++;

}

else

{

if(e1.get(i) == e2.get(j))

{

pt3.expo.add(e1.get(i));

pt3.cff.add(((int)c1.get(i)) - ((int)c2.get(j)));

j++;

i++;

}

}

}

}

while(i<e1.size())

{

pt3.expo.add(e1.get(i));

pt3.cff.add(((int)c1.get(i)));

i++;

}

while(j<e2.size())

{

pt3.expo.add(e2.get(j));

pt3.cff.add(((int)c2.get(j)));

j++;

}

iter = p.listIterator();

i=0;

while(iter.hasNext())

{

polyTest temp = (polyTest)iter.next();

if(temp.polyName.equals(pt3.polyName))

{

p.remove(i);

break;

}

i++;

}

p.add(pt3);

}

System.out.print(pt3.polyName + " = ");

for(i=0;i<pt3.expo.size();i++)

{

if(((int)pt3.expo.get(i)) == 1)

System.out.print(pt3.cff.get(i) + "X" + " ");

else

{

if(((int)pt3.expo.get(i)) == 0)

System.out.print(pt3.cff.get(i) + " ");

else

System.out.print(pt3.cff.get(i) + "X^"+pt3.expo.get(i) + " ");

}

}

System.out.println();

while(true)

{

System.out.println("Do you want to continue (Y or N)?");

sCurrentLine = in.readLine();

if(sCurrentLine.equalsIgnoreCase("Y") || sCurrentLine.equalsIgnoreCase("y"))

break;

}
if(sCurrentLine.equalsIgnoreCase("N"))

break;

}

} catch (IOException e) {

e.printStackTrace();

} finally {

try {

if (br != null)br.close();

} catch (IOException ex) {

ex.printStackTrace();

}
}
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote