Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding new problem statement of Polynomial based functions
- Loading branch information
Jarvis@LNMIIT
committed
Aug 13, 2014
1 parent
23eed03
commit 5fa8232
Showing
3 changed files
with
183 additions
and
0 deletions.
There are no files selected for viewing
137 changes: 137 additions & 0 deletions
137
Polynomial_Operations/Java/aayushKumarJarvis/Polynomial.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
import java.util.Scanner; | ||
|
||
public class Polynomial { | ||
|
||
int degreeOfPolynomial; | ||
double[] coefficients; | ||
|
||
public Polynomial() { | ||
|
||
} | ||
|
||
public Polynomial(int degree) { | ||
this.degreeOfPolynomial = degree; | ||
this.coefficients = new double[degree+1]; | ||
|
||
for(int loopingVariable = 0; loopingVariable<=getDegree(); loopingVariable++) { | ||
this.coefficients[loopingVariable] = 0; | ||
} | ||
} | ||
|
||
public int getDegree() { | ||
return degreeOfPolynomial; | ||
} | ||
|
||
|
||
public String toString() { | ||
|
||
String polynomialExpression = ""; | ||
int counter = 0; | ||
|
||
while(counter-1!=getDegree()) { | ||
|
||
if(counter == 0) | ||
polynomialExpression += "+" + polynomialExpression + coefficients[0]; | ||
else | ||
polynomialExpression = "+" + coefficients[counter] + "x^" + counter + polynomialExpression; | ||
|
||
counter++; | ||
|
||
} | ||
|
||
return polynomialExpression; | ||
} | ||
|
||
public double evaluate(double x) { | ||
|
||
double resultOfExpression = coefficients[0]; | ||
|
||
for(int loopVariable = 1; loopVariable<=getDegree(); loopVariable++) { | ||
resultOfExpression = resultOfExpression + coefficients[loopVariable]*Math.pow(x,loopVariable); | ||
} | ||
|
||
return resultOfExpression; | ||
} | ||
|
||
public static Polynomial add() { | ||
|
||
Scanner scannerObject = new Scanner(System.in); | ||
|
||
System.out.println("Enter degree of 1st polynomial"); | ||
int degree1 = scannerObject.nextInt(); | ||
System.out.println("Enter the degree of 2nd polynomial"); | ||
int degree2 = scannerObject.nextInt(); | ||
|
||
int smallerDegree = degree1<degree2 ? degree1:degree2; | ||
|
||
Polynomial objectForPolynomial1 = new Polynomial(degree1); | ||
Polynomial objectForPolynomial2 = new Polynomial(degree2) ; | ||
Polynomial objectForPolynomial3 = new Polynomial(smallerDegree); | ||
|
||
System.out.println("Enter the values of coefficients for 1st polynomial"); | ||
for(int loopingVariable = 0;loopingVariable<=degree1;loopingVariable++) | ||
objectForPolynomial1.coefficients[loopingVariable] = scannerObject.nextInt(); | ||
System.out.println("Enter the values of coeffcients for 2nd polynomial"); | ||
for(int loopingVariable = 0;loopingVariable<=degree2;loopingVariable++) | ||
objectForPolynomial2.coefficients[loopingVariable] = scannerObject.nextInt(); | ||
|
||
for(int loopingVariable = 0; loopingVariable<=smallerDegree;loopingVariable++) { | ||
objectForPolynomial3.coefficients[loopingVariable] = objectForPolynomial1.coefficients[loopingVariable] + objectForPolynomial2.coefficients[loopingVariable]; | ||
} | ||
|
||
return objectForPolynomial3; | ||
} | ||
|
||
public static Polynomial multiply() { | ||
|
||
Scanner scannerObject = new Scanner(System.in); | ||
int greaterDegree; | ||
int smallerDegree; | ||
|
||
System.out.println("Enter the degree of 1st polynomial"); | ||
int degree1 = scannerObject.nextInt(); | ||
System.out.println("Enter the degree of 2nd polynomial"); | ||
int degree2 = scannerObject.nextInt(); | ||
|
||
if(degree1>degree2) { | ||
greaterDegree = degree1; | ||
smallerDegree = degree2; | ||
} | ||
else { | ||
greaterDegree = degree2; | ||
smallerDegree = degree1; | ||
} | ||
|
||
Polynomial objectForPolynomial1 = new Polynomial(greaterDegree); | ||
Polynomial objectForPolynomial2 = new Polynomial(smallerDegree); | ||
Polynomial objectForPolynomial3 = new Polynomial(greaterDegree+smallerDegree); | ||
|
||
System.out.println("Enter the values of coefficients for 1st polynomial"); | ||
for(int loopingVariable = 0;loopingVariable<=greaterDegree;loopingVariable++) | ||
objectForPolynomial1.coefficients[loopingVariable] = scannerObject.nextInt(); | ||
System.out.println("Enter the values of coeffcients for 2nd polynomial"); | ||
for(int loopingVariable = 0;loopingVariable<=smallerDegree;loopingVariable++) | ||
objectForPolynomial2.coefficients[loopingVariable] = scannerObject.nextInt(); | ||
|
||
for(int loop1=0;loop1<=greaterDegree;loop1++) { | ||
for(int loop2=0;loop2<=smallerDegree;loop2++) { | ||
objectForPolynomial3.coefficients[loop1+loop2] = objectForPolynomial1.coefficients[loop1] * objectForPolynomial2.coefficients[loop2]; | ||
} | ||
} | ||
|
||
return objectForPolynomial3; | ||
} | ||
|
||
public static void main(String args[]) { | ||
|
||
System.out.println("Testing add function"); | ||
Polynomial obj = add(); | ||
System.out.println(obj.coefficients[0]); | ||
|
||
System.out.println("Testing multiply function"); | ||
obj.multiply(); | ||
System.out.println(obj.coefficients[0]); | ||
|
||
} | ||
} | ||
|
45 changes: 45 additions & 0 deletions
45
Polynomial_Operations/Java/aayushKumarJarvis/PolynomialTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import org.junit.Test; | ||
import static org.junit.Assert.*; | ||
|
||
public class PolynomialTest extends Polynomial { | ||
|
||
@Test | ||
public void getDegreeTest() { | ||
|
||
Polynomial objectForPolynomial = new Polynomial(5); | ||
assertEquals(objectForPolynomial.degreeOfPolynomial,5); | ||
} | ||
|
||
@Test | ||
public void toStringTest() { | ||
|
||
Polynomial objectForPolynomial = new Polynomial(5); | ||
|
||
objectForPolynomial.coefficients[0] = 1.0; | ||
objectForPolynomial.coefficients[1] = 2.0; | ||
objectForPolynomial.coefficients[2] = 3.0; | ||
objectForPolynomial.coefficients[3] = 4.0; | ||
objectForPolynomial.coefficients[4] = 5.0; | ||
objectForPolynomial.coefficients[5] = 6.0; | ||
|
||
String result = objectForPolynomial.toString(); | ||
|
||
assertEquals(result,"+6.0x^5+5.0x^4+4.0x^3+3.0x^2+2.0x^1+1.0"); | ||
} | ||
|
||
@Test | ||
public void evaluateTest() { | ||
|
||
Polynomial objectForPolynomial = new Polynomial(2); | ||
|
||
objectForPolynomial.coefficients[0] = 1.0; | ||
objectForPolynomial.coefficients[1] = 2.0; | ||
objectForPolynomial.coefficients[2] = 3.0; | ||
|
||
double result = objectForPolynomial.evaluate(2.0); | ||
|
||
assertEquals((int)result,17); | ||
} | ||
|
||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Addition, Multiplication, Degree Finding of Polynomials. |