Guest User

tylkomirko

a guest
Jan 5th, 2016
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.99 KB | None | 0 0
  1. import static java.lang.Math.*;
  2. import java.util.Scanner;
  3.  
  4. public class nowy {
  5.  
  6.  
  7. //TYLKO MIRKO <3
  8.  
  9. public static void main(String[] args) {
  10. int a, b;
  11. double e = 0.00001;
  12.  
  13. Scanner sc = new Scanner(System.in);
  14. System.out.println("Podaj poczatek przedzialu");
  15. a = sc.nextInt();
  16. System.out.println("Podaj koniec przedzialu");
  17. b = sc.nextInt();
  18.  
  19. System.out.println(polowienie(a,b,e));
  20.  
  21. }
  22.  
  23. public static double funkcja (double x){
  24. return 2*x+4;
  25. }
  26.  
  27. public static double polowienie(double a, double b, double e){
  28. double c;
  29.  
  30. if (a>b) {
  31. System.out.println("Błędnie wprowadzone dane: " + a + ", jest większa od: " + b);
  32. }
  33.  
  34. if (2*a+4 ==0){
  35. return a;
  36. }else if (2*b+4 == 0){
  37. return b;
  38. }
  39.  
  40. while(b-a > e){
  41. c = (a+b)/2;
  42.  
  43. if(funkcja(c) == 0.0) //jesli miejsce zerowe jest w srodku
  44. return c;
  45. if(funkcja(a)*funkcja(c)<0)
  46. b = c;
  47. else
  48. a = c;
  49. }
  50. return (a+b)/2;
  51. }
  52. }
Advertisement
Add Comment
Please, Sign In to add comment