Question

In java, please complete the methods in the class below. Add comments to identify your base...

In java, please complete the methods in the class below. Add comments to identify your base case or base cases and the recursive steps in each of the recursive implementations.

a. public String starsRecursive(int numOfLinesOfStars) takes the number of lines of stars to be in the returned string. The first line will have as many stars as numOfLinesOfStars. Each line after the first line will have one less star. The last line will have only one star. If the value of numOfLinesOfStars is zero or negative, then no starts are produced. This method must be implemented recursively.

e.g. System.out.println(A6.starsRecursive(6));

******
*****
****

***

**

*

b. public String starsIterative(int numOfLinesOfStars) takes the number of lines of stars to be in the returned string. The first line will have as many stars as numOfLinesOfStars. Each line after the first line will have one less star. The last line will have only one star. If the value of numOfLinesOfStars is zero or negative, then no starts are produced. This method must be implemented iteratively.

e.g. System.out.println(A6.starsIterative(6));

******
*****
****

***

**

*

c. public String reverseAndAddCommasRecursive(String wordToReverse) takes a string, reverses characters of the string while inserting commas between each characters. This method must be implemented recursively.

e.g.

System.out.println(A6.reverseAndAddCommasRecursive("cat")); ,t,a,c

d. public String reverseAndAddCommasIterative(String wordToReverse) takes a string, reverses characters of the string while inserting commas between each characters. This method must be implemented iteratively.

e.g.
System.out.println(A6.reverseAndAddCommasIterative ("cat")); ,t,a,c

e. public int multiplyOddIntegersOnlyRecursive(int highestIntInSequence) takes an integer value and multiples all the odd integers between 1 and highestIntInSequence. It returns the result of the multiplication of the odd integers. This method must be implemented recursively.

e.g. System.out.println(A6.multiplyOddIntegersOnlyRecursive(15));

2027025

f. public int multiplyOddIntegersOnlyIterative(int highestIntInSequence) takes an integer value and multiples all the odd integers between 1 and highestIntInSequence. It returns the result of the multiplication of the odd integers. This method must be implemented iteratively.
e.g. System.out.println(A6.multiplyOddIntegersOnlyIterative(15));

2027025

Driver:

package driver;

import assignment6.Assign6;

public class A6Driiver {

public static void main(String[] args) {

Assign6 A6 = new Assign6();

System.out.println("\nPrinting stars recursively:\n"+A6.starsRecursive(6));

System.out.println("\nPrinting stars iteratively:\n"+A6.starsIterative(6));

System.out.println("\nPrinting reverse with commas recursively:\n"+A6.reverseAndAddCommasRecursive("cat"));

System.out.println("\nPrinting reverse with commas iterativey:\n"+A6.reverseAndAddCommasIterative("dogs"));

System.out.println("\nPrinting multiply odd ints only recursively:\n"+A6.multiplyOddIntegersOnlyRecursive(15));

System.out.println("\nPrinting multiply odd ints only iteratively:\n"+A6.multiplyOddIntegersOnlyIterative(15));

}

}

Example output from A6Driver shown below:

Printing stars recursively:
******
*****
****

***

**

*

Printing stars iteratively:
******
*****
****

***

**

*

Printing reverse with commas recursively:
,t,a,c
Printing reverse with commas iterativey:
,s,g,o,d
Printing multiply odd ints only recursively:
2027025
Printing multiply odd ints only iteratively:
2027025
0 0
Add a comment Improve this question Transcribed image text
Answer #1

I have written the complete java code with the above 6 functions implemented.

For the multiply integers using recursion, i have used methods to convert string to integer and integer to string as the return type of the given function is string but we need to return the integer values.

Note: You may look the screenshot of the code to have better understanding of the indentation.

Java Code:

package recursion_iteration_functions;

class Assign6
{

   public String starsRecursive(int i) {
       //base case
       if(i <= 0)
           return "";
       else //else print i number of stars
       {
           for(int j=0;j<i;j++)
               System.out.print("*");
           System.out.println(); //new line
           this.starsRecursive(i-1);
       }
       return "";
   }

   public String starsIterative(int i) {
       int n = i;
       for(int u=0;u<i;u++)
       {
           for(int j=0;j<n;j++)
               System.out.print("*");
           System.out.println(); //new line
           n--;
       }
       return "";
   }

   public String reverseAndAddCommasRecursive(String string) {
       //base case- empty string
       if(string.length() == 0)
           return "";
       else
       {
           int n = string.length();
           //System.out.print(",");
           System.out.print(","+string.charAt(n-1));
           //System.out.println("String is: "+string.substring(0,n-1));
           reverseAndAddCommasRecursive(string.substring(0,n-1));
           return "";
       }
   }
  
   public String reverseAndAddCommasIterative(String string) {
       int len = string.length(); //length of string
       for(int i=len-1;i>=0;i--)
       {
           System.out.print(","+string.charAt(i));
       }
       System.out.println(); //new line
       return "";
   }

   public String multiplyOddIntegersOnlyRecursive(int i) {
       //base case
       if(i==1)
       {
           return "1";
       }
       else
       {
           if(i%2 == 0) //check if even number is send
               i--;
           //convert string to long then multiply numbers and
           //then again convert back result to string to return
           //as the return type is string
           return Integer.toString(i*Integer.parseInt(multiplyOddIntegersOnlyRecursive(i-2)));
       }
   }

   public String multiplyOddIntegersOnlyIterative(int i) {
       long res = 1; //stores result
       for(int j=1;j<=i;j+=2)
       {
           res = res*j;
       }
       System.out.println(res);
       return "";
   }
  
}

public class A6Driiver {
  
   public static void main(String[] args) {

       Assign6 A6 = new Assign6();

       System.out.println("\nPrinting stars recursively:");
       A6.starsRecursive(6);

       System.out.println("\nPrinting stars iteratively:");
       A6.starsIterative(6);

       System.out.println("\nPrinting reverse with commas recursively:");
       A6.reverseAndAddCommasRecursive("cat");

       System.out.println("\nPrinting reverse with commas iterativey:");
       A6.reverseAndAddCommasIterative("dogs");

       String res = A6.multiplyOddIntegersOnlyRecursive(15);
       System.out.println("\nPrinting multiply odd ints only recursively:\n"+res);
      

       System.out.println("\nPrinting multiply odd ints only iteratively:");
       A6.multiplyOddIntegersOnlyIterative(15);

       }
}

Output:

Screenshot of code:

Add a comment
Know the answer?
Add Answer to:
In java, please complete the methods in the class below. Add comments to identify your base...
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
  • 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...

  • To do: Now add code to Pet and PetPlay to complete the comments in the code....

    To do: Now add code to Pet and PetPlay to complete the comments in the code. import java.util.ArrayList; public class PetPlay {    //-----------------------------------------------------------------    // Stores and modifies a list of pets    //----------------------------------------------------------------- public static void main (String[] args)    { // always make your array lists generic ArrayList pets = new ArrayList(); pets.add(new Pet("dog", "Bella")); pets.add(new Pet("cat","Blackie")); pets.add(new Pet("bird", "Babs")); pets.add(new Pet("dog", "Lassie")); pets.add(new Pet("horse", "Sam")); pets.add(new Pet("dog", "Blackie")); pets.add(new Pet("turtle", "Joe")); pets.add(new Pet("bird","Annie")); pets.add(new Pet("bird", "Alex"));...

  • Please help me. package a1; public class Methods {    /*    * Instructions for students:...

    Please help me. package a1; public class Methods {    /*    * Instructions for students: Use the main method only to make calls to the    * other methods and to print some testing results. The correct operation of    * your methods should not depend in any way on the code in main.    *    * Do not do any printing within the method bodies, except the main method.    *    * Leave your testing code...

  • JAVA project PLEASE complete/ create project with comments in the programming explaining everything Text file Year...

    JAVA project PLEASE complete/ create project with comments in the programming explaining everything Text file Year of storm/ Name of storm/ mmdd storm started/ mmdd storm ended/ magnitude of storm/ //made up data 2004/Ali/1212/1219/1 2003/Bob/1123/1222/3 1980/Sarah/0123/0312/0 1956/Michael/1211/1223/4 1988/Ryan/0926/1019/ 1976/Tim/0318/1010/0 2006/Ronald/0919/1012/2 1996/Mona/0707/0723/1 2000/Kim/0101/0201/1 2001/Jim/1101/1201/3 Text file Class storm{ private String nameStorm; private int yearStorm; private int startStorm; private int endStorm; private int magStorm; public storm(String name, int year, int start, int end, int mag){ nameStorm = name; yearStorm = year; startStorm...

  • Hi I really need help with the methods for this lab for a computer science class....

    Hi I really need help with the methods for this lab for a computer science class. Thanks Main (WordTester - the application) is complete and only requires uncommenting to test the Word and Words classes as they are completed. The needed imports, class headings, method headings, and block comments are provided for the remaining classes. Javadocs are also provided for the Word and Words classes. Word The Word class represents a single word. It is immutable. You have been provided...

  • I have provided a main method. Please add your fraction class. You need constructors, add, subtract,...

    I have provided a main method. Please add your fraction class. You need constructors, add, subtract, multiply, and divide methods, and a toString method. Your toString method needs to return the numerator followed by / followed by the denominator - NO spaces. DO NOT make any attempt to reduce the fractions (we shall do that later). Please add comments throughout. import java.util.Scanner; public class H4 { public static class Fraction { } public static void main(String[] args) { Fraction f=...

  • DIRECTIONS FOR THE WHOLE PROJECT BELOW BigInt class The purpose of the BigInt class is to...

    DIRECTIONS FOR THE WHOLE PROJECT BELOW BigInt class The purpose of the BigInt class is to solve the problem using short methods that work together to solve the operations of add, subtract multiply and divide.   A constructor can call a method called setSignAndRemoveItIfItIsThere(). It receives the string that was sent to the constructor and sets a boolean variable positive to true or false and then returns a string without the sign that can then be processed by the constructor to...

  • 10. Recursive Append Download the file AppendRec.java. Write your code inside the appendNTimes method and use...

    10. Recursive Append Download the file AppendRec.java. Write your code inside the appendNTimes method and use the main method to test your code. Submit the file AppendRec.java. There is no need to delete the main method, the autograder will only grade appendNTimes. The method appendNTimes is recursive and takes two arguments, a string and an integer. It returns the original string appended to the original string n times. The method signature is as follows public static String appendNTimes ( String...

  • Check if an array is a heap in Java. Complete the method isHeapTree Together, these methods...

    Check if an array is a heap in Java. Complete the method isHeapTree Together, these methods are meant to determine if an array of objects corresponds to a heap. The methods are generic, and they should work with an array of any type T that implement Comparable<T>. Implement the second method so that it uses recursion to process the complete tree/subtree whose root is at position i in the array arr. The method should return true if that tree/subtree is...

  • in java coorect this code & screenshoot your output ---------------------------------------------------------------------- public class UNOGame {     /**...

    in java coorect this code & screenshoot your output ---------------------------------------------------------------------- public class UNOGame {     /**      * @param args the command line arguments      */         public static void main(String[] args) {       Scanner input=new Scanner(System.in);          Scanner input2=new Scanner(System.in);                             UNOCard c=new UNOCard ();                UNOCard D=new UNOCard ();                 Queue Q=new Queue();                           listplayer ll=new listplayer();                           System.out.println("Enter Players Name :\n Click STOP To Start Game..");        String Name = input.nextLine();...

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