For a java assignment, I am tasked to write a program that consists of two class
ID: 3918549 • Letter: F
Question
For a java assignment, I am tasked to write a program that consists of two classes. The first class is called Polynomial() and it represents a polynomial object that consists of an array that stores all of the coefficients for that polynomial. The second class is called PolynomialUI() and it represents the command line user interface for the polynomial object. It gets input and displays output for the user. The part that I am stuck on is this. In the description of PolynomialUI() it states that the constructor, public PolynomialUI(), will create and intitialize the polynomial object and all other program state. I am not allowed to have this class extend Polynomial. I am struggling because when i instantiate the object in the constructor, and then instantiate PolynomialUI object in the main method of PolynomialUI(), i am unable to access the Polynomial object and the corresponding methods from Polynomial in the methods of PolynomialUI(). For instance, one method takes in user input and fills the Polynomial array. This is done by calling on the addCoefficient() method contained in Polynomial class. I am unable to do this and it continues to give me errors. Help would be very much appreciated because I cannot figure out how to do what is being asked.
Explanation / Answer
You can make use of the inner class to resolve your issue. I have refactored the code, please let me know if you ant other doubt.
import java.util.*;
import java.text.*;
public class PolynomialUI
{
private Polinomial head;
private double TOLERANCE = 0.00000001;
/***** the Polinomial (inner) class ********************************/
private class Polinomial
{
private DecimalFormat precision = new DecimalFormat("#.####");
private int exp; // coeff * x^exp
private double coeff;
private Polinomial next;
public Polinomial(int exp, double coeff, Polinomial next)
{
this.exp = exp;
this.coeff = coeff;
this.next = next;
}
public String toString()
{
String form = precision.format(Math.abs(coeff));
if(exp == 0) return form ;
else
if(exp == 1) return form + "*x";
else
return form +"*x^" + exp;
}
public boolean equals(Polinomial mono)
{
return exp == mono.exp && coeff == mono.coeff;
}
}
public PolynomialUI()
{
head = null;
}
/**
* Adds a new term into the polynomial, assuming that the polynomial
* is sorted in order from smallest to largest exponent.
*/
public void addTerm(int exp, double coeff)
{
if( Math.abs(coeff) < TOLERANCE ) return;
if( head == null || exp < head.exp )
{
head = new Polinomial(exp, coeff, head);
return;
}
Polinomial cur = head;
Polinomial prev = null;
while( cur != null && exp > cur.exp)
{
prev = cur;
cur = cur.next;
}
if( cur == null || exp != cur.exp )
prev.next = new Polinomial(exp, coeff, cur);
else
{
cur.coeff += coeff;
if( Math.abs(cur.coeff) < TOLERANCE )
if(prev != null)
prev.next = cur.next;
else
head = head.next;
}
}
public String toString()
{
StringBuffer sb = new StringBuffer();
for(Polinomial tmp = head; tmp != null; tmp = tmp.next)
if(tmp.coeff < 0 )
sb.append(" - " + tmp.toString());
else
sb.append(" + " + tmp.toString());
return sb.toString();
}
/**
* Adds two polynomials
* The method does not change the original polynomial.
*/
public PolynomialUI add(PolynomialUI poly)
{
PolynomialUI res = clone();
for(Polinomial tmp = poly.head; tmp != null; tmp = tmp.next)
res.addTerm(tmp.exp, tmp.coeff);
return res;
}
public PolynomialUI clone()
{
PolynomialUI res = new PolynomialUI();
for(Polinomial tmp = head; tmp != null; tmp = tmp.next)
res.addTerm(tmp.exp, tmp.coeff);
return res;
}
public boolean equals(PolynomialUI poly)
{
Polinomial tmp1 = head;
Polinomial tmp2 = poly.head;
while(tmp1 != null && tmp2 != null)
{
if( !tmp1.equals(tmp2) ) return false;
tmp1 = tmp1.next;
tmp2 = tmp2.next;
}
return true;
}
/**
* Multiplies by a number
* The method does not change the original polynomial.
*/
public PolynomialUI multiply(double num)
{
PolynomialUI res = clone();
for(Polinomial tmp = res.head; tmp != null; tmp = tmp.next)
tmp.coeff *= num;
return res;
}
/**
* Returns a new polynomial that is the derivative of this polynomial.
*/
public PolynomialUI diff()
{
PolynomialUI res = new PolynomialUI();
for(Polinomial tmp = head; tmp != null; tmp = tmp.next)
{
if(tmp.exp != 0)
res.addTerm(tmp.exp - 1, tmp.coeff * tmp.exp );
}
return res;
}
/**
* Computes the polynomial at x = value
*/
public double eval(double value)
{
double res = 0;
for(Polinomial tmp = head; tmp != null; tmp = tmp.next)
{
res += tmp.coeff * Math.pow(value, tmp.exp);
}
return res;
}
public static void main(String[] args)
{
PolynomialUI first = new PolynomialUI();
PolynomialUI second = new PolynomialUI();
System.out.println( "first" );
first.addTerm(1, 2.1);
first.addTerm(4, 2);
first.addTerm(3, 1);
first.addTerm(0, 1.3);
first.addTerm(4, 0.3);
System.out.println( first );
System.out.println( );
System.out.println( "second" );
second.addTerm(4, -2.3);
second.addTerm(2, 1);
second.addTerm(0, -1.3);
second.addTerm(1, 0.3);
System.out.println( second );
System.out.println( );
System.out.println( "compare first with second" );
System.out.println( first.equals(second) );
System.out.println( "compare first wih first" );
System.out.println( first.equals(first) );
System.out.println( );
System.out.println( "add first and second" );
PolynomialUI third = first.add(second);
System.out.println( third );
System.out.println( first );
System.out.println( second );
System.out.println( );
System.out.println( "multiply first by 0.2" );
PolynomialUI fourth = first.multiply(0.2);
System.out.println( fourth );
System.out.println( first );
System.out.println( );
System.out.println( "differentiate first" );
PolynomialUI diff = first.diff();
System.out.println( diff );
System.out.println( first );
System.out.println( );
System.out.println( "eval first at x = 1.5" );
System.out.println( first.eval(1.5) );
System.out.println( );
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.