Facebook
From Queen Mockingbird, 4 Years ago, written in Java.
Embed
Download Paste or View Raw
Hits: 104
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6. package javaapplication1;
  7.  
  8. import java.util.List;
  9. import java.util.Scanner;
  10. /**
  11.  *
  12.  * @author pw.d.9
  13.  */
  14. public class JavaApplication1 {
  15.  
  16.     /**
  17.      * @param args the command line arguments
  18.      */
  19.     public static void main(String[] args) {
  20.         System.out.println("hello world");
  21.         double a,b,c;
  22.         Scanner sc = new Scanner(System.in);
  23.         RównanieKwadratowe rk;// = new RównanieKwadratowe(2, 2, -12);
  24. //        double d = rk.obliczDelte();
  25. //        double[] zerowe = rk.mscZerowe(d);
  26. //        System.out.println(zerowe[0]);
  27. //        System.out.println(zerowe[1]);
  28.        // System.out.print("Podaj a: ");
  29.         a= 5;//sc.nextDouble();
  30.         //System.out.print("Podaj b: ");
  31.         b= 4;//sc.nextDouble();
  32.         //System.out.print("Podaj c: ");
  33.         c= -12;//sc.nextDouble();
  34.         rk = new RównanieKwadratowe(a, b, c);
  35.         double d = rk.obliczDelte();
  36.         double[] zerowe = rk.mscZerowe(d);
  37.         System.out.println(zerowe[0]);
  38.         System.out.println(zerowe[1]);
  39.         System.out.print("Podaj x0: ");
  40.         double x0 = sc.nextDouble();
  41.         System.out.print("Podaj x1: ");
  42.         double x1 = sc.nextDouble();
  43.         System.out.print("Podaj skok: ");
  44.         double skok = sc.nextDouble();
  45.         List<Double> rs = rk.obliczWPrzedziale(x0, x1, skok);
  46.         System.out.println("X     Y");
  47.         for(double y : rs){
  48. //            System.out.println(x0 + "   " + y);
  49.             System.out.println(String.format("%1$-2.3f   %2$2.3f", x0, y));
  50.             x0+=skok;
  51.         }
  52.        
  53.        
  54.     }
  55.    
  56. }
  57.  
  58.  
  59.  
  60.  
  61.  
  62.  
  63.  
  64.  
  65.  
  66. //RównanieKwadratowe.java
  67. /*
  68.  * To change this license header, choose License Headers in Project Properties.
  69.  * To change this template file, choose Tools | Templates
  70.  * and open the template in the editor.
  71.  */
  72. package javaapplication1;
  73.  
  74. import java.util.ArrayList;
  75. import java.util.List;
  76.  
  77. /**
  78.  *
  79.  * @author pw.d.9
  80.  */
  81. class RównanieKwadratowe {
  82.     private double _a, _b, _c;
  83.     public RównanieKwadratowe(double a, double b, double c) {
  84.         _a = a;
  85.         _b = b;
  86.         _c = c;
  87.     }
  88.     public double obliczDelte(){
  89.         return _b*_b - 4*_a*_c;
  90.     }
  91.     public double[] mscZerowe(double delta) {
  92.         double[] msc_zerowe = {0,0};
  93.         if(delta > 0){
  94.             msc_zerowe[0] = (-_b - Math.sqrt(delta))/2*_a;
  95.             msc_zerowe[1] = (-_b + Math.sqrt(delta))/2*_a;
  96.             return msc_zerowe;
  97.         }
  98.         if(delta == 0){
  99.             msc_zerowe[0] = -_b/2*_a;
  100.             msc_zerowe[1] = -_b/2*_a;
  101.             return msc_zerowe;
  102.         }
  103.         return msc_zerowe;
  104.     }
  105.     public double function(double x){
  106.         return _a*x*x+_b*x+_c;
  107.     }
  108.     public List<Double> obliczWPrzedziale(double x0, double x1, double skok){
  109.         List<Double> results = new ArrayList<>();
  110.        
  111.         for(double x = x0; x<x1; x+=skok){
  112.             results.add(function(x));
  113.         }
  114.         return results;
  115.     }
  116. }
  117.