I have to come up with a tester code for the code below but i am having some tro
ID: 3711162 • Letter: I
Question
I have to come up with a tester code for the code below but i am having some trouble. It is part of a lab that deals with implementing a polynomial class that contains a linked list of terms. The polynomial class supports multiplication and addition. I have everything done but this code tester part. Any help is greatly appreciated.
public class Term
{
// instance variables - replace the example below with your own
private double coefficient;
private int power;
/**
* Constructor for objects of class Term
* @param coefficent the coefficient
* @param power the power
*/
public Term(double coefficient, int power)
{
// initialise instance variables
this.coefficient = coefficient;
this.power = power;
}
/**
* @return the coefficient.
*/
public double getCoefficient()
{
// put your code here
return coefficient;
}
/**
* @return the power
*/
public int getPower()
{
return power;
}
/**
* Multiplies two coefficients together and returns the result
* @param t the other term
* @return this * t as a term
*/
public Term multiply(Term t)
{
return new Term(coefficient * t.coefficient, power + t.power);
}
/**
* Adds the term to this term if the powers are the same
* @param t the term to attempt to add
*/
public void addIfSamePower(Term t)
{
if (t.power == power)
{
coefficient += t.coefficient;
}
}
/**
* Returns a string reps of the term with a ^ reps the exponent.
* @return a string reps of a term
*/
@Override
public String toString()
{
switch (power)
{
case 0:
return Math.abs(coefficient) + "";
case 1:
return Math.abs(coefficient) + "x";
default:
return Math.abs(coefficient) + "x^" + power;
}
}
Explanation / Answer
I believe your Polynomial class is also missing.. So I am providing both the tester class and Polynomial class.
PolynomialTest.java
public class PolynomialTest
{
public static void main(String[] args)
{
Polynomial p = new Polynomial(new Term(-10, 0));
p.print();
System.out.println(" Expected: - 10.0");
p.add(new Polynomial(new Term(-1, 1)));
p.print();
System.out.println(" Expected: - 1.0x - 10.0");
p.add(new Polynomial(new Term(9, 7)));
p.print();
System.out.println(" Expected: 9.0x^7 - 1.0x - 10.0");
p.add(new Polynomial(new Term(5, 10)));
p.print();
System.out.println(" Expected: 5.0x^10 + 9.0x^7 - 1.0x - 10.0");
Polynomial q = p.multiply(p);
q.print();
System.out.println(
" Expected: 25.0x^20 + 90.0x^17 + 81.0x^14 - 10.0x^11 - 100.0x^10 - 18.0x^8 - 180.0x^7 + 1.0x^2 + 20.0x + 100.0");
}
}
Polynomial.java
/**
*
* A class to represent a polynomial.
*
*/
public class Polynomial
{
private Term t;
/**
*
* Constructs an empty polynomial
*
*/
public Polynomial()
{
}
/**
*
* Constructs a new polynomial with the given term
*
* @param t
* the term to initialize the polynomial with
*
*/
public Polynomial(Term t)
{
this.t = t;
}
/**
*
* Adds the polynomial such that the terms are in sorted order
*
* from highest power to lowest
*
* @param p
* the polynomial to add
*
*/
public void add(Polynomial p)
{
System.out.println(this.t.getPower() + p.t.getPower());
System.out.println(this.t.getCoefficient() + p.t.getCoefficient());
}
/**
Multiplies the given polynomial with this one and returns the result
@param p the polynomial to multiply
@return this * p
*/
public Polynomial multiply(Polynomial p)
{
return this;
}
/**
*
* Prints the polynomial "nicely" so that it reads
*
* from highest term to lowest and doesn't have a
*
* leading "+" if the first term is positive.
*
*/
public void print()
{
System.out.println(t.toString());
}
}
Output
10.0
Expected: - 10.0
1
-11.0
10.0
Expected: - 1.0x - 10.0
7
-1.0
10.0
Expected: 9.0x^7 - 1.0x - 10.0
10
-5.0
10.0
Expected: 5.0x^10 + 9.0x^7 - 1.0x - 10.0
10.0
Expected: 25.0x^20 + 90.0x^17 + 81.0x^14 - 10.0x^11 - 100.0x^10 - 18.0x^8 - 180.0x^7 + 1.0x^2 + 20.0x + 100.0
TermTester class
public class TermTester {
public static void main(String[] args) {
Term t1 = new Term(10, 1);
System.out.println("Term 1 is "+t1);
System.out.println("Coefficient of term 1 is : "+t1.getCoefficient());
System.out.println("Power of term 1 is : "+t1.getPower());
System.out.println();
Term t2 = new Term(9, 7);
System.out.println("Term 2 is "+t2);
System.out.println("Coefficient of term 2 is : "+t2.getCoefficient());
System.out.println("Power of term 2 is : "+t2.getPower());
System.out.println();
Term t3 = t1.multiply(t2);
System.out.println("Term 3 after multiplying t1 and t2 is "+t3);
System.out.println("Coefficient of term 3 is : "+t3.getCoefficient());
System.out.println("Power of term 3 is : "+t3.getPower());
System.out.println();
Term t4 = new Term(20, 7);
System.out.println("Term 4 is "+t4);
System.out.println("Coefficient of term 4 is : "+t4.getCoefficient());
System.out.println("Power of term 4 is : "+t4.getPower());
System.out.println();
t2.addIfSamePower(t4);
System.out.println("Term 2 after adding Term 4 is "+t2);
System.out.println("Coefficient of term 2 is : "+t2.getCoefficient());
System.out.println("Power of term 2 is : "+t2.getPower());
System.out.println();
t1.addIfSamePower(t4);
System.out.println("Term 1 after adding Term 4 is "+t1);
System.out.println("Coefficient of term 1 is : "+t1.getCoefficient());
System.out.println("Power of term 1 is : "+t1.getPower());
}
}
Output
Term 1 is 10.0x
Coefficient of term 1 is : 10.0
Power of term 1 is : 1
Term 2 is 9.0x^7
Coefficient of term 2 is : 9.0
Power of term 2 is : 7
Term 3 after multiplying t1 and t2 is 90.0x^8
Coefficient of term 3 is : 90.0
Power of term 3 is : 8
Term 4 is 20.0x^7
Coefficient of term 4 is : 20.0
Power of term 4 is : 7
Term 2 after adding Term 4 is 29.0x^7
Coefficient of term 2 is : 29.0
Power of term 2 is : 7
Term 1 after adding Term 4 is 10.0x
Coefficient of term 1 is : 10.0
Power of term 1 is : 1