In: Computer Science
Task 1: Class Term
The class Term encapsulates the coefficient and exponent of a single term. Exponents are limited in range from 0 to 99.
public class Term : IComparable
{
private double coefficient;
private integer exponent;
// Creates a term with the given coefficient and exponent
public Term (double coefficient, integer exponent)
{ … }
// Evaluates the current term at x
public double Evaluate (double x)
{ … }
// Returns -1, 0, or 1 if the exponent of the current term
// is less than, equal to, or greater than the exponent of obj
public int CompareTo (Object obj)
{ … }
// Returns a string representation of the current term
public override string ToString( )
{ … }
// Implement read and write properties for each data member
// The set property of exponent should throw an
// ArgumentOutOfRangeException if the exponent parameter
// of the constructor is less than 0 or greater than 99.
}
Task 2: Class Polynomial (Version 1)
The class Polynomial (Version 1) is implemented as a singly linked list of terms ordered by exponent. Each polynomial is also reduced to simplest terms, that is, only one term for a given exponent. The linked list implementation is supported by the generic Node class.
public class Node
{
Public T Item { get; set; }
Public Node Next; { get; set; }
public Node (T item, Node next)
{ … }
}
public class Polynomial
{
// A reference to the first node of a singly linked list
private Node front;
// Creates the polynomial 0
public Polynomial ( )
{ }
// Inserts term t into the current polynomial in its proper order
// If a term with the same exponent already exists then the two terms are added together
public void AddTerm (Term t)
{ … }
// Adds polynomials p and q to yield a new polynomial
public static Polynomial operator + (Polynomial p, Polynomial q)
{ … }
// Multiplies polynomials p and q to yield a new polynomial
public static Polynomial operator * (Polynomial p, Polynomial q)
{ … }
// Evaluates the current polynomial at x
public double Evaluate (double x)
{ … }
// Prints the current polynomial
public void Print ( )
{ … }
}
Task 3: Class Polynomial (Version 2,)
In a separate namespace, the class Polynomial (Version 2) re-implements a polynomial as a linear array of terms ordered by exponent. Each polynomial is also reduced to simplest terms, that is, only one term for a given exponent.
public class Polynomial
{
// P is a linear array of Terms
private Term[ ] P;
…
// Re-implement the six methods of Polynomial (including the constructor)
…
}
Please find code for Task1,Task2 in c#
using System;
namespace ConsoleApp1
{
    public class Term: IComparable<Term>
    {
        private double coefficient;
        private int exponent;
        // Creates a term with the given coefficient and exponent
        public Term(double coefficient, int exponent)
        {
            if(exponent>99 || exponent<0)
            {
                throw new ArgumentOutOfRangeException();
            }
            this.coefficient = coefficient;
            this.exponent = exponent;
        }
        // Evaluates the current term at x
        public double Evaluate(double x)
        {
            double res;
            res = coefficient * Math.Pow(x, exponent);
            return res;
        }
        // Returns a string representation of the current term
        public override string ToString()
        {
            string outp=String.Empty;
            outp = coefficient + "^" + exponent;
            return outp;
        }
        public int getExponent()   {  return this.exponent; }
        public double getCoefficient() { return this.coefficient; }
        public void setExponent(int exp) {
            if (exponent > 99 || exponent < 0)
            {
                throw new ArgumentOutOfRangeException();
            }
            this.exponent = exp;
        }
        public void setcoefficient(double coe) { this.coefficient = coe; }
        // Returns -1, 0, or 1 if the exponent of the current term
        // is less than, equal to, or greater than the exponent of obj
        public int CompareTo(Term obj)
        {
            int res = 0;
            if (this.exponent > obj.exponent)
                res = 1;
            else if (this.exponent < obj.exponent)
                res = -1;
            else
                res = 0;
            return res;
        }
        
    }
}
using System;
namespace ConsoleApp1
{
    public class Polynomial
    {
       // A reference to the first node of a singly linked list
        private Node front;
        // Creates the polynomial 0
        public Polynomial()
        {
            front = null;
        }
        // Inserts term t into the current polynomial in its proper order
        // If a term with the same exponent already exists then the two terms are added together
        public void AddTerm(Term t)
        {
            Node node= new Node(t, null); 
            Node temp,pre;
            if (front== null)
            {
               
                front = node;
            }
            else
            {
                temp = front;
                pre = null;
                while (temp.Next!= null && temp.Item.getExponent() >= t.getExponent())
                {
                    pre = temp;                   
                    temp = temp.Next;
                }
                if (temp.Item.getExponent() < t.getExponent())
                {
                    pre.Next = node;
                    node.Next = temp;
                }
                else if(temp.Item.getExponent() == t.getExponent())
                {
                    double sumcoe = temp.Item.getCoefficient() + t.getCoefficient();
                    temp.Item.setcoefficient(sumcoe);
                }
                else
                {
                    temp.Next = node;
                }
            }
        }
        // Adds polynomials p and q to yield a new polynomial
        public static Polynomial operator +(Polynomial p, Polynomial q)
        {
            Polynomial sum = new Polynomial();
            Node temp1 = p.front;
            Node temp2 = q.front;
            while (temp1 != null && temp2!=null)
            {
                if (temp1.Item.getExponent() == temp2.Item.getExponent())
                {
                    double sumcoe = temp1.Item.getCoefficient() + temp2.Item.getCoefficient();
                    sum.AddTerm(new Term(sumcoe, temp1.Item.getExponent()));
                }                    
                else
                {
                    sum.AddTerm(temp1.Item);
                    sum.AddTerm(temp2.Item);
                }
                   
                temp1 = temp1.Next;
                temp2 = temp2.Next;
            }
            return sum;
        }
        public static int length(Polynomial p)
        {
            Node temp1 = p.front;
            int count = 0;
            while (temp1 != null)
            {
                temp1 = temp1.Next;
                count++;
            }
            return count;
        }
        // Multiplies polynomials p and q to yield a new polynomial
        public static Polynomial operator *(Polynomial p, Polynomial q)
        {
            Polynomial mul = new Polynomial();
            return mul;
        }
        // Evaluates the current polynomial at x
        public double Evaluate(double x)
        {
            double res = 0;
            Node temp;
            temp = front;
          
            while (temp != null)
            {
                res = res + temp.Item.Evaluate(x);
                temp = temp.Next;
            }
            return res;
        }
        // Prints the current polynomial
        public void Print()
        {
            Node temp;
            temp = front;
            string output = String.Empty;
            while (temp != null)
            {
                if(temp.Item.getCoefficient()>0)
                 output += "+"+temp.Item.ToString();   
                else
                 output += "-"+temp.Item.ToString();
                temp = temp.Next;
            }
            Console.WriteLine(output);
            Console.ReadLine();
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            Polynomial p = new Polynomial();
            p.AddTerm(new Term(2, 5));
            p.AddTerm(new Term(5, 2));
            p.AddTerm(new Term(4, 1));
            p.AddTerm(new Term(4, 4));
            p.AddTerm(new Term(4, 3));
            p.Print();
            Polynomial q = new Polynomial();
            q.AddTerm(new Term(2, 5));
            q.AddTerm(new Term(5, 2));
            q.AddTerm(new Term(4, 1));
            q.AddTerm(new Term(4, 4));
            q.Print();
            Polynomial sum = p + q;
            sum.Print();
        }
    }
}
namespace ConsoleApp1
{
    public class Node
    {
        public Term Item { get; set; }
        public Node Next { get; set; }
        public Node(Term item, Node next)
        {
            this.Item = item;
            this.Next = next;
        }
    }
}