Question

In: Computer Science

Task 1: Class Term The class Term encapsulates the coefficient and exponent of a single term....

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)

}

Solutions

Expert Solution

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;
        }
    }
}

Related Solutions

Task 1: Class Term The class Term encapsulates the coefficient and exponent of a single term....
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...
Write a class (and a client class to test it) that encapsulates a tic-tac-toe board. A...
Write a class (and a client class to test it) that encapsulates a tic-tac-toe board. A tic-tac-toe board looks like a table of three rows and three columns partially or completely filled with the characters X and O. At any point, a cell of that table could be empty or could contain an X or an O. You should have one instance variable, a two-dimensional array of values representing the tic-tac-toe board. This game should involve one human player vs....
Write a class (and a client class to test it) that encapsulates a tic-tac-toe board. A...
Write a class (and a client class to test it) that encapsulates a tic-tac-toe board. A tic-tac-toe board looks like a table of three rows and three columns partially or completely filled with the characters X and O. At any point, a cell of that table could be empty or could contain an X or an O. You should have one instance variable, a two-dimensional array of values representing the tic-tac-toe board. This game should involve one human player vs....
Given the variable as coefficient, exponent and input. Write methods that will convert the input into...
Given the variable as coefficient, exponent and input. Write methods that will convert the input into String in the following format: coefficient = 1, exponent=1 input=-3 Method 1 does not take any input, (just coefficient and exponent only) so it should return output as: "1.0 * x^1" (with inverted comma) Method 2 takes given input, so it should return output as: "1.0 * -3.0^1" (with inverted comma) ( Note coefficient 1 and exponent 3 are now 1.0 and 3.0 in...
The strength coefficient (K) and the work hardening exponent (n) for a stainless steel were determined...
The strength coefficient (K) and the work hardening exponent (n) for a stainless steel were determined during the analysis of the data from a tensile test using a standard tensile specimen. The flow curve parameters for the stainless steel are K= 1250 MPa and n = 0.4. A compression test is performed on a specimen of the stainless steel. If the starting height and diameter of the specimen are 80 mm and 18 mm, respectively and the final height of...
A polynomial can be represented using linked list, storing coefficient and exponent of each component of...
A polynomial can be represented using linked list, storing coefficient and exponent of each component of polynomial in a node of it. Write a class in C++ to implement polynomials in three variables x, y and z (a polynomial may also have a constant term). Your class should implement methods for following operations. • Add two polynomials. • Multiply two polynomials. Use a two way header list for implementation. Your program should print the coefficient and exponent of each component...
IN C++ (THIS IS A REPOST) Design a class, Array, that encapsulates a fixed-size dynamic array...
IN C++ (THIS IS A REPOST) Design a class, Array, that encapsulates a fixed-size dynamic array of signed integers. Write a program that creates an Array container of size 100 and fills it with random numbers in the range [1..999]. (Use std::rand() with std::srand(1).) When building the array, if the random number is evenly divisible by 3 or 5, store it as a negative number. Within your main.cpp source code file, write a function for each of the following processes....
find the coefficient for the terms (A) What is the coefficient for the term x 4y...
find the coefficient for the terms (A) What is the coefficient for the term x 4y 3 in (x + y) 7 ? (B) What is the coefficient for the term x 4y 3 in (x − y) 7 ? (C) What is the coefficient for the term x 2y 3 z 2 in (x + y + z) 7 ?
Task #1 Construct the Models This term: This term, out of 85 students who took the...
Task #1 Construct the Models This term: This term, out of 85 students who took the beginning-of-term survey, 29 of them love pineapple on pizza. This gives a sample statistic of p-hat = 29/85 = 0.341. Last two terms: In the Fall and Spring, out of 60 students that took the beginning-of-term survey, 12 of them love pineapple on pizza. This gives a sample statistic of p-hat = 12/60 = 0.2. a) Discuss the assumptions and conditions required to use...
please use C++ Create a class named Byte that encapsulates an array of 8 Bit objects....
please use C++ Create a class named Byte that encapsulates an array of 8 Bit objects. The Byte class will provide the following member functions: Byte - word: Bit[8] static Bit array defaults to all Bits false + BITS_PER_BYTE: Integer16 Size of a byte constant in Bits; 8 + Byte() default constructor + Byte(Byte) copy constructor + set(Integer): void sets Bit to true + clear(): void sets to 0 + load(Byte): void sets Byte to the passed Byte + read():...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT