Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

How can I use an array List using java syntax to solve this problem? In this programming assignment you are to design and implement an

How can I use an array List using java syntax to solve this problem?

In this programming assignment you are to design and implement an ADT for a polynomial. You are also to create a polynomial adder application which allows the user to input two polynomials, and then adds the two inputted polynomials.

A polynomial for this ADT will have a single variable -- x. The polynomial needs to be allowed to have only non-negative exponents. Here are some polynomial examples with non-negative exponents:

image text in transcribed4x5+7x3x2+9

image text in transcribed3x7+4x5+7x3x2+9

image text in transcribed2x43x+10

The polynomial ADT should have the following methods:

+degree():integer // Returns the degree of the polynomial.

// The degree of the polynomial is always equal to

// the highest non-negative exponent of x in the polynomial.

+getCoefficient(in power:integer):integer // Returns the coefficient of the xpower term.

+changeCoefficient(in newCoef:integer, in power:integer) // Replaces the coefficient

// of the xpower term with

// newCoef

+add(in addend:polynomial):polynomial // Returns the sum of the given polynomial

// object and the polynomial argument

+toString():String // Takes the polynomial whose toString method is being called and

// represents the polynomial as a string

// Note that this overrides Object.toString

Be aware that the following special cases for toString need to be taken into account:

+1, -1 coefficients don't display the 1 (unless power is 0)

power 1 don't display ^1

power 0 don't display x^0

negative coefficients don't display +

first term don't display a preceding +

Be sure to document each method's purpose. Your documentation should include the following:

(1) the purpose of each variable you are using in your method

(2) the purpose of any programming decision and loop syntax

(3) an overall description of the algorithm the method is implementing and how the method is using the items of (1) and (2) in its implementation

You should use a Java exception name InvalidPowerException that you make yourself. InvalidPowerException can be made with a derived class that extends the RuntimeException class. As an example, suppose we wanted to make an exception named MyException and it was derrived from RuntimeException. The syntax for MyException would be:

public class MyException extends RuntimeException {

public MyException(String s) {

super(s);

}

}

Here MyException has a constructor that simply calls the constructor of the super class of where it is derived from. In Java, the super class is the class that a derived class inherits from. Since in this example MyException is extended from RuntimeException, RuntimeException is the super class of MyException. RuntimeException is a Java built in exception class that has its own logic that knows how to handle a string that is passed to its constructor.

Your InvalidPowerException class should be made in the same way that MyExcpetion is made. Your InvalidPowerException class should be thrown in the getCoefficient and changeCoefficient methods if their power parameter receives a negative value.

Here is a Sample dialog example of the polynomial adder application you need to make using the polynomial ADT (user input appears in bold):

----------------------------------------------------------------------------------------------------------------------

POLYNOMIAL ADDER

This program adds two polynomials and displays the result.

Enter first polynomial.

Enter each term on a separate line; coefficient followed by power.

For example, 3 4 represents the term 3x^4.

Enter -1 -1 to end input for the polynomial.

4 5

7 4

-1 2

9 0

-1 -1

First polynomial: 4x^5 + 7x^4 - x^2 + 9

Enter second polynomial.

Enter each term on a separate line; coefficient followed by power.

For example, 3 4 represents the term 3x^4.

Enter -1 -1 to end input for the polynomial.

2 4

3 1

-10 0

-1 -1

Second polynomial: 2x^4 + 3x - 10

First polynomial: 4x^5 + 7x^4 - x^2 + 9

Second polynomial: 2x^4 + 3x - 10

Sum: 4x^5 + 9x^4 - x^2 + 3x - 1

Here's an example with try/catch logic using the exception thrown by the changeCoefficient to detect inputs of powers that are out of range:

Enter first polynomial.

Enter each term on a separate line; coefficient followed by power.

For example, 3 4 represents the term 3x^4.

Enter -1 -1 to end input for the polynomial.

9 0

4 5

7 -4

Error: cannot have negative power.

Please re-enter term and continue.

7 4

-1 2

-1 -1

First polynomial: 4x^5 + 7x^4 - x^2 + 9

----------------------------------------------------------------------------------------------------------------------

Note that terms do not have to be entered by the user in any particular order.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

What Is A Database And How Do I Use It

Authors: Matt Anniss

1st Edition

1622750799, 978-1622750795

More Books

Students also viewed these Databases questions

Question

How to find if any no. is divisble by 4 or not ?

Answered: 1 week ago

Question

Explain the Pascals Law ?

Answered: 1 week ago