I know this works but I want to improve this code! I don't expect anyone to re-write this but if you have some "try this instead" then great!
import java.util.*;
public class Polynomial implements Iterable<Object>, Comparable<Polynomial> {
Comparator<Polynomial> compare;
private Term head = null;
//A constructor that accepts a string that defines one polynomial in the same format
//as provided in the input file.
public Polynomial(String file) {
Scanner scan = new Scanner(file);
try{
while(scan.hasNext()){
addTerm(scan.nextDouble(), scan.nextInt());
}
}
catch (Exception ex){
System.out.println(ex.getLocalizedMessage());
throw new InvalidPolynomialSyntax("Error: Invalid Polynomial Syntax, check input");
}
}
public void addTerm(double coefficient, int exponent ){
if (exponent < 0){
throw new InvalidPolynomialSyntax("Error: Invalid Polynomial Syntax, Negative exponents, check input");
}
Term current = head;
if(current == null){
head = new Term(coefficient, exponent);
head.next = null;
}
else {
while(current.next != null){
current = current.next;
}
current.next = new Term(coefficient, exponent);
}
}
/*
* A compareTo method that compares two polynomials. 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.
*/
@Override
public int compareTo(Polynomial otherPoly) {
Term thisCurrent = this.head;
Term otherCurrent = otherPoly.head;
while (thisCurrent != null && otherCurrent != null){
//If the two polynomials have different highest
//order exponents, the one with the highest exponent is the greatest.
if (thisCurrent.getExponent() != otherCurrent.getExponent()){
return thisCurrent.getExponent() - otherCurrent.getExponent();
}
else if(thisCurrent.getCoefficient() != otherCurrent.getCoefficient()) {
if(otherCurrent.getCoefficient()> thisCurrent.getCoefficient()){
return -1;
}
else if(otherCurrent.getCoefficient()< thisCurrent.getCoefficient()){
return +1;
}
}
thisCurrent = thisCurrent.getNext();
otherCurrent = otherCurrent.getNext();
}//if both are null they are equal
if (thisCurrent == null && otherCurrent == null){
return 0;
}//If two polynomials have the same highest order exponent with the
//same coefficients the next highest exponent is examined, and so on.
if (thisCurrent == null){
return -1;
}else {
return +1;
}
}
//If the exponents are the same...
public int compareExponents(Polynomial poly2) {
Term thisPolyTerm = this.head;
Term otherPolyTerm = poly2.head;
while(thisPolyTerm != null && otherPolyTerm != null) {
if (thisPolyTerm.getExponent() != otherPolyTerm.getExponent()) {
return thisPolyTerm.getExponent() - otherPolyTerm.getExponent();
}
else {
thisPolyTerm = thisPolyTerm.getNext();
otherPolyTerm = otherPolyTerm.getNext();
}
}
if(thisPolyTerm == null && otherPolyTerm == null){
return 0;
}
if (otherPolyTerm == null){
return +1;
}
else {
return -1;
}
}
public Polynomial() {
compare = (Polynomial poly1, Polynomial poly2) -> poly1.compareExponents(poly2);
}
public Polynomial(Comparator<Polynomial> compare){
this.compare = compare;
}
@Override
public Iterator<Object> iterator() {
return new Iterator() {
private Term current = getHead();
@Override
public boolean hasNext() {
return current != null && current.getNext() != null;
}
@Override
public Term next() {
Term data = current;
current = current.next;
return data;
}
};
}
@Override
public String toString() {
StringBuilder expressionBuilder = new StringBuilder();
//first check head to avoid +1x^3 +3x^2
if (head.coefficient > 0){
expressionBuilder.append(head.toString());
}
else {
expressionBuilder.append(" - ").append(head.toString());
}
for(Term tmp = head.next; tmp != null; tmp = tmp.next) {
if (tmp.coefficient < 0) {
expressionBuilder.append(" - ").append(tmp.toString());
}
else {
expressionBuilder.append(" + ").append(tmp.toString());
}
}
return expressionBuilder.toString();
}
static class Term{
private double coefficient;
private int exponent;
private Term next;
private Term(double c, int e) {
coefficient = c;
exponent = e;
next = null;
}
private int getExponent(){
return this.exponent;
}
private double getCoefficient(){
return this.coefficient;
}
private Term getNext(){
return next;
}
@Override
public String toString(){
String termString = String.format("%.1f", Math.abs(coefficient));
if (exponent == 0) {
return termString;
}
else if(exponent == 1){
return termString + "x";
}
else{
return termString + "x^" + exponent;
}
}
}
private Term getHead() {
return head;
}
}
I don't think this part needs anything changed to it but open to suggestions(mainly providing this so the code compiles and runs)
OrderedList.Java
import java.util.*;
/*
* The third class is OrderedList, which is a utility class
* that contains two overloaded implementations
*/
public class OrderedList {
//Both methods should be class (static) methods.
//The first of the overloaded methods should accept a list
//that contains elements that implement Comparable.
public static <T extends Comparable<? super T>>
//two overloaded implementations of a method named checkSorted
boolean checkSorted(List<T> list){
boolean isSorted = true;
for(int i = list.size()-1; i > 0 ; i--){
T current = list.get(i);
if(!checkSorted(list, current)){
isSorted = false;
}
}
return isSorted;
}
//The second should instead be supplied an additional
//parameter that is an object of a class that implements
//the Comparator interface
private static <T extends Comparable<? super T>>
boolean checkSorted(List<T> list, T current) {
T currentValue = list.get(list.indexOf(current));
T nextValue = list.get(list.indexOf(current) - 1);
if (nextValue != null) {
return currentValue.compareTo(nextValue) >= 0;
}
return true;
}
}
Please ignore my notes, ha ha Main.java
import javax.swing.*;
import java.io.*;
import java.util.*;
public class Main {
private static List<Polynomial> polyList = new ArrayList<>();
public static void main(String[] args) {
processPolyList();
}
public static ArrayList<String> fromFile() {
//Create ArrayList and JFileChooser
ArrayList<String> expressionList = new ArrayList<>();
JFileChooser chooser = new JFileChooser();
//Show directories and files
chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
//user's current directory
chooser.setCurrentDirectory(new File(System.getProperty("user.dir")));
int response = chooser.showOpenDialog(null);
if (response == JFileChooser.APPROVE_OPTION){
File file = chooser.getSelectedFile();
try {
Scanner scan = new Scanner(file);
if (file.isFile()){
while (scan.hasNextLine()){
String expression = scan.nextLine();
expressionList.add(expression);
}
}
}
catch (NoSuchElementException a){
JOptionPane.showMessageDialog(JOptionPane.getRootFrame(),"File is empty!");
}
catch(FileNotFoundException b){
JOptionPane.showMessageDialog(JOptionPane.getRootFrame(),"404 File Not Found!");
}
}
return expressionList;
}
public static boolean checkWeakOrder( List<Polynomial> polyList){
boolean isWeakOrder = true;
Polynomial previous = polyList.get(polyList.size()-1);
for(int i = polyList.size()-2; i > 0; i--){
if (previous.compareExponents(polyList.get(i)) < 0){
isWeakOrder = false;
}
}
return isWeakOrder;
}
public static void processPolyList(){
try {
ArrayList<String> a = fromFile();
for (String element : a) {
Polynomial p = new Polynomial(element);
System.out.println(p);
polyList.add(p);
}
}
catch (InvalidPolynomialSyntax ex){
JOptionPane.showMessageDialog(JOptionPane.getRootFrame(),ex.getMessage());
}
System.out.println("This list is sorted by the strong order: " + OrderedList.checkSorted(polyList));
System.out.println("This list is sorted by the weak order: " + checkWeakOrder(polyList));
}
}
InvalidPolynomialSyntax.java
/*
* 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.
*/
public class InvalidPolynomialSyntax extends RuntimeException {
InvalidPolynomialSyntax(String message){
super(message);
}
}