Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.company;
- /*
- task 6
- problem 4
- */
- import java.util.Locale;
- import java.util.Scanner;
- public class Main {
- public static final String ENTER_N = "n:";
- public static final String ENTER_X = "x:";
- public static final String ENTER_E = "e:";
- public static int getInt(Scanner scanner){
- while (!scanner.hasNextInt()){
- System.out.println("error");
- scanner.next();
- }
- return scanner.nextInt();
- }
- public static double getDouble(Scanner scanner){
- while (!scanner.hasNextDouble()){
- System.out.println("error");
- scanner.next();
- }
- return scanner.nextDouble();
- }
- public static double get_a(double x, int n){
- if(n % 2 == 0){
- return -Math.pow(x,1+2*(n-1))/(1+2*(n-1));
- }
- else{
- return Math.pow(x,1+2*(n-1))/(1+2*(n-1));
- }
- }
- public static double sum_n(int n, double x){
- double sum = 0;
- for(int i = 1; i <n; i++){
- sum += get_a(x,i);
- }
- return sum;
- }
- public static double sum_e(double e, double x){
- double sum = 0;
- int i = 1;
- while (true){
- double t = get_a(x,i);
- if(Math.abs(t) >= Math.abs(e)){
- sum += t;
- i++;
- }
- else{
- break;
- }
- }
- return sum;
- }
- public static double sum_e_10(double e, double x){
- double sum = 0;
- int i = 1;
- while (true){
- double t = get_a(x,i);
- if(Math.abs(t) >= Math.abs(e/10)){
- sum += t;
- i++;
- }
- else{
- break;
- }
- }
- return sum;
- }
- public static double calc(double x){
- return Math.atan(x);
- }
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- Locale.setDefault(Locale.ROOT);
- System.out.println(ENTER_N);
- int n = getInt(scanner);
- while(!(n>0)){
- n = getInt(scanner);
- }
- System.out.println(ENTER_X);
- double x = getDouble(scanner);
- while(!(x > 0.0 && x < 1.0)){
- x = getInt(scanner);
- }
- System.out.println(ENTER_E);
- double e = getDouble(scanner);
- while(!(e>0)){
- e = getInt(scanner);
- }
- System.out.println(sum_n(n,x));
- System.out.println(sum_e(e,x));
- System.out.println(sum_e_10(e,x));
- System.out.println(calc(x));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment