Advertisement
Guest User

zadanie

a guest
Mar 30th, 2015
208
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.15 KB | None | 0 0
  1.  
  2. import java.util.logging.Level;
  3. import java.util.logging.Logger;
  4.  
  5.  
  6. class ZlyPrzedzialException extends Exception {
  7. };
  8.  
  9. public class SzukajPierwiastka {
  10.  
  11.     double f(double x) {
  12.         return 3 * x - 7;
  13.     }
  14.  
  15.     void testuj(double a, double b) throws ZlyPrzedzialException {
  16.         if (Math.signum(f(a)) * Math.signum(f(b)) > 0) {
  17.             throw new ZlyPrzedzialException();
  18.         }
  19.     }
  20.  
  21.     double szukaj(double a, double b) throws ZlyPrzedzialException {
  22.         testuj(a, b);
  23.         while (Math.abs(a - b) > 0.001) {
  24.             double c = (b + a) / 2;
  25.             try {
  26.                 testuj(a, c);
  27.  
  28.             } catch (ZlyPrzedzialException e) {
  29.                 a = c;
  30.             }
  31.         }
  32.         return a;
  33.     }
  34.  
  35.     public static void main(String[] args) {
  36.         double a, b;
  37.         a = -10;
  38.         b = 10;
  39.         SzukajPierwiastka s = new SzukajPierwiastka();
  40.         try {
  41.             s.szukaj(a, b);
  42.         } catch (ZlyPrzedzialException ex) {
  43.     //tu mial cos innego ale dziala xd        Logger.getLogger(SzukajPierwiastka.class.getName()).log(Level.SEVERE, null, ex);
  44.         }
  45.     }
  46. //
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement