Question

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 order For example writeSquares(8); prints o 49 25914 16 36 64 writeSquares(11); prints o 121 81 49 25 91 4 16 36 64 100 writeSqures(1) prints writeSqures(O) (or any negative number) prints o nothing #2) Write a recursive method isPalindrome that accepts a String and returns true if it reads the same forwards as backwards. o isPalindrome(madam) o isPalindrome(racecar) o isPalindrome(step on no pets) o isPalindrome(able was I ere l saw elba) o isPalindrome(Java) true true true true false

0 0
Add a comment Improve this question Transcribed image text
Answer #1
public class RecursiveMethodsUtil {

    public static void writeSqaures(int n) {
        if(n == 1) {
            System.out.print("1 ");
        } else if(n > 1) {
            if(n % 2 == 0) {
                System.out.print((n-1)*(n-1) + " ");
                writeSqaures(n-2);
                System.out.print(n*n + " ");
            } else {
                System.out.print(n*n + " ");
                writeSqaures(n-2);
                System.out.print((n-1)*(n-1) + " ");
            }
        }
    }

    public static boolean isPalindrome(String s) {
        if(s.length() <= 1) {
            return true;
        } else {
            if(s.charAt(0) != s.charAt(s.length()-1)) {
                return false;
            } else {
                return isPalindrome(s.substring(1, s.length()-1));
            }
        }
    }

    public static void main(String[] args) {
        writeSqaures(8);
        System.out.println();
        writeSqaures(11);
        System.out.println();
        writeSqaures(1);
        System.out.println();
        writeSqaures(0);
        System.out.println();

        System.out.println(isPalindrome("madam"));
        System.out.println(isPalindrome("racecar"));
        System.out.println(isPalindrome("step on no pets"));
        System.out.println(isPalindrome("Java"));
    }

}

49 25 91416 36 64 121 81 49 2591416 36 64 100 false

Add a comment
Know the answer?
Add Answer to:
Those are  JAVA Programs. Please write them in recursions , it requires the methods have to be...
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
  • Please write in java Write a main program that runs the following two recursive methods demonstrating...

    Please write in java Write a main program that runs the following two recursive methods demonstrating that they work. #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 order. For example • writeSquares(8): prints . 49 25 9 1 4 16 36 64 • writeSquares(11); prints 121 81 49 25 9 1 2 16 36 64 100 •...

  • Please i need programs in C 32) Write a function that, given a string, a width,...

    Please i need programs in C 32) Write a function that, given a string, a width, and an empty string for output, centers the string in the output, centers the string in the output area. The function is to return 1 if the formating is successful and 0 if any errors, such as string length greater than width, are formed. 35) Write a function called newStrCmp that does the same job as strcmp. The declaration for your functions is to...

  • can someone please help me with this. I need to use java. Recursion Write and run...

    can someone please help me with this. I need to use java. Recursion Write and run a program that reads in a set of numbers and stores them in a sequential array. It then calls a recursive function to sort the elements of the array in ascending order. When the sorting is done, the main function prints out the elements of the newly sorted array Hint: How do you sort an array recursively? Place the smallest element of the array...

  • Create two Java classes called Recursive, RecursiveDemo. Write four methods a- int sum_sqr_rec(stack<int> stk) which will...

    Create two Java classes called Recursive, RecursiveDemo. Write four methods a- int sum_sqr_rec(stack<int> stk) which will receive a stack of "int" and output the sum of the squares of the elements in the stack. b- int plus_minus_rec(stack<int> stk) which will receive a stack of "int" (example: {a,b,c,d,e,f,g,h,i,j}) and output the sum of the elements in the stack as follows: a - b + c - d + e - f + g - h + i -j c- void prt_chars_rev_rec(stack<char>...

  • 3. Write Java methods to accomplish each of the following Write a method named simpleAry which...

    3. Write Java methods to accomplish each of the following Write a method named simpleAry which accepts and return nothing. It creates an array that can hold ten integers. Fill up each slot of the array with the number 113. Then, display the contents of the array on the screen. You must use a loop to put the values in the array and also to display them. Write a method named sury which accepts an array of type double with...

  • write in java 1. Assume the availability of a method  named  makeLine that can be passed a non-negative...

    write in java 1. Assume the availability of a method  named  makeLine that can be passed a non-negative integer  n and a character  c and return a String consisting of n identical characters that are all equal to c. Write a method  named  printTriangle that receives two integer  parameters  n and k. If n is negative the method does nothing. If n happens to be an even number, itsvalue is raised to the next odd number (e.g. 4-->5). Then, when k has the value zero, the method prints...

  • Assignment 6, Merge Arrays (java) Instructions In this assignment, you will write a program which merges...

    Assignment 6, Merge Arrays (java) Instructions In this assignment, you will write a program which merges two arrays of positive integers and removes any duplicate entries. Your program will first ask for a valid length which must be an integer which is 10 or greater. The program should continue to ask until a valid length is entered. The program will then create two arrays of the length entered, fill these with random integers between 1 and 100 inclusive, and print...

  • JAVA LANG PLEASE: I have follwed these below guidelines but when i run my queue test...

    JAVA LANG PLEASE: I have follwed these below guidelines but when i run my queue test it is not executing but my stack is working fine, can you fix it please! MyQueue.java Implement a queue using the MyStack.java implementation as your data structure.  In other words, your instance variable to hold the queue items will be a MyStack class. enqueue(String item): inserts item into the queue dequeue(): returns and deletes the first element in the queue isEmpty(): returns true or false...

  • CMPS 12B Introduction to Data Structures Programming Assignment 2 In this project, you will write...

    can i get some help with this program CMPS 12B Introduction to Data Structures Programming Assignment 2 In this project, you will write a Java program that uses recursion to find all solutions to the n-Queens problem, for 1 Sns 15. (Students who took CMPS 12A from me worked on an iterative, non-recursive approach to this same problem. You can see it at https://classes.soe.ucsc.edu/cmps012a/Spring l8/pa5.pdf.) Begin by reading the Wikipcdia article on the Eight Queens puzzle at: http://en.wikipedia.org/wiki/Eight queens_puzzle In...

  • Please answer the whole question, I need them all I will give thumbs up This is...

    Please answer the whole question, I need them all I will give thumbs up This is should be the TAMPALMS.txt (1.292 KB) Property   Market_Val   Sale_Price 1   181.44   382.0 2   191.00   230.0 3   159.83   220.0 4   189.22   277.0 5   151.61   205.0 6   166.40   250.0 7   157.09   235.0 8   211.74   284.0 9   146.45   247.7 10   131.80   159.0 11   131.05   200.0 12   191.98   285.0 13   138.85   170.0 14   147.95   215.0 15   121.98   149.0 16   113.08   165.0 17   138.02   205.0 18   162.65   262.5 19  ...

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