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)
…
}
Task 4:
Class Polynomials
The class Polynomials is a collection of polynomials of either implementation stored in an instance of the generic library class List.
class Polynomials
{
private List L;
// Creates an empty list L of polynomials
public Polynomials ( )
{ … }
// Retrieves the polynomial stored at position i in L
public Polynomial Retrieve (int i)
{ … }
// Inserts polynomial p into L
public void Insert (Polynomial p)
{ … }
// Deletes the polynomial at index i
public void Delete (int i)
{ … }
// Returns the number of polynomials in L
public int Size ( )
{ … }
// Prints out the list of polynomials
public void Print ( )
{ … }
}
Task 5:
The class Program includes the static Main method which serves as the entry point of execution.
class Program
{
static void Main (string[] args)
{
Polynomial S; // Collection of polynomials
…
}
}
The main program provides the user interface to manipulates the collection S of polynomials, namely:
1) To create a polynomial and insert it into S (no index is needed),
2) To add two polynomials from S (retrieved by index) and to insert the resultant polynomial into S,
3) To multiply two polynomials from S (retrieved by index) and to insert the resultant polynomial into S,
4) To delete the polynomial from S at a given index, and
5) To evaluate the polynomial from S at a given index.
To aid the user, the collection of polynomials is output before each operation. The polynomials are numbered (indexed) starting at 0.
Indices should be checked before an add, multiply, delete or evaluate is performed.
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;
}
}
}
The class Polynomials is a collection of polynomials of either
implementation stored in an instance of the
generic library class List.
class Polynomials
{
private List L;
// Creates an empty list L of polynomials
public Polynomials ( )
{ … } (1 mark)
// Retrieves the polynomial stored at position i in L
public Polynomial Retrieve (int i) (1 mark)
{ … }
// Inserts polynomial p into L
public void Insert (Polynomial p) (1 mark)
{ … }
// Deletes the polynomial at index i
public void Delete (int i) (1 mark)
{ … }
// Returns the number of polynomials in L
public int Size ( )
{ … } (1 mark)
// Prints out the list of polynomials
public void Print ( )
{ … } (2 marks)
Please use C# to answer
Expert Answer
1.
Creates an empty list L of polynomials
public polynomials()
{
//creation of empty list of Polynomials
List<Polynomial> list=new List<Polynomial>();
(Or) the provided List l
//we can use Any() to check wheather the list is empty or not
bool check=!l.Any();
if(check)
{
Console.WriteLine("the provided list l is empty");
} else
{
Console.WriteLine("The provided list is not empty");
}
2.
Retrieves the polynomial stored at position i in L
//Can be Done by using Item property
public Polynomial Retrieve (int i) (1 mark)
{
l.Item[i];
}
i.e l.item[2] (it will display polynomial which is storred at 2 location)
3.
// Inserts polynomial p into L
public void Insert (Polynomial p) (1 mark)
{
l.Add(p);
}
4.
// Deletes the polynomial at index i
public void Delete (int i) (1 mark)
{
l.RemoveAt(i)0;//which will remove at index i
}
5.
// Returns the number of polynomials in L
public int Size ( )
{
int count=l.Count; // It will provide number of polynomials of the list
return count;
}
6.
// Prints out the list of polynomials
public void Print ( )
{
foreach(Polynomial polynomial in l)
{
Console.Write(polynomial);
}
}