Question

In Java. Needing help with this homework assignment.

two maps. P15.3 Write a class Polynomial that stores a polynomial such as f(x) = 5x10 + 9x7-x-10 as a linked list of terms. A term contains the coefficient and the power of x. For example, you would store p(x) as (5,10),(9,7),(-1,1),(?10,0) Supply methods to add, multiply, and print polynomials. Supply a constructor that makes a polynomial from a single term. For example, the polynomial p can be constructed as Polynomial p new Polynomial (new Term(-10, 0)); ?.add(new Polynomial (new Term(-1, 1))); p.add(new Polynomial(new Term(9, 7))); p. add(new Polynomial (new Term(5, 10))); Then compute p(x) x p(x) Polynomial q p.multiply(p); q.printO P15.4 Repeat Exercise P15.3, but use a Map<Integer, Doubles for the coefficients.

The following is the class I need to write up and implement.

media%2F155%2F15580d61-43ab-4085-9016-dd

media%2Fe1f%2Fe1fa01f2-d406-43e1-84bf-48


media%2F87a%2F87a0012a-f0d3-48b1-92fe-03

media%2F7b9%2F7b979008-80eb-49a9-9f3b-ba

media%2Fb18%2Fb1860623-ac15-44ca-815f-d7

0 0
Add a comment Improve this question Transcribed image text
Answer #1

public class PolynomialTester
{
    public static void main(String[] args)
    {
        Polynomial p = new Polynomial(new Term(-10, 0));
        p.print();
        System.out.println("\nExpected: - 10.0");
        p.add(new Polynomial(new Term(-1, 1)));
        p.print();
        System.out.println("\nExpected: - 1.0x - 10.0");
        p.add(new Polynomial(new Term(9, 7)));
        p.print();
        System.out.println("\nExpected: 9.0x^7 - 1.0x - 10.0");
        p.add(new Polynomial(new Term(5, 10)));
        p.print();
        System.out.println("\nExpected: 5.0x^10 + 9.0x^7 - 1.0x - 10.0");

        Polynomial q = p.multiply(p);

        q.print();
        System.out.println("\nExpected: 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");
    }
}
------------------------------------------------------------------------------------
import java.util.LinkedList;
import java.util.ListIterator;


public class Polynomial
{
    LinkedList<Term> poly;


    public Polynomial()
    {

        poly = new LinkedList<Term>();

    }

    //Constructs a new polynomial with the given term

    public Polynomial(Term t)
    {
        poly = new LinkedList();
        //add the Term t to the LinkedList
        poly.addLast(t);

    }

    //Adds the polynomial such that the terms are in sorted order from highest power to lowest

    public void add(Polynomial p)
    {


        Term nextPoly = p.poly.getFirst();

        ListIterator<Term> iter = poly.listIterator();
        int num = nextPoly.getPower();


        //sort order
        while(iter.hasNext()){

            Term xp = iter.next();
            //order is determined by the power

            if(num == xp.getPower()){

                iter.remove();
                iter.add(new Term(xp.getCoefficient() + nextPoly.getCoefficient(), xp.getPower()));
                break;
            }

            else if (num > xp.getPower()){

                iter.remove();
                iter.add(nextPoly);
                iter.add(xp);
                break;

            }

            else if(!iter.hasNext()){

                iter.add(nextPoly);

            }

        }

        if(poly.isEmpty()){

            poly.add(nextPoly);

        }

        //finally add the new object to the LinkedList
        //poly.add(n,nextPoly);

    }

    //Multiplies the given polynomial with this one and returns the result

    public Polynomial multiply(Polynomial p)
    {

        Polynomial q = new Polynomial();
        ListIterator<Term> iter = p.poly.listIterator();
        ListIterator<Term> iter2 = p.poly.listIterator();

        while(iter.hasNext()){

            Term x = iter.next();
            double coffi= x.getCoefficient();
            int power = x.getPower();

            while(iter2.hasNext()){


                Term x2 = iter2.next();
                double coffi2= x2.getCoefficient();
                int power2 = x2.getPower();
                /*Term newTerm = new Term (coffi * coffi2, power + power2);*/

                q.add(new Polynomial(new Term (coffi * coffi2, power + power2)));

            }

            while(iter2.hasPrevious()){

                iter2.previous();

            }

        }


        return q;

    }

    //Prints the polynomial

    public void print()
    {

        int i = 0;

        //print everything from LinkedList
        for(Term x : poly){

            //This is to get the add or subtract sign
            char sign = ' ';

            sign = ((x.getCoefficient() + "").charAt(0) == '-') ? '-' : '+';

            if(sign == '+' && i == 0){

                System.out.print(" " + x.toString() + " ");

            }

            else{

                System.out.print(sign + " " + x.toString() + " ");

            }

            ++i;
        }


    }
}

------------------------------------------------------------------------------------------------
public class Term
{
    private double coefficient;
    private int power;

    // constructor to initialize a single term with a given coefficient and power

    public Term(double coefficient, int power)
    {
        this.coefficient = coefficient;
        this.power = power;
    }


    public double getCoefficient()
    {
        return coefficient;
    }


    public int getPower()
    {
        return power;
    }

    //Multiplies two coefficient together and returns the result

    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

