Version 2


public class QuadraticEquation {
  // a quadratic equation a*x^2 + b*x + c = 0
  
  double a;
  double b;
  double c;
  
  QuadraticEquation(double a1, double b1, double c1) {
    // create a new quadratic equation with the given coefficients
    a = a1;
    b = b1;
    c = c1;
  }
  
  private String toSignedString(double d) {
    // creates string from double value with explicit sign
    if (d >= 0) {
      return("+ " + d);
    } else {
      return("- " + (-d));
    }
  }      

  void print() {
    // print the equation to the screen
    System.out.println(a + "*x^2 "+ toSignedString(b) + "*x " 
		       + toSignedString(c));
  }
}

public class TestQuadraticEquation {
  // simple test program for QuadraticEquation

  public static void main (String[] args) {
    // get the coefficients from the command line
    double a = Double.parseDouble(args[0]);
    double b = Double.parseDouble(args[1]);
    double c = Double.parseDouble(args[2]);

    QuadraticEquation qe1 = new QuadraticEquation(a, b, c);
    qe1.print();
  }
}

previous    contents     next

Peter Junglas 8.3.2000