Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

JAVA eclipse. Please help! Write a method that accepts an integer argument and r

ID: 3902281 • Letter: J

Question

JAVA eclipse. Please help!

Write a method that accepts an integer argument and returns the sum of all the integers from 1 up to the number passed as an argument. For example, if 50 is passed as an argument, the method will return the sum of 1, 2, 3, 4, . . . 50. Use recursion to calculate the sum. Demonstrate the method in a program.

The program should ask the user to enter a number, which is then used to display the sum. if the user enters -1, then exit the program.

View required output

Test Case 1

Test Case 2

Standard Input                
  5ENTER  10ENTER  -1ENTER  

Explanation / Answer

Solution:

IntegerSum.java

import java.util.ArrayList;

import java.util.Scanner; // Declaring Scanner Class

public class IntegersSum { // Declaring IntegerSum Class

static Scanner scan = new Scanner(System.in); //Using scanner class to take input from the keyboard

//Driver method to check the functionality of the program

public static void main(String[] args) {

takeInput();

}

//This method continuously takes input from the user

public static void takeInput() {

while(true) {

System.out.println("Please enter a number to calculate the sum or -1 to exit: ");

String temp = scan.nextLine(); //Taking input from the user as a string

int num;

//Using inputValidator to check whether its a valid integer

if(inputValidator(temp) ) {

num = Integer.parseInt(temp);//Storing the input

if(num == -1) { //If user types -1 then breaking out of the loop

break;

}else //if user types valid natural number then printing the sum by using method sumOfIntegers

System.out.println("The sum of first "+num+" natural numbers is "+sumOfIntegers(num)+".");

}else{//If user types invalid natural number then displaying error message

num = Integer.parseInt(temp);

if(num == -1) { //If user types -1 then breaking out of the loop

break;

}else System.out.println("Invalid number. The entry should be a positive number. ");

}

}

}

//This method validates the input given by the user

public static boolean inputValidator(String input) {

if(input.matches("[0-9]+")) {

if(Integer.parseInt(input) > 0)

return true;

}else return false;

return false;

}

//Calculating the sum of natural number recursively by calling the same function

public static int sumOfIntegers(int num) {

int result;

if(num==0 || num==1)

return 1;

result = sumOfIntegers(num-1) + num;

return result;

  

}

}

Sample Run 1:

Please enter a number to calculate the sum or -1 to exit:

5
The sum of first 5 natural numbers is 15.
Please enter a number to calculate the sum or -1 to exit:

10
The sum of first 10 natural numbers is 55.
Please enter a number to calculate the sum or -1 to exit:

-1

Sample Run 2:

Please enter a number to calculate the sum or -1 to exit:

-10
Invalid number. The entry should be a positive number.

Please enter a number to calculate the sum or -1 to exit:

0
Invalid number. The entry should be a positive number.

Please enter a number to calculate the sum or -1 to exit:

7
The sum of first 7 natural numbers is 28.
Please enter a number to calculate the sum or -1 to exit:

-1