    public void addIfSamePower(Term t)
    {
        if (t.power == power)
        {
            coefficient += t.coefficient;
        }
    }

    // Returns a string representation of the term with a ^ representing the exponent

    public String toString()
    {
        if (power == 0)
        {
            return Math.abs(coefficient) + "";
        }
        else if (power == 1)
        {
            return Math.abs(coefficient) + "x";
        }
        else
        {
            return Math.abs(coefficient) + "x^" + power;
        }
    }
}

Add a comment
Know the answer?
Add Answer to:
In Java. Needing help with this homework assignment. The following is the class I need to...
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. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • in C++ as Exercise P12.14. Write a class Polynomial that stores a polynomial such p(x) =...

    in C++ as Exercise P12.14. Write a class Polynomial that stores a polynomial such p(x) = 5x10 + 9x7-x-10 as a linked list of terms. A term contains the coefficient and the example, you would store p(x) as power of x. For (5,10),(9,7),(-1,1),(一10,0) Supply member functions to add, multiply, and print polynomials. Supply a com- structor that makes a polynomial from a single term. For example, the polynomia; can be constructed as Polynomial p(Term(-10, 0)); p.add (Polynomial(Term(-1, 1))); p.add (Polynomial(Term(9,...

  • I need help filling in the the code in the methods add, multiply, and evaluate package...

    I need help filling in the the code in the methods add, multiply, and evaluate package poly; import java.io.IOException; import java.util.Scanner; /** * This class implements evaluate, add and multiply for polynomials. * * * */ public class Polynomial {       /**    * Reads a polynomial from an input stream (file or keyboard). The storage format    * of the polynomial is:    * <pre>    * <coeff> <degree>    * <coeff> <degree>    * ...    *...

  • The second lab continues with the Polynomial class from the first lab by adding new methods...

    The second lab continues with the Polynomial class from the first lab by adding new methods for polynomial arithmetic to its source code. (There is no inheritance or polymorphism taking place yet in this lab.) Since the class Polynomial is designed to be immutable, none of the following methods should modify the objects this or other in any way, but return the result of that arithmetic operation as a brand new Polynomial object created inside that method. public Polynomial add(Polynomial...

  • For this lab, you must define a class called Polynomial. This class definition must include the f...

    Please answer this in python 3, thank you. For this lab, you must define a class called Polynomial. This class definition must include the following methods: ._init_0- the initialiser for the class ._str_0- returns a formatted string representation of a Polynomial object add_term) - adds a new term (coefficient and exponent) to the Polynomial .addo-modifies the existing Polynomial by adding another one to it ._add_0-returns a new Polynomial object that is the sum of two polynomials scale) - scales a...

  • Create a class to represent a term in an algebraic expression. As defined here, a term...

    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 parameters, and accessor methods that return the coefficient and the exponent. Your class...

  • For this lab, you must define a class called Polynomial. This class definition must include the f...

    please answer this question in python 3 For this lab, you must define a class called Polynomial. This class definition must include the following methods: ._init_0- the initialiser for the class ._str_0- returns a formatted string representation of a Polynomial object add_term) - adds a new term (coefficient and exponent) to the Polynomial .addo-modifies the existing Polynomial by adding another one to it ._add_0-returns a new Polynomial object that is the sum of two polynomials scale) - scales a Polynomial...

  • Polynomial Using LinkedList class of Java Language Description: Implement a polynomial class using a LinkedList defined...

    Polynomial Using LinkedList class of Java Language Description: Implement a polynomial class using a LinkedList defined in Java (1) Define a polynomial that has the following methods for Polynomial a. public Polynomial() POSTCONDITION: Creates a polynomial represents 0 b. public Polynomial(double a0) POSTCONDITION: Creates a polynomial has a single x^0 term with coefficient a0 c. public Polynomial(Polynomial p) POSTCONDITION: Creates a polynomial is the copy of p d. public void add_to_coef(double amount, int exponent) POSTCONDITION: Adds the given amount to...

  • A polynomial p(x) is an expression in variable x which is in the form axn + bxn-1 + …. + jx + k, where a, b, …, j, k are...

    A polynomial p(x) is an expression in variable x which is in the form axn + bxn-1 + …. + jx + k, where a, b, …, j, k are real numbers, and n is a non-negative integer. n is called the degree of polynomial. Every term in a polynomial consists of a coefficient and an exponent. For example, for the first term axn, a is the coefficient and n is the exponent. This assignment is about representing and computing...

  • Java code tasks: Please help with a single class (class Coord) in this program. This project...

    Java code tasks: Please help with a single class (class Coord) in this program. This project is called Panic! We will describe a single-level floorplan of a building, filled with different kinds of people and different kinds of threats. We will simulate how the people seek to evacuate the building to safety, and how the threats spread throughout the building. Various events will be announced, such as person movements, threats spawning, and people reaching safety or succumbing to the threats....

  • I only need help with the constructors and add method. Thanks! Background Arbitrary length integers are...

    I only need help with the constructors and add method. Thanks! Background Arbitrary length integers are integers that have no size restriction. Recall that the int type has a range of -2,147,483,648 to 2,147,483,647. The long type runs from -263 to 263 - 1. Sometimes these numbers are not large enough. One example application that may need larger numbers is public-key cryptography where very large integers are used to make decryption hard. A large integer may be represented as a...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT