aLT22

4semlab1 (Java) ALPHA VERS

Mar 3rd, 2015
204
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.40 KB | None | 0 0
  1. /**
  2.  * Created by Алексей on 16.02.2015.
  3.  */
  4. import java.util.Scanner;
  5.  
  6. public class Main extends Arithmetic {
  7.     public static void main (String [] args) {
  8.         Scanner reader = new Scanner(System.in);
  9.         System.out.print("Введите количество элементов ряда: ");
  10.         N = reader.nextInt();
  11.         System.out.print("Введите аргумент x: ");
  12.         x = reader.nextDouble();
  13.         System.out.print("Введите эпсилон: ");
  14.         eps = reader.nextDouble();
  15.         if (N <= 0) {
  16.             System.out.println ("Неверное N!");
  17.         } else {
  18.             Arithmetic use = new Arithmetic();
  19.             System.out.println("Сумма n-элементов ряда равна " + use.Counter_N(N, x));
  20.             System.out.println("Сумма элементов, больших некоторого эпсилон равна " + use.Counter_E(N,x,eps,count));
  21.             System.out.println("Всего слагаемых в сумме с эпсилон " + count);
  22.         }
  23.     }
  24. }
  25.  
  26. // создаем класс вычислений (в нем будет метод факториала), этот класс будет наследовать класс Main
  27.  
  28. class Arithmetic {
  29.     static int N, count = 0;
  30.     static double x, eps, Sigma_N = 0, Sigma_E = 0;
  31.     public int Factorials (int iterator) {
  32.         int ch_Fact = 1, nch_Fact = 1;
  33.         if (iterator % 2 == 0) {
  34.             for (int k = 2; k <= iterator; k+=2){
  35.                 ch_Fact *= k;
  36.             }
  37.             return ch_Fact;
  38.         } else {
  39.             for (int k = 1; k <= iterator; k+=2) {
  40.                 nch_Fact *= k;
  41.             }
  42.             return nch_Fact;
  43.         }
  44.     }
  45.  
  46.     public double Counter_N (int N, double x) {
  47.         for (int i = 1; i <= N; i++) {
  48.             Sigma_N += ((Math.pow(-1.0, (double) i) * Math.pow(Math.cos(x),(double)(i/2))) / (Factorials(i+1)));
  49.         }
  50.         return Sigma_N;
  51.     }
  52.     public double Counter_E (int N, double x, double eps, int count) {
  53.         int i = 1;
  54.         while (i <= N && Math.abs(Math.pow(-1.0,(double)i) * Math.pow(Math.cos(x),(double)(i/2)) / (Factorials(i+1))) > eps) {
  55.             Sigma_E += Math.abs(Math.pow(-1.0,(double)i) * Math.pow(Math.cos(x),(double)(i/2)) / (Factorials(i+1)));
  56.             count++;
  57.             i++;
  58.         }
  59.         return Sigma_E;
  60.     }
  61. }
Advertisement
Add Comment
Please, Sign In to add comment