编写一个类,该类封装了一元二次方程共有的属性和功能,即该类有刻画方程系数的3个成员变量以及计算实根的方法。 ...

package practice1;

public class Pratice1 {

 private double a;

 private double b;

 private double c;

 private double[] d;

 public String[] i;

 public void setValue(double a, double b, double c) {
  this.a = a;
  this.b = b;
  this.c = c;
  this.d = new double[2];
  this.i = new String[2];
 }

 public boolean canGetValue() {
  double temp;
  temp = b * b - 4 * a * c;
  if (temp < 0) {
   this.i[0] = (-b) / (a * 2) + "+" + Math.sqrt(-temp) / (2 * a) + "i";

   this.i[1] = (-b) / (a * 2) + "-" + Math.sqrt(-temp) / (2 * a) + "i";
   return false;
  } else {
   this.d[0] = (-b + Math.sqrt(temp)) / (2 * a);
   this.d[1] = (-b - Math.sqrt(temp)) / (2 * a);
   return true;
  }
 }

 /**
  * @param args
  */

 public double getA() {
  return a;
 }

 public double getB() {
  return b;
 }

 public double getC() {
  return c;
 }

 public double getD(int i) {
  return d[i];
 }

 public static void main(String[] args) {
  // TODO Auto-generated method stub
  Pratice1 pratice1 = new Pratice1();
  pratice1.setValue(1, 3, 4);
  if (pratice1.canGetValue()) {
   System.out.println(pratice1.getA());
   System.out.println(pratice1.getB());
   System.out.println(pratice1.getC());
   System.out.println(pratice1.getD(0));
   System.out.println(pratice1.getD(1));

  } else {
   System.out.println(pratice1.i[0]);
   System.out.println(pratice1.i[1]);
  }
 }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值