Java ADT——一元二次多項式
4845 ワード
1、一元二次多項式抽象データ型類QUAdratic.java
2、一元二次多項式操作インタフェースQUAbstratic.java
3、一元二次多項式ADTとインタフェースを応用し、相応の機能を実現する
4、運行結果:
package com.practice1_2;
/**
* ADT
*/
public class QUAdratic {
private int a;
private int b;
private int c;
public QUAdratic(){
this.a = 0;
this.b = 0;
this.c = 0;
}
public QUAdratic(int a, int b, int c){
this.a = a;
this.b = b;
this.c = c;
}
public int getA() {
return a;
}
public int getB() {
return b;
}
public int getC() {
return c;
}
public void setA(int a) {
this.a = a;
}
public void setB(int b) {
this.b = b;
}
public void setC(int c) {
this.c = c;
}
}
2、一元二次多項式操作インタフェースQUAbstratic.java
package com.practice1_2;
/**
*
*/
public interface QUAbstratic {
//
QUAdratic InitQuadreatic(int a, int b, int c);
// ,
QUAdratic Add(QUAdratic q1, QUAdratic q2);
// x ,
int Eval(QUAdratic q, int x);
// 0 ,
float[] Root(QUAdratic q);
//
void Print(QUAdratic q);
}
3、一元二次多項式ADTとインタフェースを応用し、相応の機能を実現する
package com.practice1_2;
/**
* ADT
* 1、 x ,
* 2、
*/
public class QUAbtraticAppli implements QUAbstratic{
public static void main(String[] args){
QUAbtraticAppli app = new QUAbtraticAppli();
app.test();
}
private void test(){
QUAdratic q1 = InitQuadreatic(1,4,3);
QUAdratic q2 = InitQuadreatic(-2,5,-8);
QUAdratic total = Add(q1,q2);
Print(q1);
Print(q2);
Print(total);
System.out.println("x 2 :" + Eval(total, 2));
judgeRoot(total);
}
private void judgeRoot(QUAdratic q){
float[] result = Root(q);
if(result[0] == -1){
System.out.println(" ");
return;
}
if(result[1] == 0){
System.out.println(" : " + result[1]);
return;
}
System.out.println(" : ");
System.out.println(" :x1 = " + result[1]);
System.out.println(" : x2 = " + result[2]);
}
@Override
public QUAdratic InitQuadreatic(int a, int b, int c) {
return new QUAdratic(a,b,c);
}
@Override
public QUAdratic Add(QUAdratic q1, QUAdratic q2) {
QUAdratic total = new QUAdratic();
total.setA(q1.getA() + q2.getA());
total.setB(q1.getB() + q2.getB());
total.setC(q1.getC() + q2.getC());
return total;
}
@Override
public int Eval(QUAdratic q, int x) {
return q.getA() * (int)Math.pow(x,2) + q.getB() * x + q.getC();
}
@Override
public float[] Root(QUAdratic q) {
float[] result = new float[3];
float derta = (float)Math.pow(q.getB(),2) - 4*q.getA()*q.getC();
if(derta < 0){
result[0] = -1;
result[1] = 0;
result[2] = 0;
return result;
}
else if(derta == 0){
result[0] = 0;
result[1] = -1 * q.getB() / (2 * q.getA());
result[2] = 0;
return result;
}
else {
result[0] = 1;
result[1] = (-1 * q.getB() + (float)Math.sqrt(derta)) / (2 * q.getA());
result[2] = (-1 * q.getB() - (float)Math.sqrt(derta)) / (2 * q.getA());
return result;
}
}
@Override
public void Print(QUAdratic q) {
if(q.getA() != 0){
if(q.getA() != -1 && q.getA() != 1)
System.out.print(q.getA() + "x^2");
else {
if(q.getA() == 1)
System.out.print("x^2");
else
System.out.print("-x^2");
}
}
if(q.getB() != 0){
if(q.getB() >0) {
if(q.getB() != 1)
System.out.print("+" + q.getB() + "x");
else
System.out.print("+x");
}
else {
if(q.getB() != -1)
System.out.print(q.getB() + "x");
else
System.out.print("-x");
}
}
if(q.getC() != 0){
if(q.getC() >0)
System.out.print("+" + q.getC());
else
System.out.print(q.getC());
}
System.out.println();
}
}
4、運行結果:
x^2+4x+3
-2x^2+5x-8
-x^2+9x-5
x 2 :9
:
:x1 = 0.5948751
: x2 = 8.405125
Process finished with exit code 0