Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package rownanieKwadratowe;
- /**
- *
- * @author Student
- */
- public class Main {
- public static void main(String[] args) {
- QuadraticEquation n = new QuadraticEquation(1,0,-4);
- try{
- System.out.println("Wynik = " + n.getResult(0)+ " " + n.getResult(1));
- }catch (Exception e){
- System.out.println("Delta ujemna");
- e.printStackTrace(System.out);
- }
- System.out.println("a = " + n.getA());
- System.out.println("b = " + n.getB());
- System.out.println("c = " + n.getC());
- System.out.println("delta = " + n.getDelta());
- }
- }
- /*--------------------------------------------------------------------------------------------------------------*/
- package rownanieKwadratowe;
- public class QuadraticEquation {
- private double a;
- private double b;
- private double c;
- private double delta;
- private double[] result = new double[2];
- public QuadraticEquation(double a, double b, double c){
- this.a = a;
- this.b = b;
- this.c = c;
- quadraticEquation();
- }
- private void quadraticEquation(){
- double a = this.a;
- double b = this.b;
- double c = this.c;
- double delta;
- double[] result = new double[2];
- delta = Math.pow(b,2) - (4*a*c);
- this.delta=delta;
- }
- private void calcResults() throws Exception{
- double delta = this.delta;
- if(delta>0)
- {
- result[0] = (-b - Math.sqrt(delta)/2*a);
- result[1] = (-b + Math.sqrt(delta)/2*a);
- }
- else if(delta==0){
- result[0] = -b/2*a;
- }
- else throw new Exception("Delta jest ujemna - Brak Rozwiazan");
- this.result = result;
- }
- /*
- ---Variable A
- */
- public double getA() {
- return a;
- }
- public void setA(double a) {
- this.a = a;
- quadraticEquation();
- }
- /*
- ---Variable B
- */
- public double getB() {
- return b;
- }
- public void setB(double b){
- this.b = b;
- quadraticEquation();
- }
- /*
- ---Variable C
- */
- public double getC() {
- return c;
- }
- public void setC(double c){
- this.c = c;
- quadraticEquation();
- }
- /*
- ---Variable Delta
- */
- public double getDelta() {
- return delta;
- }
- /*
- ---Variable Result
- */
- public double getResult(int x) throws Exception{
- calcResults();
- return result[x];
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement