Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.logging.Level;
- import java.util.logging.Logger;
- class ZlyPrzedzialException extends Exception {
- };
- public class SzukajPierwiastka {
- double f(double x) {
- return 3 * x - 7;
- }
- void testuj(double a, double b) throws ZlyPrzedzialException {
- if (Math.signum(f(a)) * Math.signum(f(b)) > 0) {
- throw new ZlyPrzedzialException();
- }
- }
- double szukaj(double a, double b) throws ZlyPrzedzialException {
- testuj(a, b);
- while (Math.abs(a - b) > 0.001) {
- double c = (b + a) / 2;
- try {
- testuj(a, c);
- } catch (ZlyPrzedzialException e) {
- a = c;
- }
- }
- return a;
- }
- public static void main(String[] args) {
- double a, b;
- a = -10;
- b = 10;
- SzukajPierwiastka s = new SzukajPierwiastka();
- try {
- s.szukaj(a, b);
- } catch (ZlyPrzedzialException ex) {
- //tu mial cos innego ale dziala xd Logger.getLogger(SzukajPierwiastka.class.getName()).log(Level.SEVERE, null, ex);
- }
- }
- //
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement