AntonS2000

task6_v

Oct 30th, 2018
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.68 KB | None | 0 0
  1. package com.company;
  2.  
  3. /*
  4.     task 6
  5.     problem 4
  6.  */
  7.  
  8. import java.util.Locale;
  9. import java.util.Scanner;
  10.  
  11. public class Main {
  12.  
  13.     public static final String ENTER_N = "n:";
  14.     public static final String ENTER_X = "x:";
  15.     public static final String ENTER_E = "e:";
  16.  
  17.  
  18.     public static int getInt(Scanner scanner){
  19.         while (!scanner.hasNextInt()){
  20.             System.out.println("error");
  21.             scanner.next();
  22.         }
  23.         return scanner.nextInt();
  24.     }
  25.  
  26.     public static double getDouble(Scanner scanner){
  27.         while (!scanner.hasNextDouble()){
  28.             System.out.println("error");
  29.             scanner.next();
  30.         }
  31.         return scanner.nextDouble();
  32.     }
  33.  
  34.     public static double get_a(double x, int n){
  35.  
  36.         if(n % 2 == 0){
  37.             return -Math.pow(x,1+2*(n-1))/(1+2*(n-1));
  38.         }
  39.         else{
  40.             return Math.pow(x,1+2*(n-1))/(1+2*(n-1));
  41.         }
  42.     }
  43.  
  44.     public static double sum_n(int n, double x){
  45.         double sum = 0;
  46.  
  47.         for(int i = 1; i <n; i++){
  48.             sum += get_a(x,i);
  49.         }
  50.         return  sum;
  51.     }
  52.  
  53.     public static double sum_e(double e, double x){
  54.         double sum = 0;
  55.         int i = 1;
  56.         while (true){
  57.             double t = get_a(x,i);
  58.             if(Math.abs(t) >= Math.abs(e)){
  59.                 sum += t;
  60.                 i++;
  61.             }
  62.             else{
  63.                 break;
  64.             }
  65.         }
  66.         return sum;
  67.     }
  68.  
  69.     public static double sum_e_10(double e, double x){
  70.         double sum = 0;
  71.         int i = 1;
  72.         while (true){
  73.             double t = get_a(x,i);
  74.             if(Math.abs(t) >= Math.abs(e/10)){
  75.                 sum += t;
  76.                 i++;
  77.             }
  78.             else{
  79.                 break;
  80.             }
  81.         }
  82.         return sum;
  83.     }
  84.  
  85.     public static double calc(double x){
  86.         return Math.atan(x);
  87.     }
  88.  
  89.  
  90.     public static void main(String[] args) {
  91.  
  92.         Scanner scanner = new Scanner(System.in);
  93.         Locale.setDefault(Locale.ROOT);
  94.  
  95.         System.out.println(ENTER_N);
  96.         int n = getInt(scanner);
  97.         while(!(n>0)){
  98.             n = getInt(scanner);
  99.         }
  100.  
  101.  
  102.         System.out.println(ENTER_X);
  103.         double x = getDouble(scanner);
  104.         while(!(x > 0.0 && x < 1.0)){
  105.             x = getInt(scanner);
  106.         }
  107.  
  108.  
  109.         System.out.println(ENTER_E);
  110.         double e = getDouble(scanner);
  111.         while(!(e>0)){
  112.             e = getInt(scanner);
  113.         }
  114.  
  115.         System.out.println(sum_n(n,x));
  116.         System.out.println(sum_e(e,x));
  117.         System.out.println(sum_e_10(e,x));
  118.         System.out.println(calc(x));
  119.  
  120.     }
  121. }
Advertisement
Add Comment
Please, Sign In to add comment