Home        Next       Previous

/*

Description: Define a class in which the objects represent 
a polynomial with real (type double) coefficients. 
Use an array to store the coefficients as follows: 
coef[0] is the constant term(degree 0 coefficient), 
coef[1] is the coefficient of x(degree 1 coefficient), 
coef[2] is the coefficient of x^2(degree 2), 
and so on to the lead coefficient coef[n] the coefficient
for the x^n term (for a Polynomial object of degree n). 
Write a main program to input a Polynomial object. 
Display it, compute its Antiderivative and dispaly it. 
Input two double valuew (a & b), the limits of a definite 
integral, and compute and display the definite integral
of the input polynomial between the given limits. 
That is: Given the input polynomial p(x), compute 
p(x) a, b = P(b)-P(a) Where p(x) is the antiderivative 
polynomial of p(x), P(a) and P(b) are the evaluations 
of the antiderivative at the limits a and b
(using the Evaluate(v) method).
*/
public class Polynomial
{
private int degree;
private double[] coef;

public Polynomial(int d)
{ //allocate the coef array
coef = new double [d + 1]; 
zeroCoef();
degree = d;
}

public void zeroCoef()
{
for(int i =0; i <=degree; i++)
coef[i] = 0.0;
}
public int getDegree()
{
return degree;
}

public double getCoef(int t)
{
return coef[t]; // 0 <= t <= degree
}

public void setCoef (double v, int t)
{
coef[t] = v;
}

public void readPoly()
{
System.out.println("Enter the coefficients in descending"+
" order (including zeros).");
for (int i = 0; i <= degree; i++)
coef[i] = MyInput.readDouble();
}

public String toString()
{
String p = " ";
int q = 0; //restores degree size

for (int i = 0; i < coef.length; i++)

if(degree == 1)
{
p+=(coef[i] + "x" + " + ");
}
else if(degree == 0)
{
p+=(coef[i]);
}
else
{
p+= (coef[i] + "x^" + degree + " " + "+ ");
}
degree--;
q++;
}
degree = degree + q; 
return p;
}

public Polynomial Derivative()
{
int q = 0;
int k = degree;
if(k == 1)
{
Polynomial d = new Polynomial(0);
d.coef[0] = coef[0];
return d; 
}
else if(k ==0)
{
Polynomial d = new Polynomial(0);
return d; 
}
else
{
Polynomial d = new Polynomial(k-1);
for (int i = 0; i < coef.length - 1; i++)

d.coef[i] = coef[i] * (k);
k--;
}
//for (int i = 0; i < coef.length - 1; i++)
//degree++; 
return d; 

}

public Polynomial Antiderivative()
{
Polynomial a = new Polynomial(degree+1);
degree++;
for(int i = 0; i < coef.length; i++)
{
a.coef[i] = (coef[i]/(degree));
degree--;
}
for(int i = 0; i <coef.length; i++)
degree++;
degree--;
return a; 
}

public double EvaAnti(double x)

double s = 0;
for(int i = 0; i < coef.length; i++)
{
s += (coef[i]) * (Math.pow(x,degree));
degree--;
}
for(int i = 0; i < coef.length; i++)
degree++;
return s;
}
}

/*________________________________*/

public class Main
{
public static void main(String[] args)
{
int size = 0;
double upper, lower, definiteInt, EvaDer;
System.out.print("Enter degree: ");
size = MyInput.readInt(); 

Polynomial poly = new Polynomial(size);
poly.readPoly(); //gets coefficients
System.out.println("The polynomial in desending order is:"); 
System.out.println(poly);
System.out.println();

Polynomial dpoly = new Polynomial(size); 
dpoly = poly.Derivative();
System.out.println("The derivative of the polynomial is:");
System.out.println(dpoly);
System.out.println();

Polynomial apoly = new Polynomial(size);
apoly = poly.Antiderivative();
System.out.println("The Antiderivative of the polynomial is:");
System.out.println(apoly);
System.out.println();

System.out.print("Enter the upper limit: ");
upper = MyInput.readDouble();

System.out.print("Enter the lower limit: ");
lower = MyInput.readDouble();

definiteInt = apoly.EvaAnti(upper) - apoly.EvaAnti(lower);

System.out.println("The evaluation from " + lower + " to "
+ upper + " is: " + definiteInt); 
}
}
/*
Enter degree: 0
Enter the coefficients in descending order (including zeros).
4
The polynomial in desending order is:
4.0

The derivative of the polynomial is:
0.0

Evaluate the polynomial by entering x:
1
The evaluation is: 4.0
The Antiderivative of the polynomial is:
4.0x + 0.0

Enter the upper limit: 5
Enter the lower limit: -3
The evaluation from -3.0 to 5.0 is: 32.0

A:\>java Main
Enter degree: 2
Enter the coefficients in descending order (including zeros).
3
-5
2
The polynomial in desending order is:
3.0x^2 + -5.0x + 2.0

The derivative of the polynomial is:
6.0x + -5.0

Evaluate the polynomial by entering x:
2
The evaluation is: 1.0
The Antiderivative of the polynomial is:
1.0x^3 + -2.5x^2 + 2.0x + 0.0

Enter the upper limit: 3
Enter the lower limit: 1
The evaluation from 1.0 to 3.0 is: 10.0

A:\>java Main
Enter degree: 2
Enter the coefficients in descending order (including zeros).
3
2
1
The polynomial in desending order is:
3.0x^2 + 2.0x + 1.0

The derivative of the polynomial is:
6.0x + 2.0

Evaluate the polynomial by entering x:
2
The evaluation is: 11.0
The Antiderivative of the polynomial is:
1.0x^3 + 1.0x^2 + 1.0x + 0.0

Enter the upper limit: 3
Enter the lower limit: 1
The evaluation from 1.0 to 3.0 is: 36.0

A:\>java Main
Enter degree: 1
Enter the coefficients in descending order (including zeros).
2
0
The polynomial in desending order is:
2.0x + 0.0

The derivative of the polynomial is:
2.0

Evaluate the polynomial by entering x:
2
The evaluation is: 2.0
The Antiderivative of the polynomial is:
1.0x^2 + 0.0x + 0.0

Enter the upper limit: 5
Enter the lower limit: 3
The evaluation from 3.0 to 5.0 is: 16.0

A:\>java Main
Enter degree: 2
Enter the coefficients in descending order (including zeros).
6
-4
1
The polynomial in desending order is:
6.0x^2 + -4.0x + 1.0

The derivative of the polynomial is:
12.0x + -4.0

Evaluate the polynomial by entering x:
2
The evaluation is: 2.0
The Antiderivative of the polynomial is:
2.0x^3 + -2.0x^2 + 1.0x + 0.0

Enter the upper limit: 2
Enter the lower limit: 1
The evaluation from 1.0 to 2.0 is: 9.0

A:\>java Main
Enter degree: 3
Enter the coefficients in descending order (including zeros).
1
0
0
1
The polynomial in desending order is:
1.0x^3 + 0.0x^2 + 0.0x + 1.0

The derivative of the polynomial is:
3.0x^2 + 0.0x + 0.0

Evaluate the polynomial by entering x:
2
The evaluation is: 9.0
The Antiderivative of the polynomial is:
0.25x^4 + 0.0x^3 + 0.0x^2 + 1.0x + 0.0

Enter the upper limit: 2
Enter the lower limit: -2
The evaluation from -2.0 to 2.0 is: 4.0

A:\>
*/_______________________*/

/**
* Title: "Input and Output"
* Description: 
MyInput.java: Contain the methods for reading int, double, and
string values from the keyboard
*/
import java.io.*;

public class MyInput
{
/**Read a string from the keyboard*/
public static String readString()
{
BufferedReader br
= new BufferedReader(new InputStreamReader(System.in), 1);

// Declare and initialize the string
String string = " ";

// Get the string from the keyboard
try
{
string = br.readLine();
}
catch (IOException ex)
{
System.out.println(ex);
}

// Return the string obtained from the keyboard
return string;
}

/**Read an int value from the keyboard*/
public static int readInt()
{
return Integer.parseInt(readString());
}

/**Read a double value from the keyboard*/
public static double readDouble()
{
return Double.parseDouble(readString());
}

/**Read a byte value from the keyboard*/
public static byte readByte()
{
return Byte.parseByte(readString());
}

/**Read a short value from the keyboard*/
public static short readShort()
{
return Short.parseShort(readString());
}

/**Read a long value from the keyboard*/
public static long readLong()
{
return Long.parseLong(readString());
}

/**Read a float value from the keyboard*/
public static float readFloat()
{
return Float.parseFloat(readString());
}
}
Home        Next       Previous

 

New Page 1