Advertisement
Voldemord

[Java] ZadanieRównanieKwadratowe

Mar 14th, 2019
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.66 KB | None | 0 0
  1. package rownanieKwadratowe;
  2.  
  3. /**
  4.  *
  5.  * @author Student
  6.  */
  7. public class Main {
  8.     public static void main(String[] args) {
  9.         QuadraticEquation n = new QuadraticEquation(1,0,-4);
  10.        
  11.        
  12.         try{
  13.             System.out.println("Wynik = " + n.getResult(0)+ " " + n.getResult(1));
  14.         }catch (Exception e){
  15.             System.out.println("Delta ujemna");
  16.             e.printStackTrace(System.out);
  17.         }
  18.        
  19.         System.out.println("a = " + n.getA());
  20.         System.out.println("b = " + n.getB());
  21.         System.out.println("c = " + n.getC());
  22.         System.out.println("delta = " + n.getDelta());
  23.        }
  24.    
  25. }
  26.  
  27. /*--------------------------------------------------------------------------------------------------------------*/
  28.  
  29. package rownanieKwadratowe;
  30.  
  31. public class QuadraticEquation {
  32.     private double a;
  33.     private double b;
  34.     private double c;
  35.     private double delta;
  36.     private double[] result = new double[2];
  37.    
  38.     public QuadraticEquation(double a, double b, double c){
  39.         this.a = a;
  40.         this.b = b;
  41.         this.c = c;
  42.         quadraticEquation();
  43.     }
  44.    
  45.     private void quadraticEquation(){
  46.         double a = this.a;
  47.         double b = this.b;
  48.         double c = this.c;
  49.         double delta;
  50.         double[] result = new double[2];
  51.        
  52.         delta = Math.pow(b,2) - (4*a*c);
  53.         this.delta=delta;
  54.     }
  55.    
  56.     private void calcResults() throws Exception{
  57.         double delta = this.delta;
  58.         if(delta>0)
  59.         {
  60.             result[0] = (-b - Math.sqrt(delta)/2*a);
  61.             result[1] = (-b + Math.sqrt(delta)/2*a);            
  62.         }
  63.         else if(delta==0){
  64.             result[0] = -b/2*a;
  65.         }
  66.         else throw new Exception("Delta jest ujemna - Brak Rozwiazan");      
  67.         this.result = result;
  68.     }
  69.  
  70.     /*
  71.     ---Variable A
  72.     */
  73.     public double getA() {
  74.         return a;
  75.     }
  76.    
  77.     public void setA(double a) {
  78.         this.a = a;
  79.         quadraticEquation();
  80.     }
  81.      /*
  82.     ---Variable B
  83.     */
  84.     public double getB() {
  85.         return b;
  86.     }
  87.  
  88.     public void setB(double b){
  89.         this.b = b;
  90.         quadraticEquation();
  91.     }
  92.      /*
  93.     ---Variable C
  94.     */
  95.     public double getC() {
  96.         return c;
  97.     }
  98.  
  99.     public void setC(double c){
  100.         this.c = c;
  101.         quadraticEquation();
  102.     }
  103.      /*
  104.     ---Variable Delta
  105.     */
  106.     public double getDelta() {
  107.         return delta;
  108.     }
  109.  
  110.         /*
  111.     ---Variable Result
  112.     */
  113.     public double getResult(int x) throws Exception{
  114.         calcResults();
  115.         return result[x];
  116.     }
  117.    
  118.    
  119. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement