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.
Please use C# to answer.
Working code implemented in C# and appropriate comments provided for better understanding.
Source Code for Term.cs:
using System;
public class Term : IComparable
{
private double coefficient;
private int exponent;
// Creates a term with the given coefficient and exponent
public Term(double coefficient, int exponent)
{
this.coefficient = coefficient;
this.exponent = exponent;
}
// (2 marks)
// Evaluates the current term at x
public double Evaluate(double x)
{
double answer;
answer = coefficient * Math.Pow(x, exponent);
return answer;
}
// (2 marks)
// 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)
{
Term temp = obj as Term;
if (Exponent < temp.Exponent)
return -1;
else
if (Exponent == temp.Exponent)
return 0;
else
return 1;
}
//(4 marks)
// Returns a string representation of the current term
public override string ToString()
{
return " + (" + Math.Pow(coefficient, exponent);
}
// (3 marks)
// 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. (3
marks)
public double Coefficient { get; set; }
public int Exponent
{
get
{ return exponent; }
set
{
if(exponent < 0 || exponent > 99)
throw new ArgumentOutOfRangeException("!! Out of Range !!");
else
exponent = value;
}
}
}
public class Test
{
static void Main(string[] args)
{
Term m = new Term(10,9);
m.Coefficient = 10;
m.Exponent = 9;
m.Evaluate(20);
m.ToString();
Console.Write(m.Exponent);
Console.Write(m.Coefficient);
Console.Write(m.ToString());
}
}
Sample Output Screenshots:
Get Answers For Free
Most questions answered within 1 hours.