Java黑皮书课后题第9章:*9.11(代数:2*2的线性方程)为一个2*2的线性方程设计一个名为LinearEquation的类
Java黑皮书课后题第9章:*9.11(代数:2*2的线性方程)为一个2*2的线性方程设计一个名为LinearEquation的类
- 题目
- 破题
- 代码
- Test10
- Test11_LinearEquation
- 运行结果
- UML图
题目

破题
Test11:测试程序
Test11_LinearEquation:实现题目要求
点击跳转练习题3.3
代码
Test10
import java.util.Scanner;public class Test11 {public static void main(String[] args) {// 获取用户输入Scanner input = new Scanner(System.in);System.out.println("Enter a, b, c, d, e, f: ");double a = input.nextInt(), b = input.nextInt(), c = input.nextInt();double d = input.nextInt(), e = input.nextInt(), f = input.nextInt();// 传入构造方法Test11_LinearEquation le = new Test11_LinearEquation(a, b, c, d, e, f);// 判断是否有结果boolean bool = le.isSolvable();if (bool){System.out.println("x is " + le.getX() + " and y is " + le.getY());} elseSystem.out.println("The equation has no solution");}
}
Test11_LinearEquation
public class Test11_LinearEquation {private double a, b, c, d, e, f;// 构造方法public Test11_LinearEquation (double a, double b, double c, double d, double e, double f){this.a = a;this.b = b;this.c = c;this.d = d;this.e = e;this.f = f;}// getterpublic double getA() {return a;}public double getB() {return b;}public double getC() {return c;}public double getD() {return d;}public double getE() {return e;}public double getF() {return f;}// isSolvable方法public boolean isSolvable(){boolean bool = false;if (a * d - b * c != 0){bool = true;}return bool;}// getX和getYpublic double getX(){return (e * d - b * f) / (a * d - b * c);}public double getY(){return (a * f - e * c) / (a * d - b * c);}
}
运行结果
Enter a, b, c, d, e, f:
1 2 3 4 5 6
x is -4.0 and y is 4.5
UML图

本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!
