/* * Quadratic2014.java * * Created on 9 Οκτώβριος 2012, 9:12 πμ */ /** * * *MODULARITY - ADVANTAGES (reference to computational thinking) *(INSERT METHOD - CALCULATE METHOD - OUPUT METHOD) *1. MODULARITY ADDS TO THE READABILITY OF THE PROGRAM *2. EASIER TO TEST AND DEBUG *3. CODE REUSE - YOU CAN CALL THE SAME METHOD FROM DIFFERENT PARTS OF THE PROGRAM *4. DIFFERENT PARTS OF THE PROGRAM CAN BE DEVELOPPED FROM DIFFERENT PROGRAMERS AT THE SAME TIME - OFCOURSE * THEY MUST AGREE BETWEEN THEM INTO SOME CONVENTIONS LIKE SIZE OF THE MODULES, NAMES OF THE VARIABLES ETC *5. THE PROGRAM IS EASIER TO MAINTAIN AND UPGRADE */ import java.io.*; public class Quadratic2014 { double a, b, c, x1, x2, D; public Quadratic2014() throws IOException { insert(); calculate(); output(); } public void insert()throws IOException { InputStreamReader inStream=new InputStreamReader(System.in); BufferedReader stdin=new BufferedReader(inStream); System.out.println("Solutions of the Quadratic ax^2 + bx + c = 0"); System.out.print ("Coefficient a ? = "); String data=stdin.readLine(); a = Double.parseDouble(data); System.out.print ("Coefficient b ? = "); data=stdin.readLine(); b = Double.parseDouble(data); System.out.print ("Coefficient c ? = "); data=stdin.readLine(); c = Double.parseDouble(data); } public void calculate(){ D = (b*b) - (4*a*c); if (D<0) System.out.println("D " + D + " is negative, no solution is possible"); else if (D==0) x1=(-b)/(2*a); else {x1 = ((-b)+(Math.sqrt(D)))/(2*a); x2 = ((-b)-(Math.sqrt(D)))/(2*a); } } public void output(){ if (D==0) System.out.println("One double solution x1=x2="+x1); else if (D>0) System.out.println("Two separate solutions x1="+x1+" x2="+x2); } public static void main(String[] args) throws IOException { new Quadratic2014(); } } // -------> write a program that calculates ax^3 + bx^2 +cx + d =0; capture all error cases.