Skip to content

Instantly share code, notes, and snippets.

@bowmana
Last active February 18, 2020 01:19
Show Gist options
  • Save bowmana/531c79b1ce341346e5a8fd468239ea0a to your computer and use it in GitHub Desktop.
Save bowmana/531c79b1ce341346e5a8fd468239ea0a to your computer and use it in GitHub Desktop.
/*Alex Bowman
* HW2 Polynomials
* Prof. Silvestri
* 2/17/20
*/
package hw2;
public class Poly {
private double coeff[];
public Poly() {
coeff = new double[0]; // degree -1
}
public int getDegree() {
return this.coeff.length - 1;
}
public double getCoeff(int n) {
return (n >= 0 && n <= this.getDegree()) ? this.coeff[n] : 0.0;
}
public void setCoeff(int n, double value) {
if (n < 0)
throw new ArithmeticException("Negative power: " + n);
if (n > this.getDegree()) {
// Make the array bigger!
double temp[] = new double[n + 1];
// Copy existing coefficients into temp
for (int i = 0; i <= this.getDegree(); i++)
temp[i] = this.coeff[i];
// Trash old coefficients array
this.coeff = temp;
}
coeff[n] = value;
// if terms with largest exponents are 0, decrease degree
int degree = this.getDegree();
while (degree >= 0 && this.coeff[degree] == 0.0)
degree--;
if (degree < this.getDegree()) {
// Make the array smaller!
double temp[] = new double[degree + 1];
// Copy existing coefficients into temp
for (int i = 0; i <= degree; i++)
temp[i] = this.coeff[i];
// Trash old coefficients array
this.coeff = temp;
}
}
public Poly termMult(double coeff, int exp) {
Poly newPoly = new Poly();
for (int i = this.getDegree(); i >= 0; i--) {
double c = this.getCoeff(i) * coeff;
int e = i + exp;
newPoly.setCoeff(e, c);
}
return newPoly;
}
public Poly add(Poly p2) {
Poly a = new Poly();
int greater = (this.getDegree() > p2.getDegree()) ? this.getDegree() : p2.getDegree();
for (int i = greater; i >= 0; i--) {
a.setCoeff(i, p2.getCoeff(i) + this.getCoeff(i));
}
return a;
}
public Poly negative() {
Poly b = new Poly();
for (int i = this.getDegree(); i >= 0; i--) {
b.setCoeff(i, -this.getCoeff(i));
}
return b;
}
public Poly sub(Poly p2) {
return this.add(p2.negative());
}
public double evaluate(double x) {
double p = 0;
for (int i = this.getDegree(); i >= 0; i--)
p += coeff[i] * Math.pow(x, i);
return p;
}
public String toString() {
if (getDegree() == -1)
return "0";
else if (this.getDegree() == 0)
return "" + coeff[0];
else if (getDegree() == 1)
return coeff[1] + "x + " + coeff[0];
String p = coeff[getDegree()] + "x^" + getDegree();
for (int i = getDegree() - 1; i >= 0; i--) {
if (coeff[i] == 0)
continue;
else if (coeff[i] > 0)
p = p + " + " + (coeff[i]);
else if (coeff[i] < 0)
p = p + " - " + (-coeff[i]);
if (i == 1)
p = p + "x";
else if (i > 1)
p = p + "x^" + i;
}
return p;
}
}
/*Alex Bowman
* HW2 Polynomials
* Prof. Silvestri
* 2/17/20
*/
package hw2;
import java.util.Scanner;
public class PolynomialDriver {
private final static String TITLE = "Polynomial Expression Manipulator V1.0";
private final static String CONTINUE_PROMPT = "Do this again? [y/N] ";
//**********************************************
// Put as many methods you need here
//**********************************************
// Start your logic coding in the process method
private static void process(Scanner sc, String args[]) {
System.out.print("Enter order of first polynomial: ");
Poly p1 = new Poly();
int p1Degree = sc.nextInt();
System.out.print("Enter coefficients (high order first): ");
for (int i = p1Degree; i >= 0; i--)
p1.setCoeff(i, sc.nextDouble());
System.out.print("Enter order of second polynomial: ");
Poly p2 = new Poly();
int p2Degree = sc.nextInt();
System.out.print("Enter coefficients (high order first): ");
for (int i = p2Degree; i >= 0; i--)
p2.setCoeff(i, sc.nextDouble());
Poly pAdd = p1.add(p2);
Poly p1Neg = p1.negative();
Poly pSub = p1.sub(p2);
Poly p2Neg = p2.negative();
System.out.printf("P1 = %s\n", p1);
System.out.printf("-P1 = %s\n", p1Neg);
System.out.printf("P2 = %s\n", p2);
System.out.printf("-P2 = %s\n", p2Neg);
System.out.printf("P1 + P2 = %s\n", pAdd);
System.out.printf("P1 - P2 = %s\n", pSub);
System.out.print("Enter x to evaluate p1 and p2: ");
double x = sc.nextDouble();
System.out.printf("P1(%s) = %.4f\n", x, p1.evaluate(x));
System.out.printf("P2(%s) = %.4f\n", x, p2.evaluate(x));
sc.nextLine(); // Clear Keyboard
}
//**********************************************
// Do not change the doThisAgain method
private static boolean doThisAgain(Scanner sc, String prompt) {
System.out.print(prompt);
String doOver = sc.nextLine();
return doOver.trim().equalsIgnoreCase("Y");
}
//**********************************************
// Do not change the main method
public static void main(String args[]) {
System.out.println("Welcome to " + TITLE);
Scanner sc = new Scanner(System.in);
do {
process(sc, args);
} while (doThisAgain(sc, CONTINUE_PROMPT));
sc.close();
System.out.println("Thank you for using " + TITLE);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment