QuadraticEquation.rar

  • PUDN用户
    了解作者
  • Java
    开发工具
  • 1KB
    文件大小
  • rar
    文件格式
  • 0
    收藏次数
  • 1 积分
    下载积分
  • 4
    下载次数
  • 2009-11-25 17:06
    上传日期
Quadratic Equation solution
QuadraticEquation.rar
  • Quadratic.java
    1.5KB
  • QuadraticApplication.java
    1.1KB
内容介绍
//Practical 2 - Part B - Question 1 public class Quadratic { private int a; private int b; private int c; private double x1, x2; public Quadratic(int a, int b, int c) { // assign the value of parameter a, b and c to // the instance variable a, b and c. Check �this� // keyword in Java. this.a = a; this.b = b; this.c = c; } public boolean validate() { // check if B2 >= 4AC and A != 0 boolean isValid = true; if(a!=0 && ((Math.pow(b, 2))>=(4*a*c))) { return true; } else { return false; } } public void evaluate() { // evaluate the quadratic equation with the given // a, b and c values from the constructor. double discriminant = 0.0; discriminant = (Math.pow(b, 2)) - (4*a*c); if(discriminant == 0.0) { //only one root available x1 = (-b + Math.sqrt(discriminant)) / (2 *a); } else { if(discriminant > 0.0) { //real roots x1 = (-b + Math.sqrt(discriminant)) / (2 * a); x2 = (-b - Math.sqrt(discriminant)) / (2 * a); } else if(discriminant < 0.0) { //imaginary roots //this part of the code is unreachable as B*B >= 4*A*C, so discriminant will be //always greater that zero x1 = (-b + Math.sqrt(-discriminant)) / (2 * a); x2 = (-b + Math.sqrt(-discriminant)) / (2 * a); } } } public double getX1() { return x1; } public double getX2() { return x2; } } // Quadratic class
评论
    相关推荐