Question

The class Term encapsulates the coefficient and exponent of a single term. Exponents are limited in...

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.

Homework Answers

Answer #1

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:

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
This is my course class. Can someone please explain what the equals method is for and...
This is my course class. Can someone please explain what the equals method is for and also the compareTo method is for in general and then explain what the methods are doing in this class. public class Course {    private boolean isGraduateCourse;    private int courseNum;    private String courseDept;    private int numCredits;       public Course(boolean isGraduateCourse, int courseNum, String courseDept, int numCredits) {        this.isGraduateCourse=isGraduateCourse;        this.courseNum=courseNum;        this.courseDept=courseDept;        this.numCredits=numCredits;   ...
Implementing Polynomials using Singly Linked List in C++ The Term Class Create a class to represent...
Implementing Polynomials using Singly Linked List in C++ The Term Class Create a class to represent a term in an algebraic expression. As defined here, a term consists of an integer coefficient and a nonnegative integer exponent. E.g. • in the term 4X2, the coefficient is 4 and the exponent 2 • in -6X8, the coefficient is -6 and the exponent 8 Your class will have a constructor that creates a Term object with a coefficient and exponent passed as...
Please solve this problem in java. import java.util.Arrays; public class PriorityQueue { /* This class is...
Please solve this problem in java. import java.util.Arrays; public class PriorityQueue { /* This class is finished for you. */ private static class Customer implements Comparable { private double donation; public Customer(double donation) { this.donation = donation; } public double getDonation() { return donation; } public void donate(double amount) { donation += amount; } public int compareTo(Customer other) { double diff = donation - other.donation; if (diff < 0) { return -1; } else if (diff > 0) { return...
TrackMinMax For this lab, you will create a generic version of the IntTrackMinMax class you wrote...
TrackMinMax For this lab, you will create a generic version of the IntTrackMinMax class you wrote in a previous lab, called TrackMinMax. The API is: Function Signature Description constructor TrackMinMax() constructor check void check(T i) compares i to the current minimum and maximum values and updates them accordingly getMin T getMin() returns the minimum value provided to check() so far getMax T getMax() returns the maximum value provided to check() so far toString String toString() returns the string "[min,max]" As...
java Consider the following class definition: public class Circle { private double radius; public Circle (double...
java Consider the following class definition: public class Circle { private double radius; public Circle (double r) { radius = r ; } public double getArea(){ return Math.PI * radius * radius; } public double getRadius(){ return radius; } } a) Write a toString method for this class. The method should return a string containing the radius and area of the circle; For example “The area of a circle with radius 2.0 is 12.1.” b) Writeaequalsmethodforthisclass.ThemethodshouldacceptaCircleobjectasan argument. It should return...
Please explain code 1 and code 2 for each lines code 1 public class MyQueue {...
Please explain code 1 and code 2 for each lines code 1 public class MyQueue {    public static final int DEFAULT_SIZE = 10;    private Object data[];    private int index; code 2 package test; import java.util.*; /* Class Node */ class Node { protected Object data; protected Node link; /* Constructor */ public Node() { link = null; data = 0; } /* Constructor */ public Node(Object d,Node n) { data = d; link = n; } /*...
in jGRASP INVENTORY CLASS You need to create an Inventory class containing the private data fields,...
in jGRASP INVENTORY CLASS You need to create an Inventory class containing the private data fields, as well as the methods for the Inventory class (object). Be sure your Inventory class defines the private data fields, at least one constructor, accessor and mutator methods, method overloading (to handle the data coming into the Inventory class as either a String and/or int/float), as well as all of the methods (methods to calculate) to manipulate the Inventory class (object). The data fields...
Assignment 1 - ITSC 2214 I have to complete the array list for a shopping list...
Assignment 1 - ITSC 2214 I have to complete the array list for a shopping list code and can't figure out how to complete it: Below is the code Complete the three empty methods (remove(), find(), and contains()) in the ShoppingListArrayList.java file. These methods are already implemented in the ShoppingListArray class. /////////////////////////////////////////////////////////////////////////////////////////////////////////// Grocery Class (If this helps) package Shopping; public class Grocery implements Comparable<Grocery> { private String name; private String category; private int aisle; private float price; private int quantity;...
public final class SimpleRegister implements ICashRegister { //(value of coin, number of coins) private Map<Integer, Integer>...
public final class SimpleRegister implements ICashRegister { //(value of coin, number of coins) private Map<Integer, Integer> moneyBox; //store the log of transactions for auditing StringBuilder log; /** * Constructs an empty register */ public SimpleRegister() { moneyBox = new TreeMap<Integer, Integer>(); moneyBox.put(1, 0); moneyBox.put(5, 0); moneyBox.put(10, 0); moneyBox.put(25, 0); moneyBox.put(100, 0); moneyBox.put(500, 0); moneyBox.put(1000, 0); log = new StringBuilder(); } @Override public void addPennies(int num) { moneyBox.put(1, moneyBox.get(1) + num); String auditMessage = String.format("Deposit: $%.02f\n", num * 1 / 100.0f);...
Need to get the following output by Editing ChekingAccount.h ,ChekingAccount.cpp CheckingAccount Derived class CheckingAccount that inherits...
Need to get the following output by Editing ChekingAccount.h ,ChekingAccount.cpp CheckingAccount Derived class CheckingAccount that inherits from base class Account and include an additional data member of type double that represents the fee charged per transaction (transactionFee). Write Checking- Account’s constructor that receives the initial balance, as well as a parameter indicating a transaction fee amount. If transaction fee is less than zero, the transactionFee will be set to zero. Write the chargeFee member function that updates the balance by...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT