I can't seem to get this program working. I was asked to write a program that merges two files that contain polynomials. To merge two files, the input files must be in sorted order. The merge operation repetitively selects the smaller value from the two files. When two sorted files are merged, the result will be sorted.

This program should contain three classes. The first class Project1 should contain the two methods described below:

1. The main method, which calls the checkSorted method twice to verify that each of the input files is sorted in ascending order. It should catch the exception FileUnsorted if it is thrown, display an error message and terminate the program. Otherwise, it then performs the merge operation. The output is to be displayed on the screen.

2. A class method called checkSorted, which returns a BufferedReader object. The user should supply the name of the input file. The file should be read and it should be verified that the file is in ascending order. If it is not, the exception FileUnsorted should be thrown. Otherwise, after the file has been completely read, it should be closed and reopened to position it at the beginning of the file.

The second class should be a class named Polynomial. It should implement the Comparable interface. The polynomial should be stored as an array of integers. The size of the array should be one more than the highest exponent. This class should contain the following methods.

1.A constructor.

2.A class method named input that reads in a polynomial. The buffered reader for the file should be passed to this method as a parameter. The polynomials in the file will be stored as integer pairs that represent a coefficient and its corresponding exponent. For example, 5 3 4 1 8 0 represents the polynomial 5x3 + 4x + 8. They will always be written from the highest exponent to the lowest. Exponents with zero coefficients will be omitted.

3.A toString method that converts a polynomial to a string. The polynomial 5x3 + 4x + 8 should be converted to the following string "5x^3 + 4x + 8".
4.A method compareTo that compares two polynomials. The comparison should be similar to the Big-O comparison of methods. 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. The following examples should help explain the ordering:

6x4 + 7x2 + 3 > 20x3 + 8x2 + 10
7x4 + 7x2 + 3 > 6x4 + 17x2 + 7x + 30
6x4 + 9x2 + 3 > 6x4 + 7x +9

The third class should be the exception FileUnsorted. It requires no methods or instance data.

The code i have worked out so far:

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.Scanner;

public class Project1{
      public static void main(String[]args) throws IOException {
                 // call checkSorted() on both files
                Scanner scanner = new Scanner(System.in);

                System.out.println ("Please enter your first file's name:");
                String Input1 = scanner.nextLine();
                System.out.println ("Please enter your second file's name:");
                String Input2 = scanner.nextLine();

                	BufferedReader fileA = new BufferedReader(new FileReader(Input1));
                	BufferedReader fileB = new BufferedReader(new FileReader(Input2));
                catch(FileUnsorted ex)
                 // display error
      public static BufferedReader checkSorted(BufferedReader file) throws FileUnsorted, IOException {

            Polynomial p1 = null, p2;
            String line;

            //while a new polynomial is in file
            while((line =file.readLine())!=null)
                  p2= new Polynomial(line);

                       // compare p1 andp2
                       if(p1.compareTo(p2) >0)
                             throw new FileUnsorted();

                  p1= p2;


            //by induction we only need to compare the last 2 elements

            //return a new reader
            return new BufferedReader(file);
import java.io.BufferedReader;
import java.io.IOException;
import java.util.Scanner;

// Polynomial class

public class Polynomial implements Comparable<Polynomial>{
// class methods

	public Polynomial input(BufferedReader file) throws IOException
            return new Polynomial(file.readLine());

      private int[] coef;  // Array of coefficients
      private int deg;     // degree of polynomial (0 for the zero polynomial)

      public Polynomial(String input){
    	  Scanner line =new Scanner(input);

            int coefficient =line.nextInt();
            int deg =line.nextInt();
            //set array size
            coef = new int[deg+1];
            coef[deg] = coefficient;
            deg = degree();
                 coefficient = line.nextInt();
                 deg = line.nextInt();

                 coef[deg] =coefficient;

      // return the degree of this polynomial (0 for the zero polynomial)
      public int degree() {
          int d = 0;
          for (int i = 0; i < coef.length; i++)
              if (coef[i] != 0) d = i;
          return d;
      // convert to string representation
      public String toString() {
          if (deg ==  0) return "" + coef[0];
          if (deg ==  1) return coef[1] + "x + " + coef[0];
          String s = coef[deg] + "x^" + deg;
          for (int i = deg-1; i >= 0; i--) {
              if      (coef[i] == 0) continue;
              else if (coef[i]  > 0) s = s + " + " + ( coef[i]);
              else if (coef[i]  < 0) s = s + " - " + (-coef[i]);
              if      (i == 1) s = s + "x";
              else if (i >  1) s = s + "x^" + i;
          return s;
      // Compares two polynomials   
      public int compareTo(Polynomial rhs){
            //check degree
            if(coef.length !=rhs.coef.length)
                 return rhs.coef.length- coef.length;

            //else check coefficients
            for(int i= coef.length-1; i>=0; i--)
                 if(coef[i] !=rhs.coef[i])
                       return rhs.coef[i] -coef[i];

            return 0;

public class FileUnsorted extends RuntimeException

	public FileUnsorted()
            System.out.println("File contains unsorted polynomials");

Is this a java coding problem or a math logic problem?

If you are having problems coding an algorithm in java, please post the algorithm and explain where your problem coding it is.

I don't know where you can go to get any math help.

An example would help: Show the contents of two small files, and what the merged output should look like and explain what the steps are to determine how to create the output file.

This article has been dead for over six months. Start a new discussion instead.