Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

public class Term implements TermInterface{ private int coefficient; private int power; public Term(){ coefficient=2; power=3; } public Term(int coefficient,int power){ this.coefficient=coefficient; this.power=power; } public Term(Term

public class Term implements TermInterface{

private int coefficient;

private int power;

public Term(){

coefficient=2;

power=3;

}

public Term(int coefficient,int power){

this.coefficient=coefficient;

this.power=power;

}

public Term(Term t){

try{

if(t==null){

throw new IllegalArgumentException("The Copy objcet is null");

}else{

this.coefficient=t.coefficient;

this.power=t.power;

}

}catch(IllegalArgumentException e){

System.out.println(e);

}

}

@Override

public String toString(){

String result=null;

if(coefficient==0){

result="0";

}else if(power==0){

result="";

}else if(coefficient==1&&power==1){

result="x";

}else if(coefficient==-1&&power==-1){

result="-x";

}else if(power==1){

result=String.valueOf(coefficient)+"x";

}else if(coefficient==1){

result="x^"+String.valueOf(power);

}else if(coefficient==-1){

result="-x^"+String.valueOf(power);

}else{

result=String.valueOf(coefficient)+"x^"+String.valueOf(power);

}

return result;

}

@Override

public int degree() {

if(coefficient==0)

return 0;

else

return power;

}

@Override

public Term derivative() {

Term term=null;

double derivative=coefficient*(power-1);

term=new Term(coefficient,power-1);

return term;

}

@Override

public boolean equals(Object obj){

boolean status=false;

if(this.equals(obj)){

status=true;

}

return status;

}

@Override

public double evaluate(double value) {

return value;

}

@Override

public void addIn(Term anotherTerm) {

try{

if(anotherTerm==null)

{

throw new IllegalArgumentException("The anotherterm object is null");

}else if(anotherTerm.power!=power){

throw new IllegalArgumentException("The anotherterm object power and current poer not same");

}else{

Term term=new Term(anotherTerm);

}

}catch (IllegalArgumentException e) {

System.out.println(e);

}

}

@Override

public Term multiplyBy(Term anotherTerm) {

Term term=null;

try{

if(anotherTerm==null)

{

throw new IllegalArgumentException("The anotherterm object is null");

}else{

term=new Term(anotherTerm);

}

}catch (IllegalArgumentException e) {

System.out.println(e);

}

return term;

}

}

/ This interface will define the methods that must be present in any

class that implements it

// The comments will serve as the requirements for the methods.

// The requirements for the extra credit are also listed in the comments,

but the actual

// interface for those methods have been commented out.

public interface PolynomialInterface

{

//insert - updates itself by inserting the Term that is received so

that Terms in the resulting

// Polynomial are in descending

order.

// if the received Term is null, it should

// throw new

IllegalArgumentException("your String");

// If the received Term isZero(), it should not

be inserted

// If the received Term's degree is already

present,

// then existing Term with the same

degree just adds in the received Term.

// Otherwise, the received Term should be put

into the correct place in the Polynomial

// so that the degrees are in

descending order. (just like a Math Polynomial).

// If the insertion results in a 0 Term in the

Polynomial then the 0 Term should be removed.

public void insert(Term aTerm);

//toString - returns its representation as a String

// Each term should use its own toString()

when building up the String to return

// Terms in the toString() should be

separated by " + " (OK to have 4x^3 + -2x^2)

// public String toString(); //EXTRA CREDIT

//toALString - returns its representation as a String by returning

it's ArrayList's toString().

public String toALString();

//multiplyBy - receives another Polynomial and finds the product

of itself and the Polynomial that is received.

// It the Polynomial that is received

is null, it should

// throw new

IllegalArgumentException("your String");

// Creates and returns a new

Polynomial that holds the answer. Neither itself nor the

// Polynomial that is received are

changed.

// public Polynomial multiplyBy(Polynomial anotherPoly); //EXTRA

CREDIT

//contains - returns true if it contains the Term that is

received.

// If the received Term is null, it should

// throw new

IllegalArgumentException("your String");

// (Term must have a correct .equals for

it to work)

public boolean contains(Term aTerm);

//numTerms - returns the number of Terms in this Polynomial

public int numTerms();

//evaluate - returns what the entire Polynomial evaluates to, using

the received value of x

public double evaluate(double x);

The assignment: You have already written a class called Term, which holds a Mathematical Term. For this Minilab, you are to write a class called Polynomial, which uses an ArrayList (or Vector if you want) to hold a number of Terms, which form a polynomial.

Changing your Term class: Please be sure that your Term class has data that is private since other classes should not be able to access the data directly. To make the Polynomial class work correctly, your Term class should also contain another method called:

//isZero returns true if this instance of a Term is 0 (has a 0 coefficient) public boolean isZero()

You should understand:

how your Term class works, especially the following methods: isZero

degree addIn multiplyBy evaluate

how an ArrayList works, especially the following methods: add (both versions)

get remove isEmpty size contains toString

Using the Term class in your Polynomial class (has-a vs. is-a):

class should use an ArrayList (or Vector) of Terms to hold its data.

Your Polynomial It can either:

1) Have an ArrayList as its data, similar to our Queue demo. In that case, it would be a has-a relationship since the Polynomial has a ArrayList. The data and relationship are shown below:

public class Polynomial {

//------- data private ArrayList theAL;

And every time a method wanted to manipulate the data, it would access the ArrayLists methods with a call such as:

theAL.add(i, aTerm);

(Polynomial class)

(ArrayList class)

2) Or...have an ArrayList) as parent class, so it would be an ArrayList itself and inherit ArrayLists methods. In that case, it would be an is-a relationship since the Polynomial is a ArrayList. The data and relationship are shown below:

public class Polynomial extends ArrayList (ArrayList class) {

//------- data (nothing, since it already is an ArrayList)

(Polynomial class)

And every time a method wanted to manipulate the data, it would access its own (inherited) methods with a call such as:

this.add(i, aTerm); //calls its own method that was inherited from ArrayList

However, do not use both has-a and is-a at the same time. Java allows it, but it is very easy to mix up which ArrayList is being manipulated.

Implementing PolynomialInterface: Your Polynomial class should implement PolynomialInterface. If you choose to use both implements and extends then they are written like this:

public class Polynomial extends ArrayList implements PolynomialInterface

You can have additional methods besides those specifically listed in the PolynomialInterface. You will have to do this to add the extra credit.

Inserting a Term into the ArrayList: The insert(Term aTerm) method is the most challenging, as the Terms should be in descending order by degree. The insert method will have to traverse the ArrayList and decide when and where to tell the ArrayList to add it or tell an existing Term to do an addIn. The methods of individual elements can be accessed like this:

for (int i=0; i (for example) theAL.get(i).addIn(...

Or, if you decide to use a variable, like this: {

Term myTerm = theAL.get(i);

myTerm.addIn(... }

changing myTerm will also change the element in the ArrayList, since they are both references to the same Term.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Advances In Databases And Information Systems 25th European Conference Adbis 2021 Tartu Estonia August 24 26 2021 Proceedings Lncs 12843

Authors: Ladjel Bellatreche ,Marlon Dumas ,Panagiotis Karras ,Raimundas Matulevicius

1st Edition

3030824713, 978-3030824716

More Books

Students also viewed these Databases questions

Question

How does FHLMC differ from FNMA? How are they the same?

Answered: 1 week ago