Find Us On: Facebook Twitter Rss Feed Blog
Need To Register? Sign Up Login

Java - Recursions Methods

flamenca91
flamenca91 


Exercise 1 1. Create a New Project named ArithmeticProgression and copy the file ArithmeticProgression.java into the project. 2. A common example of a recursive formula is one to compute the sum of the first n integers, 1 + 2 + 3 + … + n. The recursive formula can be expressed as 1 + 2 + 3 + … + n = n + (1 + 2 + 3 + … + (n – 1)) Implement the body of the method sumInts() in the class ArithmeticProgression so that it recursively calls itself to return the sum of the first n integers class ArithmeticProgression import java.util.*; public class ArithmeticProgression { public static void main(String[] args) { Scanner keyboard = new Scanner(System.in); char repeat = 'y'; while(repeat == 'y' || repeat == 'Y') { int number = -1; // Arbitrary value to enter loop while(number <= 0) { System.out.println("Enter a positive integer:"); number = keyboard.nextInt(); } System.out.println("The sum of the first " + number + " of integers is " + sumInts(number) + "."); System.out.println(); System.out.println("Do again? Enter 'y' for yes."); repeat = keyboard.next().charAt(0); } } private static int sumInts(int n) { // Implement this method so that it correctly returns the sum of the first n integers // You must use recursion, i.e. this method must call sumInts() // The following is obviously incorrect return(n); } } Attachments
Tags Clarifications

Answers

Java - Recursions Methods - Solution

dickersonka
dickersonka 



Attached is the solution to the Java - Recursions Methods assignment including source and comments Attachments
ArithmeticProgression.java (1 K) Download Preview
{ Scanner keyboard = new Scanner(System.in); char repeat = 'y'; while(repeat == 'y' || repeat == 'Y') { int number = -1; // Arbitrary value to enter loop while(number <= 0) { System.out.println("Enter

ArithmeticProgression_Screenshot.png (3 K) Download Preview

Screenshots
Screenshot
Purchase Answer

Recursions MEthods - Soltution

mariobeka
mariobeka 



Below you will find the ArithmeticProgression.java file with the implemented recursive method. Also a screenshot of the working program.
Comments provided as well inside the code.
Attachments
ArithmeticProgression.java (1 K) Download Preview
while (repeat == 'y' || repeat == 'Y') { int number = -1; // Arbitrary value to enter loop while (number <= 0) { System.out.println("Enter a positive integer:"); number = keyboard.nextInt(); } System.out.println("The sum of the first

Screen Shot 2014-05-05 at 9.22.07 PM.png (39 K) Download Preview

Screenshots
Screenshot
Purchase Answer