为一个2*2的线性方程设计一个名为LinearEquation的类,然后编写一个测试程序测试它:
代码:/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author Administrator
*/
class LinearEquation{
int a,b,c,d,e,f;
LinearEquation(int a,int b,int c,int d,int e,int f){
this.a=a;
this.b=b;
this.c=c;
this.d=d;
this.e=e;
this.f=f;
}
int getA(){
return a;
}
int getB(){
return b;
}
int getC(){
return c;
}
int getD(){
return d;
}
int getE(){
return e;
}
int getF(){
return f;
}
boolean isSolvable(){
return a*d-b*c!=0?true:false;
}
int getX(){
return (e*d-b*f)/(a*d-b*c);
}
int getY(){
return (a*f-e*c)/(a*d-b*c);
}
}
public class XiTi811 {
public static void main(String[] args){
System.out.println("请输入a,b,c,d,e,f的值:");
java.util.Scanner input=new java.util.Scanner(System.in);
System.out.print("a=");
int a=input.nextInt();
System.out.print("b=");
int b=input.nextInt();
System.out.print("c=");
int c=input.nextInt();
System.out.print("d=");
int d=input.nextInt();
System.out.print("e=");
int e=input.nextInt();
System.out.print("f=");
int f=input.nextInt();
LinearEquation l=new LinearEquation(a,b,c,d,e,f);
if(a*d-b*c==0)
System.out.println("此方程无解!");
else
System.out.println("线性方程的解为:"+l.getX()+"和"+l.getY());
}
}
本文来源:https://www.2haoxitong.net/k/doc/15f89a5bccbff121dd3683d8.html
文档为doc格式