Write a static recursive method that returns the number of digits in the integer passed to it as an argument of type int. Allow for both positive and negative arguments. For example, −120 has three digits. Do not count leading zeros. Embed the method in a
Want to see the full answer?
Check out a sample textbook solutionChapter 11 Solutions
Java: An Introduction to Problem Solving and Programming (8th Edition)
Additional Engineering Textbook Solutions
Problem Solving with C++ (10th Edition)
Starting Out with Java: From Control Structures through Data Structures (4th Edition) (What's New in Computer Science)
Java How to Program, Early Objects (11th Edition) (Deitel: How to Program)
Introduction to Java Programming and Data Structures, Comprehensive Version (11th Edition)
Web Development and Design Foundations with HTML5 (9th Edition) (What's New in Computer Science)
Starting Out with C++: Early Objects
- Write a recursive method called drawTriangle() that outputs lines of '*' to form an upside down isosceles triangle. Method drawTriangle() has one parameter, an integer representing the base length of the triangle. Assume the base length is always odd and less than 20. Output 9 spaces before the first '*' on the last line for correct formatting. Hint: The number of '*' decreases by 2 for every line drawn. Ex: If the input of the program is: 3 the method drawTriangle() outputs: *** * Ex: If the input of the program is: 19 the method drawTriangle() outputs: ******************* ***************** *************** ************* *********** ********* ******* ***** *** * Note: No space is output before the first '*' on the first line when the base length is 19.arrow_forwardWrite and test the following recursive methods:1. A method that, for a positive integer n, prints odd numbers between 1 and n.2. A method that, for a positive integer n, prints odd numbers between n and 1.3. A method to add the first n terms of the series: 1 +12−13+14−15…1?4. A recursive version of the following method:void cubes(int n) {for (int i = 1; i <= n; i++)System.out.print(i * i * i, " ");}arrow_forwardWrite a Java program that asks the user to enter a string and counts how many times the letter "s" or "S" appears in the string. Display that number and then display the string that many times. Requirements: Call a method that gets the string from the user. Use recursion to ensure the string has at least 5 characters. Call a method that returns the number of times the letter "s" or "S" appears in the string. This method should use recursion. Call a method that takes the number of times the letter"s" or "S" appeared in the string and display the original string that was obtained from the user that many times using recursion. Call a method that sorts the letters in the string and searches for the letter "m". Display Found or Not Found.arrow_forward
- Create a class named RecusiveMethod having a recursive method. The recursive method accepts an integer argument and displays all even numbers from 1 up to the number passed as an argument. For example, if 25 is passed as an argument, the method will display 2, 4, 6, 8, 10, 12, 14, ……24. Demonstrate the method.arrow_forwardComplete the java program. Use two methods and make one of them recursivearrow_forwardWrite a recursive method that parses a binary number as astring into a decimal integer. The method header is public static int bin2Dec(String binaryString) Write a test program that prompts the user to enter a binary string and displaysits decimal equivalent.arrow_forward
- 8.Write a static method recReplace that takes a String str, a String sub1, and a String sub2 as parameters, and replaces all occurrences of sub1 in str with sub2. The method should be recursive, which means no loops. public static String recReplace(String str, String sub1, String sub2) {arrow_forwardWrite down only the methods that are required; testers are not requiredarrow_forwardJAVA Question 2: For two integers m and n, their GCD (Greatest Common Divisor) can be computed by a recursive method. Write a recursive method gcd(m,n) to find their Greatest Common Divisor. Method body: If m is 0, the method returns n. If n is 0, the method returns m. If neither is 0, the method can recursively calculate the Greatest Common Divisor with two smaller parameters: One is n, the second one is m mod n (or m % n). The recursive method cannot have loops. Note: although there are other approaches to calculate Greatest Common Divisor, please follow the instructions in this question, otherwise you will not get the credit. main method: Prompt and read in two numbers to find the greatest common divisor. Call the gcd method with the two numbers as its argument. Print the result to the monitor. Example program run: Enter m: 12 Enter n: 28 GCD(12,28) = 4 And here is what I have so far, package CSCI1302;import java.util.*;public class RecursionDemo { public static void…arrow_forward
- help create a Java recursive method that determines if a number is a prime numberarrow_forwardWrite a recursive Java method that takes a String as a parameter and returns true if the String is a palindrome. You may assume that the string only contains lower case characters (i.e. no numbers, spaces, punctuation, etc).arrow_forwardJava Program: Recursive Method There are n people in a room where n is an integer greater then or equal to 2. Each person shakes hands once with every other person. What is the total number of handshakes in the room? Write a recursive method to solve this problem with the following header:public static int handshake(int n)where handshake(n) returns the total number of handshakes for n people in the room. To get you started if there are only one or two people in the room, then:handshake(1)=0handshake(2)=1arrow_forward
- Programming Logic & Design ComprehensiveComputer ScienceISBN:9781337669405Author:FARRELLPublisher:Cengage