in java please

Business, Finance, Economics, Accounting, Operations Management, Computer Science, Electrical Engineering, Mechanical Engineering, Civil Engineering, Chemical Engineering, Algebra, Precalculus, Statistics and Probabilty, Advanced Math, Physics, Chemistry, Biology, Nursing, Psychology, Certifications, Tests, Prep, and more.
Post Reply
answerhappygod
Site Admin
Posts: 899604
Joined: Mon Aug 02, 2021 8:13 am

in java please

Post by answerhappygod »

In Java Please 1
In Java Please 1 (144.83 KiB) Viewed 32 times
in java please
Part 1 A polynomial is an expression consisting of a variable along with some terms including coefficients and powers. For instance, p(x) = 3x+ + x? - 4x + 5 is a polynomial. We can see a polynomial as a list of terms. A term contains a non-negative integer as the power of variable x and an integer as the coefficient. Develop a class Polynomial that stores a polynomial using its terms as some pairs of two integers for power and coefficient, using the Map interface. For instance, we can see the above polynomial as P(x):(4,3), (2,1),(1,-4),(0,5) Note: You must store the polynomial in its descending order, i.e., start from the term with the highest power to the term with the lowest power. For doing this, you can use the method reverseOrder() from the Collections utility class as the argument for the map you declare in the Polynomial class. Provide required instance variables. (3 marks) Provide the following methods: (2 marks) Default constructor: It creates an empty polynomial. For an empty polynomial, you can set its degree to -1. • (3 marks) Second constructor. It receives two integers, power and coefficient, and creates its corresponding polynomial that has only one term. . (4 marks) Third constructor: It receives a map of powers and coefficients and creates its corresponding polynomial. • (3 marks) A copy constructor: It receives a polynomial and creates a deep copy of it. • (5 marks) add: It receives a Polynomial object as a parameter and adds it to the existing polynomial, i.e., the implicit parameter. . (3 marks) overloaded add: It is a static method that receives two Polynomial objects as parameters, adds them, stores them in another Polynomial object, and returns it. . (5 marks) subtract: It receives a Polynomial object as a parameter and subtracts it from the existing polynomial, i.e., the implicit parameter. This method can use the add method, defined above, to conduct polynomial subtraction. • (3 marks) overloaded subtract: It is a static method that receives two Polynomial objects as parameters, subtracts the second one from the first one, stores in another Polynomial object, and . (4 marks) multiply: It receives a Polynomial object as a parameter and returns a Polynomial object which is the multiplication of the parameter and the existing polynomial, i.e. the implicit parameter. . (2 marks) degree: It returns the degree of the Polynomial object, i.e., the implicit parameter. To have an efficient way of finding the degree of a polynomial, you must define an instance variable, degree, for the Polynomial class, and keep updating its value whenever needed. The degree of a polynomial is the greatest power. For instance, the degree of the polynomial above is 4. (3 marks) coefficient: It receives an integer (power) as a parameter and returns its corresponding coefficient in the Polynomial object. For instance, for the polynomial above, if the method receives 2, it should return 1, which is the coefficient of the term with a power of 2. (3 marks) changeCoefficient: It receives two integers, power and a new coefficient as parameters and changes the corresponding coefficient of the power given to the new coefficient in the Polynomial object. For instance, for the polynomial above, if the method receives 2 and 4, it should returns it. Page 2 of 6
change the coefficient of x* from 1 to 4. If a term with the power given does not exist in the Polynomial, do nothing, . (2 marks) removeTerm: It receives an integer (power) as a parameter and removes the term with the corresponding power from the Polynomial. If a term with that power does not exist, do nothing. . (3 marks) evaluate: It receives a double value as a parameter and evaluates the Polynomial object using the value. For instance, if this method is called with 2 for the polynomial above, it should return 49. (p(2) = 3 2* +22 - 4+2+5 = 49. Exception Handling: If a negative value has been sent for the power for a given term, your program should not accept it. This means you must properly handle the exception by showing a message and ignoring that term. The execution of the program should not be terminated in this case. You can simply throw the IllegalArgumentException in this case and catch it properly. (2 marks) Override the following standard methods inherited from the object class: • (3 marks) equals: Two Polynomial objects are equal if they have the same number of terms, with the same powers and corresponding coefficients. • (4 marks) toString: It should return a string representation of the Polynomial object. Note that if the coefficient of a term is 1, or if the power of a term is 1, you should not print them. For instance, for the above polynomial, it should return: "P(x) = 3x4 + x2 - 4x + 5* We would also like to be able to compare two polynomials as follows: First make the Polynomial class Comparable, and then implement the required method based on the rules below: (3 marks) For two polynomials p(x), as the implicit parameter, and g(x), as the explicit parameter: o The method should return PO) > 9(0) o The method should return p(0) < 9(0) The method should return if p(O) = 9(0) . For instance, p(x)= 3x4 + x2 - 4x +5>(x) = 5x4 + 2x3 + 1 because p(O)=5>20) = 1 Part 2: In this part, you must create another Java class, Quadratic, which is a subclass of Polynomial, with the following features: A quadratic polynomial is in the format of g(x) = ax +bx+c, in which a, b, and c are integers. A quadratic polynomial has two roots, one root, or no root, based on the value of delta = b2 - 4ac. Quadratic class has two extra instance variables, rootl and root2. (1 mark) Provide the following methods: (4 marks) Constructor: It creates a quadratic polynomial with three coefficients. Note: In this constructor, you must use the existing public methods you have already developed in the superclass. Remember to calculate the roots of the quadratic as well. (1 mark) Getter methods: Two getter methods for the two roots. (4 mark) roots: This method should calculate the roots of the quadratic polynomial and update the corresponding instance variables. Hint: Return true if the quadratic has roots, false otherwise. The tester class, PolynomialTester.java, has been provided to help you for the development as well as executing and comparing the expected and actual outputs. The outputs of the PolynomialTester class using your code for Polynomial.java should be similar to on the next page. 1 -1 0 if c Page 3 of 6
Creating P1(x) with the terms (0,-9), (2,-2), (6,8), and (4,6) ... P1(x) - BX6 + 6x4 - 2x2 - 9 Degree of Pl(x) - 6 Coefficient of x2 in Pl(x) - -2 P1(x) after changing coefficient of term 6 and removing term 4 P1(x) - 3x6 - 2x2 - 9 Creating P2(x) with the terms (-2,5), (0,4), (2,1), and (1,3) ... java.lang. IllegalArgumentException: Power of a term can't be negative. The term ignored. P2(x) = x2 + 3x + 4 Create a copy of P2(x) ... Copy of P2(x) - x2 + 3x + 4 Adding P2(x) to P1(x)... P1(x) - Pl(x) + P2(x) - 3x6 - x2 + 3x - 5 Subtracting Pl(x) from P2(x) and store in P2(x)... P2(x) - P2(x) - P1(x) - - 3x6 + 2x2 + 9 Multiplying Pl(x) by P2(x) and store it into Q(x) ... Q(x) - P1(x) * P2(x) - - 9x12 + 9x8 - 9x7 + 42x6 - 2x4 + 6x3 - 19x2 + 27% - 45 P2(5) - -46816.0 P1(x) - 3x6 - x2 + 3x - 5 P2(x) - - 3x6 + 2x2 + 9 P1(x) is not equal to P2(x) Add P1(x) and P2(x) and store it into P3(x) ... P3(x) - x2 + 3x + 4 Subtracting Pl(x) from P2(x) and store it into P4(x) ... P4(x) - - 6x6 + 3x2 - 3x + 14 P3(x) is less than P4(x) Q(x) - 2x2 + 5x - 3 -3.000 Roots of quadratic Q(x) -5x 2+10x+3: Rooti- 0.500, Root- Q(x) - 10x2 + 5x + 3 This quadratic polynomial has no real roots. (Delta < 0)
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!
Post Reply