All pastes #784631 Raw Edit

quadpoly

public text v1 · immutable
·published 2007-11-19 19:53 UTC
rendered paste body
public class QuadPoly
{
 // Representation of 3 Polynomial Coefficients.
 // The coefficients of x^2, x and 1 in order.
 private double coefx2;
 private double coefx1;
 private double coefx0;
 // Construct a set of coefficients -- given the values.
 public QuadPoly(double requiredCoefx2,
     		 double requiredCoefx1,
     		 double requiredCoefx0)
 {
  coefx2 = requiredCoefx2;
  coefx1 = requiredCoefx1;
  coefx0 = requiredCoefx0;
 }// QuadPoly
 
 // Compare this polynomial with a given other one, for equality.
 public boolean equals(QuadPoly other)
 {
  return coefx2 == other.coefx2 && coefx1 == other.coefx1
		   && coefx0 == other.coefx0;
 }//equals

 // Compare this polynomial with a given other one, for less than.
 public boolean lessThan(QuadPoly other)
 {
  return coefx2 < other.coefx2
	  || coefx2 == other.coefx2 && (coefx1 < other.coefx1
		|| coefx1 == other.coefx1 && coefx0 < other.coefx0);
 }//less than

 // Print out the polynomial in standard format
 public String toString()
 {
  return coefx2 + "x^2 + " + coefx1 + "x + " + coefx0;
 }//toString

 //addition method
 public QuadPoly addPolys(QuadPoly other)
 {
  double newCoefx2 = (coefx2 + other.coefx2);
  double newCoefx1 = (coefx1 + other.coefx1);
  double newCoefx0 = (coefx0 + other.coefx0);
  return new QuadPoly(newCoefx2, newCoefx1, newCoefx0);
 }//addPolys

}// class QuadPoly

public class AddQuadPoly
{
 // Program to add coefficients of quadratic polynomials.

 public static void main(String [] args)
 {
  // First Polynomial
  QuadPoly polyFirst = new QuadPoly(Double.parseDouble(args[0]),
			   	    Double.parseDouble(args[1]),
			   	    Double.parseDouble(args[2]));

  // Second Polynomial
  QuadPoly polySecond = new QuadPoly(Double.parseDouble(args[3]),
			   	     Double.parseDouble(args[4]),
			   	     Double.parseDouble(args[5]));

  // Method to add the coefficients and SOP them.
  printAddition(polyFirst, polySecond);
 }//main

 // Need to equate coefficients and add them.
 private static void printAddition(QuadPoly polyFirst, QuadPoly polySecond)
 {
  // lets start by printing out the polynomials
  System.out.println("Polynomial " + polyFirst);

  System.out.println("Added to   " + polySecond);

  // now lets add them.
  QuadPoly polyAdded = polyFirst.addPolys(polySecond);
  // and print the addition.
  System.out.println(polyAdded);
 }//printAddition

}//class AddQuadPoly