Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Created by Алексей on 16.02.2015.
- */
- import java.util.Scanner;
- public class Main extends Arithmetic {
- public static void main (String [] args) {
- Scanner reader = new Scanner(System.in);
- System.out.print("Введите количество элементов ряда: ");
- N = reader.nextInt();
- System.out.print("Введите аргумент x: ");
- x = reader.nextDouble();
- System.out.print("Введите эпсилон: ");
- eps = reader.nextDouble();
- if (N <= 0) {
- System.out.println ("Неверное N!");
- } else {
- Arithmetic use = new Arithmetic();
- System.out.println("Сумма n-элементов ряда равна " + use.Counter_N(N, x));
- System.out.println("Сумма элементов, больших некоторого эпсилон равна " + use.Counter_E(N,x,eps,count));
- System.out.println("Всего слагаемых в сумме с эпсилон " + count);
- }
- }
- }
- // создаем класс вычислений (в нем будет метод факториала), этот класс будет наследовать класс Main
- class Arithmetic {
- static int N, count = 0;
- static double x, eps, Sigma_N = 0, Sigma_E = 0;
- public int Factorials (int iterator) {
- int ch_Fact = 1, nch_Fact = 1;
- if (iterator % 2 == 0) {
- for (int k = 2; k <= iterator; k+=2){
- ch_Fact *= k;
- }
- return ch_Fact;
- } else {
- for (int k = 1; k <= iterator; k+=2) {
- nch_Fact *= k;
- }
- return nch_Fact;
- }
- }
- public double Counter_N (int N, double x) {
- for (int i = 1; i <= N; i++) {
- Sigma_N += ((Math.pow(-1.0, (double) i) * Math.pow(Math.cos(x),(double)(i/2))) / (Factorials(i+1)));
- }
- return Sigma_N;
- }
- public double Counter_E (int N, double x, double eps, int count) {
- int i = 1;
- while (i <= N && Math.abs(Math.pow(-1.0,(double)i) * Math.pow(Math.cos(x),(double)(i/2)) / (Factorials(i+1))) > eps) {
- Sigma_E += Math.abs(Math.pow(-1.0,(double)i) * Math.pow(Math.cos(x),(double)(i/2)) / (Factorials(i+1)));
- count++;
- i++;
- }
- return Sigma_E;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment