Question

Write an application with two classes USING JAVA METHODS HAVE TO BE CREATED USING RECURSION. First...

Write an application with two classes USING JAVA METHODS HAVE TO BE CREATED USING RECURSION. First class named Recursion has the following three recursive methods:

// PRECONDITION n is positive integer. Method sumTerms returns sum of terms that are

// reciprocal values of first n integers, with  alternating signs.     

// For example,  sumTerms(7) returns  the following:  1/1 – 1/2  + 1/3 – 1/4  + 1/5 -1/6 + 1/7.

// Terms with an odd denominator have positive sign, and terms with even denominator have

// negative sign.  

  • double sumTermsRec(int n)

// PRECONDITION n is non-negative integer denoting the number of occupied positions in the

// array words.   Method printsWordsRec prints each String element in the array words

// together with its string length in the same order as they appear in the array words

  • void printWordsRec (String[] words, int n)

// PRECONDITION n is non-negative integer denoting the number of occupied positions in the

// array words.   Method printsWordsReverseRec prints each String element in the array

// words together with its length. Elements are printed in reverse  order than they apper in

// the  array words.

  • void printWordsReverseRec (String[] words, int n)

If array words has first three elements assigned values "Paris", Athens ", "Rome" method  printWordsRec should print

Paris 5

Athens 6

Rome 4

and  method printWordsReverseRec should print

Rome 4

Athens 6

Paris 5.

Class Tester has main method in it. In the main method invoke each of the three methods from class Recursion. Program outcome must provide English explanation which method is invoked and what is the result, and/or what method does.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

PLEASE NOTE : FEEL FREE TO ASK ANY DOUBTS by COMMENTING

  • I'm little bit confused of condition "Program outcome must provide English explanation which method is invoked and what is the result, and/or what method does."
    • We are using recursion, if we print about function it prints for every call. So, i'm not printing about function explanation.
    • Let me know if you have any queries regarding this
  • I'm just calling the functions and printing result in Tester code.
  • if you want me to change anything or if you face any troubles, please let me know by commenting.
  • I used comments for better understanding. Comments starts with ' // '
  • Please refer to the screenshot of the code to understand the indentation of the code

Language : Java

Filename : Recursion.java , Tester.java

IDE : Java Eclipse

:::::::::::::::::::::::::::::::::::::::::::: CODE ::::::::::::::::::::::::::::::::::::::::

Recursion.java

public class Recursion {
   // this method is used to return
   // reciprocal values of first n integers, with alternating signs
   public double sumTermsRec(int n) {
       // if n value is 0, return 0
       if(n==0) {
           return 0;
       }
       // if n is even, return negative value with calling next value
       if(n%2 == 0) {
           return -(1.0/n)+sumTermsRec(n-1);
       }
       // if n is odd, return positive value with calling next value
       return (1.0/n)+sumTermsRec(n-1);
   }

   // this method is used to print array values from first to last
   public void printWordsRec(String[] words, int n) {
       // if n value is more than 0
       if(n > 0) {
           // call function for next index of n which is n-1
           printWordsRec(words,n-1);
           // print string at index n-1
           System.out.println(words[n-1]+" "+words[n-1].length());
       }
   }

   // this method is used to print array values from last to first
   public void printWordsReverseRec (String[] words, int n) {
       // if n value is more than 0
       if(n > 0) {
           // print string at index n-1
           System.out.println(words[n-1]+" "+words[n-1].length());
           // call function for next index of n which is n-1
           printWordsRec(words,n-1);
       }
   }
}

Tester.java

public class Tester {
   public static void main(String[] args) {
       // create Recursion class instance
       Recursion r = new Recursion();

       // call sumTermsRec function with value 7
       System.out.println(r.sumTermsRec(7));

       // array of strings
       String[] words = {"Paris", "Athens","Rome"};

       // call printWordsRec to print first to last
       r.printWordsRec(words,words.length);

       // call printWordsReverseRec to print last to first
       r.printWordsReverseRec(words,words.length);
   }
}

:::::::::::::::::::::::::::::::::::::::::::: OUTPUT ::::::::::::::::::::::::::::::::::::::::

X Console X <terminated> Tester [Java Application] C:\Program Files\Java\jdk-13\bin\javaw.exe 0.7595238095238095 Paris 5 Athe

:::::::::::::::::::::::::::::::::::::: CODE in EDITOR ::::::::::::::::::::::::::::::::::

Recursion.java

2 3 Recursion.java X Tester.java 1 public class Recursion // this method is used to return //reciprocal values of first n int

Tester.java

26 4 Recursion.java X Tester.java X 1 public class Tester { public static void main(String[] args) { 3 // create Recursion cl

_________________________________________________________________

Dear Friend, Feel Free to Ask Any Doubts by Commenting. ASAP i'll respond when i'm available.

I'm on a critical Situation. Please Do Not Forget To Give A Thumbs UP +1. It will Helps me A Lot.

Thank YOU :-)

Add a comment
Know the answer?
Add Answer to:
Write an application with two classes USING JAVA METHODS HAVE TO BE CREATED USING RECURSION. First...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • In Java write the following array- processing methods into the same application, Lab13.java. Use the main...

    In Java write the following array- processing methods into the same application, Lab13.java. Use the main method in this application to test your methods. 1) Write the void method, shiftLeft, which accepts an array of int and changes the elements of this array so that the index of each element is now less by one and the last element of the array is now zero. For example, if the parameter array is {7, 3, 2, 9, 5}, then when this...

  • Write a method called printReverse() that takes a string and uses recursion to print the contents...

    Write a method called printReverse() that takes a string and uses recursion to print the contents of the string in reverse order. The string itself should not be reversed; it must be left in its original form. The method has the following header:   void printReverse(String s, int i) where s is a reference to the string, and i is an integer parameter that you may use as you see fit. You do not need to code up this method as...

  • Programming Assignment #7 (Recursion) This assignment is to write some methods that perform simple array operations...

    Programming Assignment #7 (Recursion) This assignment is to write some methods that perform simple array operations recursively. Specifically, you will write the bodies for the recursive methods of the ArrayRecursion class, available on the class web page. No credit will be given if any changes are made to ArrayRecursion.java, other than completing the method bodies Note that the public methods of ArrayRecursion – contains(), getIndexOfSmallest(), and sort() – cannot be recursive because they have no parameters. Each of these methods...

  • JAVA Recursion: For this assignment, you will be working with various methods to manipulate strings using...

    JAVA Recursion: For this assignment, you will be working with various methods to manipulate strings using recursion. The method signatures are included in the starter code below, with a more detailed explanation of what function the method should perform. You will be writing the following methods: stringClean() palindromeChecker() reverseString() totalWord() permutation() You will be using tools in the String class like .substring(), .charAt(), and .length() in all of these methods, so be careful with indices. If you get stuck, think...

  • The Language is Java GenericMethodTest (20) Download the OverloadedMethods.java program. Replace all the methods except main...

    The Language is Java GenericMethodTest (20) Download the OverloadedMethods.java program. Replace all the methods except main with a single generic method that produces the same output. Call the new file GenericMethodsTest.java. OverloadMethods.java: // Printing array elements using overloaded methods. public class OverloadedMethods { // method printArray to print Integer array public static void printArray(Integer[] inputArray) { // display array elements for (Integer element : inputArray) { System.out.printf("%s ", element); } System.out.printf("\n"); } // method printArray to print Double array public...

  • Write a recursive method in java to find GCD of two integers using Euclid's method. Integers...

    Write a recursive method in java to find GCD of two integers using Euclid's method. Integers can be positive or negative. public class Recursion {                 public static void main(String[] args) {                                 Recursion r = new Recursion();                                 System.out.println(“The GCD of 24 and 54 is “+r.findGCD(24,54)); //6                 }                 public int findGCD(int num1, int num2){                                 return -1;                 } }

  • Write java class “BinarySearch” that uses recursion to find the index of a target value in...

    Write java class “BinarySearch” that uses recursion to find the index of a target value in an ascending sorted array. If not found, the result is -1. The array has to be unsorted before you sort it. Use “ BinarySearchDriver.java” to drive the “BinarySearch” class /************************************************************* * BinarySearchDriver.java * Student Name * *. *************************************************************/ public class BinarySearchDriver { public static void main(String[] args) {     int[] array = new int[] {55, 88, 33, 5, 8, 12, 16, 23, 45}; /unsorted...

  • In Java programming language Please write code for the 6 methods below: Assume that Student class...

    In Java programming language Please write code for the 6 methods below: Assume that Student class has name, age, gpa, and major, constructor to initialize all data, method toString() to return string reresentation of student objects, getter methods to get age, major, and gpa, setter method to set age to given input, and method isHonors that returns boolean value true for honors students and false otherwise. 1) Write a method that accepts an array of student objects, and n, the...

  • Please Write the following program in c# You are to write an application which will create...

    Please Write the following program in c# You are to write an application which will create a company, add agents (their name, ID, and weekly sales) to that company, and printout the details of all agents in the company. The application will contain three classes: Agent.cs, Company.cs, and CompanyTest.cs (this class is already provided). Flow of the application: The CompanyTest class creates an object of the Company class and reserves space for five agents. At this point if you call...

  • Those are  JAVA Programs. Please write them in recursions , it requires the methods have to be...

    Those are  JAVA Programs. Please write them in recursions , it requires the methods have to be recursive(I understood how to write them by using for loop). Please don't use any syntax beyond chapter of Recursion. I only covered the materials to recursions and the materials before recursion chapter. Appreciated! #1) Write a recursive method writesquares that accepts an integer n and prints the first n squares with the odd squares in decending order, followed by the even squares in acending...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT