Answered step by step
Verified Expert Solution
Question
1 Approved Answer
please include a main class as instructed to test the program. that will be helpful for me as well. and also it has to be
please include a main class as instructed to test the program. that will be helpful for me as well. and also it has to be in java
You are to develop a Java program that adds polynomials. The polynomiak will be represented in your program as singly linked lists of terms. All of the polynomials will be in terms of the variabler You will write four Java classes: The Term Class Term represents a temm in a polynomial. It will have two private instance variables (data fields) named cocflicient and exponent. For example, 4x would be represented by a Term object with coefficient 4 and exponent 6 . In terms of methods, this class needs - gelters for each instance variable - a constructor that takes parameters for both instance variables - a toString that retums the Tem in the form: 4x6 - an add Term method that takes a Term as a parameter and returns a Term that is the sum of the calling object and the parameter. If the sum of the two Terms cannot be expressed as a single Term (because the exponents don't mateh), it returns null. The Polynomial Class Polynomial is the central class of your program, representing the actual polynomial expressions. It will include the following elements: - A private inner class Node with fwo instance variables termData of type Term and next of type Node. This class tunctions as the node elass for the singly linked list representing your polynomial and should have the necessary functionality for that, - Two private instance variables of type Node: terusstead and termsTail. These will be nall if the polynomial has no terms. Otherwise, they will point to the first and last terms of the polynomial, respectively. - The following public methods: a defiult constructor a constructor that accepts a String representing a polynomial in the form 4x6+ 2x3+1x2 and initializes the object to represent that polynornial. We are simplifying the problem by guaranteeing spaces betwecn terms and operators and explicitly representing coefficients of 1(50x2 is 1x2 rather than x2). Note that subtraction in the polynomial will result in a negative coefficient: 3x22 will bave two terms, one with coefficient 3 and exponent 2 and one with cocfficient -2 and exponent 0 . There will be no negative exponents. an addernToPolynomial that adds a Tenn to the end of the list of terms. a toString that returns a String repeesenting the polynomial in the form shown in the sample output below. an addPolynomial that accepts a Polynomial object as a parameter and returns a Polynomial object representing the sum of the calling object and the parameter: Your new polynomial must have the terms in the standard order (largest exponent to-ssnallest) and mest not include terns with coefficients of 0 . - You may add private methods as desired to improve the design of the program. Your fourth class will include main and will use the otber classes to write a program that allows users to entes pairs of polynomials to be added. Your fourth class will include main and will use the other classes to write a progr users to enter pairs of polynomials to be added. Sample program run (user input in bold) Welcome to the Polynomial Addition Program. Please enter your first polynomial: 4x3+2x7 Please enter the second polynomial: 10x4+2x32x+3 The sum is: 10x4+6x34 Would you like to add two more polynomials? y Please enter your first polynomial: 6x35x2+4 Please enter the second polynomial: 2x37x2+3x4 The sum is: 4x312x2+3x Would you like to add two more polynomials? n Thank you for using the Polynomial Addition Program Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started