Database System Concepts
7th Edition
ISBN: 9780078022159
Author: Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher: McGraw-Hill Education
expand_more
expand_more
format_list_bulleted
Question
Java, I am not displaying my results correct. It should add up the digits in the string. input string |result for the sumIt Recursion functions and a findMax function that finds the largest number in a string
"1d2d3d" | 6 total
"55" |10 total
"xx" | 0 total
"12x8" |12 Max number
"012x88" |88 Max Number
"012x88ttttt9xe33ppp100" |100 Max Number public class Finder {
//Write two recursive functions, both of which will parse any length string that consists of digits and numbers. Both functions
//should be in the same class and have the following signatures.
//use the if/else statement , Find the base case and -1 till you get to base case
//recursive function that adds up the digits in the String
public static int sumIt(String s)
{
//if String length is less or equal to 1 retrun 1.
if (s.length()<= 1){
//use Integer.praseInt(s) to convert string to Integer
//returns the interger values
return Integer.parseInt(s);
}else{
//else if the CharAt(value in index at 0 = 1) is not equal to the last vaule in the string else {//return the numeric values of a char value + call the SumIt method with a substring = 1
return Character.getNumericValue(s.charAt(0) ) + sumIt(s.substring(1));
}
}
//write a recursion function that will find the largest interger in the string.
public int findMax(String s,int max)
{//check the characters of the string is greater than 0
max = 0;
//check the vales in the string , vaules = i
if(s.substring(0, 1).matches("[0-9]")) { // checks for number
int a = Integer.parseInt(s.substring(0, 1)); // convert String to Interger
//substring begin index, endindex)
if(a > max) {
return findMax(s.substring(1), a)-1; //if a is greater than the index value 1
}
else return findMax(s.substring(1), max)-1; // else we just use the old max
}
else return findMax(s.substring(1), max)-1; // in case its not a num
}
// return max; // return max when all characters are gone.
//input string result
//"1d2d3d" 6
//"55" 10
//"xx" 0
//input string result
//"12x8" 12
//"012x88" 88
//"012x88ttttt9xe33ppp100" 100
//Testing
public static void main(String[] args) {
String a = "12X8";
String b = "55";
String c = "xx";
String d = "12x8";
String e = "012x88";
String f = "012x88ttttt9xe33ppp100";
System.out.println (a);
System.out.println (b);
System.out.println (c);
System.out.println (d);
System.out.println (e);
System.out.println (f);
}
}
SAVE
AI-Generated Solution
info
AI-generated content may present inaccurate or offensive content that does not represent bartleby’s views.
Unlock instant AI solutions
Tap the button
to generate a solution
to generate a solution
Click the button to generate
a solution
a solution
Knowledge Booster
Similar questions
- Write a program named stars.py that has two functions for generating star polygons. One function should be implemented using an iteration (a for loop), and another function should be a recursion. It is optional to fill in your shapes. The functions for generating stars should be named star (size, n, d=2) and star_recursive (size, n, level, d=2), where size is the size of the polygon side (edge), n is the number of sides (or angles), d is a density or winding number that should be set to default value 2, and level is the level of recursion initially equal to n.arrow_forwardHow to attempt? Question: Given a string, write a recursive function which will print one character of a string at one time by removing white spaces and the following special characters [#../?]. Example: Input: Hello User Output: Recursive call 1: r Recursive call 2: e Recursive call 3: s Recursive call 4: U Recursive call 5: o Recursive call 6: 1 Recursive call 7:1 Recursive call 8: e Recursive call 9: Harrow_forwardWrite a recursive function that accepts a number and returns its factorial. b. Write a recursive function that accepts an array, its size and the index of the initial element as arguments. The function fills the array with the elements of the following sequence: n1 = 3, nk+1 = nk+35 c. Write an iterative and a recursive versions of the binary search. In C++ codingarrow_forward
- Given the following pseudocode: function fun(n) { var outer_count=0; var inner_count=0; var sum = 0; for (var i=0; i 0, as an expression of normal arithmetic and n, what is the value of outer_count that counts the number of times the outer loop executes? Enter an expression Fun.1.inner: Assuming n EN and n > 0, as an expression of normal arithmetic and n, what is the value of inner_count that counts the number of times the inner loop executes? Enter an expressionarrow_forwardPlease only do rec_sum_digits function in python using only recursive and no looparrow_forwardpython3 Write a recursive function called is_palindrome(string) that takes a string parameter and checks if it is a palindrome ignoring the spaces, if any, and returns True/False. Sample output:>>> print(is_palindrome("never odd or even"))True>>> print(is_palindrome("step on no pets"))Truearrow_forward
- Python: How do I create a recursive function asterisk with both recursive and base cases? so for n=3, you would have below; *** ** * code that I have: for i in range(n): print("*",end="")arrow_forwardplease code in python Write a recursive function to add a positive integer b to another number a, add(a, b), where only the unit 1 can be added, For example add(5, 9) will return 14. The pseudocode is: # Base case: if b is 1, you can just return a + 1 # General case: otherwise, return the sum of 1 and what is returned by adding a and b - 1.arrow_forwardCan this python code be done without using the isdigit() function. Please try to change the following code in the picture so it doesn't use isdigit().arrow_forward
- Need help Writing a function, countVowels, that accepts a string as an argument. countVowels should return the number of vowels in that string. Use recursion. example countVowels('Four score and seven years'); // => 9arrow_forwardhelp in pytyhonarrow_forwardWrite a recursive function that takes as a parameter a nonnegative integer and generates the following pattern of stars. If the nonnegative integer is 4, the pattern generated is as follows: **** *** ** * * ** *** **** Also, write a program that prompts the user to enter the number of lines in the pattern and uses the recursive function to generate the pattern. For example, specifying 4 as the number of lines generates the preceding pattern.arrow_forward
arrow_back_ios
arrow_forward_ios
Recommended textbooks for you
- Database System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- C How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education
Database System Concepts
Computer Science
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:McGraw-Hill Education
Starting Out with Python (4th Edition)
Computer Science
ISBN:9780134444321
Author:Tony Gaddis
Publisher:PEARSON
Digital Fundamentals (11th Edition)
Computer Science
ISBN:9780132737968
Author:Thomas L. Floyd
Publisher:PEARSON
C How to Program (8th Edition)
Computer Science
ISBN:9780133976892
Author:Paul J. Deitel, Harvey Deitel
Publisher:PEARSON
Database Systems: Design, Implementation, & Manag...
Computer Science
ISBN:9781337627900
Author:Carlos Coronel, Steven Morris
Publisher:Cengage Learning
Programmable Logic Controllers
Computer Science
ISBN:9780073373843
Author:Frank D. Petruzella
Publisher:McGraw-Hill Education