// polymult.java // // // This program read an input n from the user. // Then, it either reads or randomly generates 2 polynomials, // each with n coefficients // It multiplies these 2 polynomials. // Finally, it prints out the result of the multiplication. // // // A polynomial with n coefficient is represented as // an array of n integers. // // // The program contains the following subroutines: // // public static void polyread( int [] p, int n, BufferedReader br) // throws IOException // This function reads in n coefficients from the user for // polynomial p // // public static void polyrand( int [] p, int n) // This function creates random coefficients for polynomial p, // which has n coefficients. // // public static void polyprint( int [] p, int n) // This function prints the coefficients of the polynomial on the screen // starting with the coefficient of the monomial of degree 0. // // public static void polymult( int [] p, int [] q, int [] r, int n) // This function multiplies polynomials p and q, each has n coefficients // and stores the result in r. // import java.io.*; import java.util.*; public class polymult { public static void main(String [] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int [] a, b, c; int n; System.out.println("Enter N: "); n = Integer.parseInt(br.readLine()); a=new int[n]; b=new int[n]; if (n <= 10) { polyread(a,n,br); // read the first polynomial polyread(b,n,br); // read the second polynomial } else { polyrand(a,n); // random first polynomial polyrand(b,n); // random second polynomial } c=new int[2*n-1]; polymult(a,b,c,n); // multiplying the polynomials // printing out the result System.out.println(" The coefficients of the first polynomial are"); polyprint(a,n); System.out.println(" The coefficients of the second polynomial are"); polyprint(b,n); System.out.println(" The coefficients of the resulting polynomial are"); polyprint(c,2*n-1); } public static void polyread( int [] p, int n, BufferedReader br) throws IOException { System.out.println("Please enter the " + n + " coefficients, sep. by spaces"); StringTokenizer S = new StringTokenizer(br.readLine()); for (int i = 0; i < n; i++) { p[i] = Integer.parseInt(S.nextToken()); } } public static void polyrand( int [] p, int n) { for( int i=0; i