Question

In: Computer Science

Please write in java: Write a recursive method toNumber that forms the integer sum of all...

Please write in java: Write a recursive method toNumber that forms the integer sum of all digit characters in a string. For example, the result of toNumber("3ac4") would be 7. Hint: If next is a digit character ('0' through '9'), Character.isDigit(next) is true and the numeric value of next is Character. digit(next, 10).

Solutions

Expert Solution

Java Program :

public class Main
{
    // Recursive function toNumber(s)
    public static int toNumber(String s)
    {
        if(s.isEmpty())          // Base case : return 0 if string is empty
        {
            return 0;
        }
        else                     // string is not empty
        {
            if(Character.isDigit(s.charAt(0))) // check if character is digit from '0' to '9'
            {
                // subract '0' from digit to convert character to integer digit 
                return s.charAt(0) - '0' + toNumber(s.substring(1)); // call the recursive function with remaining string 
            }
            else 
            {
                return toNumber(s.substring(1)); // if not a digit then simply call the recursive function again
            }
        }
    }
        public static void main(String[] args) {
                System.out.println(toNumber("3ac4"));
        }
}

Screenshot :


Related Solutions

Write, in Java, a recursive method countBinaryStrings that has one integer parameter n and returns the...
Write, in Java, a recursive method countBinaryStrings that has one integer parameter n and returns the number of binary strings of length n that do not have two consecutive 0’s. For example, for n = 4, the number of binary strings of length 4 that do not contain two consecutive 0’s is 8: 1111, 1110, 1101, 1011, 1010, 0111, 0110, 0101. For this problem, your method needs to return only the number of such strings, not the strings themselves. You...
java Write a recursive program to reverse a positive integer. . Your method should take a...
java Write a recursive program to reverse a positive integer. . Your method should take a non negative integer as a parameter and return the reverse of the number as an integer. e.g. if you pass 12345, your method should return 54321.
Write a RECURSIVE method that receives as a parameter an integer named n. The method will...
Write a RECURSIVE method that receives as a parameter an integer named n. The method will output n # of lines of stars. For example, the first line will have one star, the second line will have two stars, and so on. The line number n will have "n" number of ****** (stars) so if n is 3 it would print * ** *** The method must not have any loops!
write a recursive method that returns the product of all elements in java linked list
write a recursive method that returns the product of all elements in java linked list
In java, Write a recursive function to calculate the sum of the nodes only on even...
In java, Write a recursive function to calculate the sum of the nodes only on even levels of the subtree. please do not add any parameters to do this function. private int sumEvenLevels(Node current){ //you can only pass in root. //code }
- Write a method sum that expects a List<Integer> as a parameter. The method returns an...
- Write a method sum that expects a List<Integer> as a parameter. The method returns an int representing the sum of the integers in the list. - Write an index-based loop that prints the contents of a list.
Write a recursive method to sum the values in an array of integers. Create a file...
Write a recursive method to sum the values in an array of integers. Create a file ArraySum.java and add the recursive method public int sumOfArray (). Use the driver class ArraySumDriver.java to populate your array and demonstrate that your method works. ////ArraySumDriver.java/////// public class ArraySumDriver { private final static int ARRAY_SIZE = 6; /** * @param args */ public static void main(String[] args) { int index = 0; Integer[] myArray = new Integer[ARRAY_SIZE]; ArraySum arraySum = new ArraySum(); myArray[index++] =...
Write a recursive method to implement Binary Search of a sorted integer array. Signature of method...
Write a recursive method to implement Binary Search of a sorted integer array. Signature of method could be public int BinarySearch(int target, int low, int high)
Write a recursive method to implement Binary Search of a sorted integer array. Signature of method...
Write a recursive method to implement Binary Search of a sorted integer array. Signature of method could be public int BinarySearch(int target, int low, int high)
JAVA PLEASE Write a recursive function that does the following: Given a number, add all the...
JAVA PLEASE Write a recursive function that does the following: Given a number, add all the digits and display the sum. Example: ​​The sum of the number 5432 would be 14. o Do not use the static modifier. No global variables. Your program should implement a non-tail recursive algorithm. In other words, it should do something as it moves towards the base case, the tail, and also do something as it comes back from the tail to the beginning. o...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT