## Question

The second programming project involves writing a program that examines a file of polynomials and determines whether the polynomials in that file are in strictly ascending order using two different methods of comparison.

Each line of the input file will contain one polynomial. On each line will be the terms of the polynomial. Each term will be represented as a pair of values. The first element of that pair is a real value that represents the coefficient and the second an integer value, which is its corresponding exponent. For example, 5.6 3 4 1 8.3 0 represents the polynomial 5.6x³ + 4x + 8.3. They are intended to be written from the highest exponent to the lowest, but ensuring that is true is a program requirement. Exponents with zero coefficients will be omitted

The program for this project should consist of four outer classes. The Polynomial class is the first of those four. Instances of the Polynomial class should define an individual polynomial. Polynomial objects should be represented internally by a singly linked list. Each node of that linked list should contain one term of the polynomial consisting of its coefficient and exponent. You are not permitted to use the predefined Java

LinkedList class, but instead must create the nodes of the linked list as instances of a static nested class inside the Polynomial class. The Polynomial class must implement both the Iterable and Comparable interfaces. It must have four public methods.

1. A constructor that accepts a string that defines one polynomial in the same format as provided in the input file.

2. A compareTo method that compares two polynomials. If the two polynomials have different highest order exponents, the one with the highest exponent is the greatest. If their highest exponents are the same, their coefficients are compared. If two polynomials have the same highest order exponent with the same coefficients the next highest exponent is examined, and so on.

3. An iterator method that produces an iterator the iterates across the terms of the polynomial from highest exponent to lowest and returns and an object that contains only the coefficient and exponent of the next term.

4. A toString method that converts a polynomial to a string. Terms with 0 coefficients should be omitted entirely. The exponent of the term with an exponent of 1 should omit the exponent and the term with exponent 0 should omit the variable x as well. As an example, the polynomial “5.6 3 4 1 8.3 0” should be converted to the following string "5.6x³ + 4x + 8.3".

The second class is InvalidPolynomialSyntax, which defines an unchecked exception that contains a constructor that allows a message to be supplied. It is thrown by the constructor of the Polynomial class should the supplied string contain coefficients or exponents of an improper type or should the exponents fail to be listed in strictly descending order.

The third class is OrderedList, which is a utility class that contains two overloaded implementations of a method named checkSorted, which determines whether a List object, supplied as a parameter, is in strictly ascending order. Both methods should be class (static) methods. The first of the overloaded methods should accept a list that contains elements that implement Comparable. The second should instead be supplied an additional parameter that is an object of a class that implements the Comparator interface. Refer to the signatures of the two sort methods in the predefined Java Collections class as a model for how these two methods should be defined. Do not duplicate code, but instead ensure that first overloaded method calls the second..

The fourth outer class is the class that contains the main method. The main method should allow the user to select the input file from the default directory by using an object of the JFileChooser class. It should then populate an ArrayList of objects of type Polynomial as it reads in the lines of the file. As the polynomials are read in, they should also be displayed in the format provided by the toString method. Should the InvalidPolynomialSyntax exception be thrown, it should be caught and a JOptionPane message should be displayed containing the reason for the invalid syntax. The list of polynomials should be then checked to see whether it is in sorted order according to two orderings. The first check is the one that uses the compareTo method of the Polynomial class. We refer to this first ordering as the strong order. The second results from the use of a comparator. We refer to it as the weak order. It should display whether it fails to be sorted by either of both of the two orderings or if it is sorted according to both.

Inside the main class, a named lambda expession should be defined that implements the Comparator interface. Its compare method should consider only the exponents and not the coefficients in comparing the polynomials, much like how functions are categorized by Big-O.

As an example, consider the following list of polynomials:

4.0x³ + 2.5x + 8.0

5.0x⁴ + 5.0

4.5x⁴ + 5.7x² + 8.6

This list fails to be sorted by the strong order that considers both the coefficients and exponents because the third polynomial is less than the second because of the coefficients on the x⁴ terms. The coefficient on that term in the second polynomial is 5.0, which is greater than 4.5, the coefficient on the corresponding term in the third polynomial.

They are sorted by the weak order that considers only the exponents, however. The third polynomial is greater than the second because the third has a x² term and the second one does not.

You are to submit two files.

1. The first is a .zip file that contains all the source code for the project. The .zip file should contain only source code and nothing else, which means only the .java files. If you elect to use a package the .java files should be in a folder whose name is the package name. Every outer class should be in a separate .java file with the same name as the class name. Each file should include a comment block at the top containing your name, the project name, the date, and a short description of the class contained in that file.

2. The second is a Word document (PDF or RTF is also acceptable) that contains the documentation for the project, which should include the following:

a. A UML class diagram that includes all classes you wrote. Do not include predefined classes. You need only include the class name for each individual class, not the variables or methods

b. A test plan that includes test cases that you have created indicating what aspects of the program each one is testing

c. A short paragraph on lessons learned from the project.

## Solution Preview

These solutions may offer step-by-step problem-solving explanations or good writing examples that include modern styles of formatting and construction of bibliographies out of text citations and references. Students may use these solutions for personal skill-building and practice. Unethical use is strictly forbidden.

import javax.swing.*;import java.io.BufferedReader;

import java.io.FileReader;

import java.io.IOException;

import java.util.*;

/**

* Main class implementing Comparator interface.

* Hence it implements the compare method

* to compare the two instances of polynomial

* expressions.

*/

public class P2GUI implements Comparator<Polynomial> {

public static void main(String[] args) {

List<Polynomial> polynomials = new ArrayList<>();

// select the input file

JFileChooser jFC = new JFileChooser();

int result = jFC.showOpenDialog(null);

String filePath = null;

if (result == JFileChooser.APPROVE_OPTION) {

filePath = jFC.getSelectedFile().getAbsolutePath();

// read the input file line by line and

try {

BufferedReader reader = new BufferedReader(new FileReader(filePath));

String line;...

By purchasing this solution you'll be able to access the following files:

Solution.docx and Solution.zip.