Question

Step 1: design and code a program that uses a recursive method to convert a String...

Step 1: design and code a program that uses a recursive method to convert a String of digit characters into an integer variable. For example, convert the character string13531” to an integer with the value 13,531. Note that the comma is present only to distinguish the integer value from the character string (in quotes) . The program should be repetitive to allow the user to enter any number of number strings, convert them, display them, and sum them.

When the user enters a sentinel value to terminate the input, display the sum and the calculated average of the numbers entered with one (1) decimal place.

To avoid the possibility of an overflow error, you may limit the String to eight (8) characters.

Step 2: design the recursive method to take in a number string as a parameter and return its integer value. The solution must be the result of the recursive process. It must NOT use an iterative process to obtain the return value. You are not allowed to use any method or function from the Java API or C++ STL. You must do the "parsing" in your own code in your own recursive method.

Remember, a “recursiveprocess obtains its results by calling itself repeatedly with differing values from the previous call. It ends (returns its result) when its input parameter detects the value of the “basecase. In this instance, the base case is when all the character digits have been converted.

There are two (2) methods that can easily be used in this process:

  • One is by decreasing the power of ten multiple when moving from the leftmost character to the right.
  • The other is to increase the sum by ten on each iteration before adding the current character (moving from left to right).

Step 3: Using the JAVADOC format, document the program and every method

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


ANSWER:-

import java.util.*;
class StringToInt{
   public static int convertToInt(String s,int i){
       int num = Character.getNumericValue(s.charAt(i));
       if(i == 0)
           return num;
       return num + convertToInt(s, i-1)*10;
   }
   public static void main(String[] args) {
       Scanner sc = new Scanner(System.in);
       System.out.println("Enter -1 to exit\n");
       int sum = 0,count = 0;
       float avg = 0.0f;
       String s;
       do{
           System.out.print("Enter No: ");
           s = sc.nextLine();
           if(!s.equals("-1")){

                // second argument is for accessing each digit in string
               sum += convertToInt(s,s.length()-1);
               count++;
           }
       }while(!s.equals("-1"));
       System.out.println("Sum = "+sum);
       System.out.printf("Average = %.1f",(float)sum/count);
   }
}

OUTPUT:-

NOTE:- If you have any doubts, please comment below. Please give positive rating. THANK YOU!!!!

Add a comment
Know the answer?
Add Answer to:
Step 1: design and code a program that uses a recursive method to convert a String...
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
  • Python program. Character-Analysis Program Problem Statement: Design a program - IN PYTHON - that asks the...

    Python program. Character-Analysis Program Problem Statement: Design a program - IN PYTHON - that asks the user to enter a string. The string will then be displayed back to the user. This is followed by determining the number of alphabetic characters, numeric characters, lower_case letters, upper_case letters, whitespace characters, and then displaying them. The user should be given additional chances to enter additional strings, and each time a string is entered, the above process is to be performed. The inputting...

  • Write a recursive function to convert a character string of digits to an integer. Example: convert(“1234”)...

    Write a recursive function to convert a character string of digits to an integer. Example: convert(“1234”) returns 1234. Hint: To convert a character to a number, subtract the ASCII value ‘0’ from the character. For example, if the string s has only one character, then the function can return the value s[0] – ‘0’.

  • Write the code in python programming Language String Statistics: Write a program that reads a string...

    Write the code in python programming Language String Statistics: Write a program that reads a string from the user and displays the following information about the string: (a) the length of the string, (b) a histogram detailing the number of occurrences of each vowel in the string (details provided below), (c) the number of times the first character of the string occurs throughout the entire string, (d) the number of times the last character of the string occurs throughout the...

  • Java Program Write a recursive method that takes a string and return a string where every...

    Java Program Write a recursive method that takes a string and return a string where every character appears twice. Output must match exactly as below: only two LL's since there is already two of the character For example, if the string is “HELLO”, it will return “HHEELLOO”. Write a program to test it. must be recursive!!!

  • Design a program in pseudocode that asks the user to enter a string containing a series...

    Design a program in pseudocode that asks the user to enter a string containing a series of ten single-digit numbers with nothing separating them. The program should display the sum of all the single digit numbers in the string except for the highest value digit. You may only use one main module and one sorting function; otherwise, do not modularize your program. For example, if the user enters 5612286470, the sum would be 33. (0+1+2+2+4+5+6+6+7) If there are multiple highest...

  • In C++ please! Please include .cpp and .hpp files! Thank you! Recursive Functions Goals Create and...

    In C++ please! Please include .cpp and .hpp files! Thank you! Recursive Functions Goals Create and use recursive functions In this lab, we will write a program that uses three recursive functions. Requirements: Important: You must use the array for this lab, no vectors allowed. First Recursive Function Write a function that recursively prints a string in reverse. The function has ONLY one parameter of type string. It prints the reversed character to the screen followed by a newline character....

  • Using Java only Implement a program that uses a recursive method to print a string backward....

    Using Java only Implement a program that uses a recursive method to print a string backward. Your program must contain a recursive method that prints the string backward. Use appropriate parameters in your method.

  • Write a program that uses a recursive function to determine whether a string is a character-unit...

    Write a program that uses a recursive function to determine whether a string is a character-unit palindrome. Moreover, the options for doing case sensitive comparisons and not ignoring spaces are indicated with flags. For example "A nut for a jar of tuna" is a palindrome if spaces are ignored and not otherwise. "Step on no pets" is a palindrome whether spaces are ignored or not, but is not a palindrome if it is case sensitive. Background Palindromes are character sequences...

  • I need java code for the following problem. Lab 7: Methods 1. Write a Java program called Numbers that calls the following methods and displays the returned value: Write a method called cubelt that a...

    I need java code for the following problem. Lab 7: Methods 1. Write a Java program called Numbers that calls the following methods and displays the returned value: Write a method called cubelt that accepts one integer parameter and returns the value raised to the third power as an integer. o Write a method called randominRange that accepts two integer parameters representing a range. The method returns a random integer in the specified range inclusive. 2. o Write a method...

  • Prompt the user to enter two character strings. The program will then create a new string...

    Prompt the user to enter two character strings. The program will then create a new string which has the string that appears second, alphabetically, appended to the end of the string that shows up first alphabetically. Use a dash (-') to separate the two strings. You must implement the function string appendStrings(string, string), where the return value is the combined string. Display the newly created string. Your program must be able to handle both uppercase and lowercase characters. You are...

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