import java.util.*;
// This program was made by Erdogan Akbiyik
/* Project 5 Polynomial Program - This program was created in purpose to find the solution of the mathematical operations.
*/
import java.lang.Math;
// In this program, we will use the result of the given function values.
// We write the methods according to the given values.
class Polynomial {
private double[] coeff;
private int degree;
public Polynomial() {
degree = 0;
coeff = new double[degree+1];
coeff[0] = 0;
}
public Polynomial(String coeff) {
setCoeff(coeff);
}
public double[] getCoeff() {
return coeff;
}
public int getDegree() {
return degree;
}
public void setCoeff(String coeff) {
String[] arr = coeff.split(" ");
this.degree = arr.length-1;
this.coeff = new double[degree+1];
for(int i=0;i<arr.length;i++)
{
double d = Double.parseDouble(arr[i]);
this.coeff[i] = d;
}
}
public Polynomial add(Polynomial p)
{
Polynomial r = new Polynomial();
r.degree = (this.degree < p.degree) ? p.degree : this.degree;
r.coeff = new double[r.degree+1];
for(int i = 0; i<r.coeff.length;i++) {
if(i<this.coeff.length && i<p.coeff.length)
{
r.coeff[i] = this.coeff[i] + p.coeff[i];
}
else if(i<this.coeff.length)
{
r.coeff[i] = this.coeff[i];
}
else if(i<p.coeff.length)
{
r.coeff[i] = p.coeff[i];
}
}
return r; // returning the value if its in the current r of Polynomial
}
public String toString()
{
String r = "f(x) = " + this.coeff[0];
for(int i=1;i<this.coeff.length;i++)
{
if(this.coeff[i]>=0) r+= "+";
r += this.coeff[i] + "x^"+i;
}
return r;
}
public double evaluate(double x)
{
double r = 0;
for(int i=0;i<this.coeff.length;i++)
{
r+= Math.pow(x,i)*this.coeff[i];
}
return r; // returning the value if its in the current r of evaluate
}
public Polynomial derivative()
{
Polynomial r = new Polynomial();
r.degree = this.degree-1;
r.coeff = new double[r.degree+1];
for(int i=0;i<r.coeff.length;i++)
{
r.coeff[i] = this.coeff[i+1]*(i+1);
}
return r; // returning the value if its in the current r of Polynomial derivative
}
public double findRoot(double x)
{
Polynomial deriv = this.derivative();
double h = this.evaluate(x) / deriv.evaluate(x);
while (Math.abs(h) >= 0.00001)
{
h = this.evaluate(x) / deriv.evaluate(x);
x = x - h;
}
return x; // returning the value if its in the current x of Root
// Thanks for using my program!!!
}
}
//Test Driver for polynomial project
//As you build your class comment out parts of the switch
//so that you can check each case
//Build the constructors and the toString methods first
//Build stub methods for others not working yet
import java.util.Scanner;
class PolynomialDriver {
public static void main(String args []) {
Scanner input = new Scanner(System.in);
System.out.println("##Constructor Tests##");
//Test default constructor
Polynomial testValue1 = new Polynomial();
//Test toString method
System.out.println("Your test polynomial is " + testValue1); //should be f(x) = 0.0
//Test constructor
Polynomial testValue2 = new Polynomial("0 1 -3 0 1 2 0 0");
System.out.println("Your 2nd test polynomial is " + testValue2);
System.out.println("It's degree is : " + testValue2.getDegree()); // should be 5
System.out.println();
//Loop to repeat menu until quit is chosen
int choice = 0;
while (choice != 7) {
choice = menu();
switch(choice) {
case 1:
System.out.println("Display Polynomials");
System.out.println("#1 : " + testValue1);
System.out.println("#2 : " + testValue2);
System.out.println();
break;
case 2: //input values with single spaces between values
System.out.println("Input New Polynomials");
String coeff;
System.out.print("Enter values for first Polynomial :");
coeff = input.nextLine();
testValue1.setCoeff(coeff);
System.out.println();
System.out.print("Enter values for second Polynomial :");
coeff = input.nextLine();
testValue2.setCoeff(coeff);
System.out.println();
break;
case 3: //call add method
System.out.println("Add Polynomials");
System.out.println("The Sum of \t" + testValue1);
System.out.println(" and \t" + testValue2);
System.out.println(" is : \t" + testValue1.add(testValue2));
System.out.println();
break;
case 4:
System.out.println("Evaluate Polynomial #1");
System.out.println("Enter a value for x to evaluate f(x) for Polynomial #1");
double value;
value = input.nextDouble();
input.nextLine(); //empty the buffer
System.out.println("When x = " + value + " for " + testValue1);
System.out.println("f(x) = " + testValue1.evaluate(value));
System.out.println();
break;
case 5:
System.out.println("Derivative of Polynomial #1");
System.out.println("The derivative of " + testValue1);
System.out.println("is : " + testValue1.derivative());
System.out.println();
break;
case 6:
System.out.println("Find Root of Polynomial #1");
System.out.println("Enter a value to be the initial guess for a root as");
System.out.println("needed to perform the Newton-Raphson method : ");
value = input.nextDouble();
input.nextLine(); //empty the buffer
System.out.println("Root of " + testValue1);
System.out.println("found at x = " + testValue1.findRoot(value));
System.out.println();
break;
case 7:
System.out.println("I hope that you enjoyed this program!");
}
}// end while
}
//Method to call menu
//Exceptions not set up here. Only integers will work
public static int menu() {
int choice = 0;
Scanner input = new Scanner(System.in);
while(choice < 1 || choice > 7) {
System.out.println("Please choose from the following: ");
System.out.println("\t#1 Display two polynomials stored");
System.out.println("\t#2 Input two new polynomials");
System.out.println("\t#3 Add polynomials");
System.out.println("\t#4 Evaluate polynomial #1 with a specific value");
System.out.println("\t#5 Show Derivative of polynomial #1");
System.out.println("\t#6 Find root of polynomial #1 with initial guess");
System.out.println("\t#7 End program");
System.out.print("Choice : ");
choice = input.nextInt();
System.out.println();
}
return choice;
}
} //end class
http://erdoganakbiyik.blogspot.com/
https://www.youtube.com/channel/UCDsUnmBfVdEPkcC8FlzPKcg
No comments:
Post a Comment