Java黑皮书课后题第9章:*9.10(代数:二次方程式)为二次方程式设计一个名为QuadraticEquation的类
生活随笔
收集整理的這篇文章主要介紹了
Java黑皮书课后题第9章:*9.10(代数:二次方程式)为二次方程式设计一个名为QuadraticEquation的类
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
Java黑皮書課后題第9章:*9.10(代數(shù):二次方程式)為二次方程式設(shè)計(jì)一個(gè)名為QuadraticEquation的類
- 題目
- 破題
- 代碼
- Test10
- Test10_QuadraticEquation
- 運(yùn)行結(jié)果
- UML圖
題目
破題
Test10:測(cè)試程序
Test10_QuadraticEquation:實(shí)現(xiàn)題目要求
點(diǎn)擊這里跳轉(zhuǎn)3.1
代碼
Test10
import java.util.Scanner;public class Test10 {public static void main(String[] args) {// 用戶輸入Scanner input = new Scanner(System.in);System.out.println("Enter a, b, c: ");int a = input.nextInt(), b = input.nextInt(), c = input.nextInt();// 創(chuàng)建對(duì)象+調(diào)用構(gòu)造方法Test10_QuadraticEquation qe = new Test10_QuadraticEquation(a, b, c);// 獲取判別式結(jié)果double judge = qe.getDiscriminant();if (judge > 1){System.out.printf("%.3f %.3f\n", qe.getRoot1(), qe.getRoot2());} else if (judge < 0.0001 && judge > -0.0001){System.out.printf("%.3f\n", qe.getRoot1());} else {System.out.printf("The equation has no roots");}} }Test10_QuadraticEquation
public class Test10_QuadraticEquation {private double a, b, c;// 構(gòu)造方法public Test10_QuadraticEquation(int a, int b, int c){this.a = a;this.b = b;this.c = c;}// getter方法public double getA() {return a;}public double getB() {return b;}public double getC() {return c;}// getDiscriminant方法public double getDiscriminant(){return b * b - 4 * a * c;}// 獲取兩根public double getRoot1(){return (-b + Math.sqrt(getDiscriminant())) / (2 * a);}public double getRoot2(){return (-b - Math.sqrt(getDiscriminant())) / (2 * a);} }運(yùn)行結(jié)果
Enter a, b, c: 1 2 3 The equation has no rootsUML圖
總結(jié)
以上是生活随笔為你收集整理的Java黑皮书课后题第9章:*9.10(代数:二次方程式)为二次方程式设计一个名为QuadraticEquation的类的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Java黑皮书课后题第9章:9.8(Fa
- 下一篇: Java黑皮书课后题第9章:*9.11(