Big Java Late Objects
2nd Edition
ISBN: 9781119330455
Author: Horstmann
Publisher: WILEY
expand_more
expand_more
format_list_bulleted
Textbook Question
Chapter 5, Problem 10PE
Write a recursive method
public static String reverse(String str)
that computes the reverse of a string. For example, reverse("flow") should return "wolf". Hint: Reverse the substring starting at the second character, then add the first character at the end. For example, to reverse "flow", first reverse "low" to "wol", then add the "f" at the end.
Expert Solution & Answer
Want to see the full answer?
Check out a sample textbook solutionStudents have asked these similar questions
Write a recursive method that displaysa string reversely on the console using the following header:
public static void reverseDisplay(String value)
For example, reverseDisplay("abcd") displays dcba. Write a test programthat prompts the user to enter a string and displays its reversal.
Write a short recursive Java method that determines if a string s is a palindrome, that is, it is equal to its reverse. Examples of palindromes include 'racecar' and 'mom'. Test the method by asking the user to provide string entries to be checked. Hint: Check the equality of the first and last characters and recur (but be careful to return the correct value for both odd and even-length strings).
Write a recursive method called reverseString() that takes in a string as a parameter and returns the string in reversed order. The main method is provided to read a string from the user and call the reverseString() method.
Use Java.
Ex: If the input of the program is:
Hello
the reverseString() method returns and the program outputs:
Reverse of "Hello" is "olleH".
Ex: If the input of the program is:
Hello, world!
the reverseString() method returns and the program outputs:
Reverse of "Hello, world!" is "!dlrow ,olleH".
Hint: Move the first character to the end of the returning string and pass the remaining sub-string to the next reverseString() method call.
import java.util.Scanner;
public class LabProgram {
/* TODO: Write recursive reverseString() method here. */public static void main(String[] args) {Scanner scnr = new Scanner(System.in);String input, result;input = scnr.nextLine();result = reverseString(input); System.out.printf("Reverse of \"%s\" is \"%s\".", input, result);}}
Chapter 5 Solutions
Big Java Late Objects
Ch. 5.1 - Consider the method call Math.pow(3, 2). What are...Ch. 5.1 - What is the return value of the method call...Ch. 5.1 - The Math.ceil method in the Java standard library...Ch. 5.1 - It is possible to determine the answer to Self...Ch. 5.2 - What is the value of cubeVolume(3)?Ch. 5.2 - Prob. 6SCCh. 5.2 - Provide an alternate implementation of the body of...Ch. 5.2 - Declare a method squareArea that computes the area...Ch. 5.2 - Consider this method: public static int...Ch. 5.3 - What does this program print? Use a diagram like...
Ch. 5.3 - Prob. 11SCCh. 5.3 - What does this program print? Use a diagram like...Ch. 5.4 - Prob. 13SCCh. 5.4 - What does this method do? public static boolean...Ch. 5.4 - Implement the mystery method of Self Check 14 with...Ch. 5.5 - How do you generate the following printout, using...Ch. 5.5 - Prob. 17SCCh. 5.5 - Prob. 18SCCh. 5.5 - Prob. 19SCCh. 5.5 - The boxString method contains the code for...Ch. 5.6 - Consider the following statements: int...Ch. 5.6 - Consider this method that prints a page number on...Ch. 5.6 - Consider the following method that computes...Ch. 5.6 - The comment explains what the following loop does....Ch. 5.6 - In Self Check 24, you were asked to implement a...Ch. 5.7 - Explain how you can improve the intName method so...Ch. 5.7 - Prob. 27SCCh. 5.7 - What happens when you call intName(0)? How can you...Ch. 5.7 - Trace the method call intName(72), as described in...Ch. 5.7 - Prob. 30SCCh. 5.8 - Which lines are in the scope of the variable i...Ch. 5.8 - Which lines are in the scope of the parameter...Ch. 5.8 - The program declares two local variables with the...Ch. 5.8 - There is a scope error in the mystery method. How...Ch. 5.8 - Prob. 35SCCh. 5.9 - Consider this slight modification of the...Ch. 5.9 - Consider this recursive method: public static int...Ch. 5.9 - Consider this recursive method: public static int...Ch. 5.9 - Prob. 39SCCh. 5.9 - The intName method in Section 5.7 accepted...Ch. 5 - In which sequence are the lines of the Cubes.java...Ch. 5 - Write method headers for methods with the...Ch. 5 - Give examples of the following methods from the...Ch. 5 - Prob. 4RECh. 5 - Consider these methods: public static double...Ch. 5 - Prob. 6RECh. 5 - Design a method that prints a floating-point...Ch. 5 - Write pseudocode for a method that translates a...Ch. 5 - Describe the scope error in the following program...Ch. 5 - For each of the variables in the following...Ch. 5 - Prob. 11RECh. 5 - Perform a walkthrough of the intName method with...Ch. 5 - Consider the following method: public static int...Ch. 5 - Consider the following method that is intended to...Ch. 5 - Suppose an ancient civilization had constructed...Ch. 5 - Give pseudocode for a recursive method for...Ch. 5 - Give pseudocode for a recursive method that sorts...Ch. 5 - Write the following methods and provide a program...Ch. 5 - Write the following methods and provide a program...Ch. 5 - Prob. 4PECh. 5 - Prob. 5PECh. 5 - Prob. 6PECh. 5 - Prob. 7PECh. 5 - Prob. 8PECh. 5 - Write methods public static double...Ch. 5 - Write a recursive method public static String...Ch. 5 - Write a recursive method public static boolean...Ch. 5 - Use recursion to implement a method public static...Ch. 5 - Use recursion to determine the number of digits in...Ch. 5 - Write a method that computes the balance of a bank...Ch. 5 - Write a method that tests whether a file name...Ch. 5 - It is a well-known phenomenon that most people are...Ch. 5 - Prob. 3PPCh. 5 - Use recursion to compute an, where n is a positive...Ch. 5 - Leap years. Write a method public static boolean...Ch. 5 - In Exercise P3.13 you were asked to write a...Ch. 5 - Prob. 10PPCh. 5 - Write a program that reads two strings containing...Ch. 5 - Prob. 12PPCh. 5 - Write a program that reads words and arranges them...Ch. 5 - Prob. 14PPCh. 5 - Write a program that reads two fractions, adds...Ch. 5 - Write a program that prints the decimal expansion...Ch. 5 - Write a program that reads a decimal expansion...Ch. 5 - Write two methods public static void...Ch. 5 - Write a program that reads in the width and height...Ch. 5 - Repeat Exercise P5.19 with hexagonal circle...Ch. 5 - Postal bar codes. For faster sorting of letters,...Ch. 5 - Write a program that reads in a bar code (with :...Ch. 5 - Write a program that converts a Roman number such...Ch. 5 - A non-governmental organization needs a program to...Ch. 5 - Having a secure password is a very important...Ch. 5 - Prob. 30PPCh. 5 - Prob. 31PPCh. 5 - Electric wire, like that in the photo, is a...Ch. 5 - The drag force on a car is given by FD=12v2ACD...
Additional Engineering Textbook Solutions
Find more solutions based on key concepts
Big data Big data describes datasets with huge volumes that are beyond the ability of typical database manageme...
Management Information Systems: Managing the Digital Firm (15th Edition)
Consider the adage Never ask a question for which you do not want the answer. a. Is following that adage ethica...
Experiencing MIS
Define the term unique key and give an example.
Database Concepts (8th Edition)
Consider the adage Never ask a question for which you do not want the answer. a. Is following that adage ethica...
Experiencing MIS
Big data Big data describes datasets with huge volumes that are beyond the ability of typical database manageme...
Management Information Systems: Managing The Digital Firm (16th Edition)
5.5 Describe the four basic elements of counter-controlled iteration.
C++ How to Program (10th Edition)
Knowledge Booster
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.Similar questions
- Write a recursive method thatfinds the number of occurrences of a specified letter in a string using the followingmethod header: public static int count(String str, char a) For example, count("Welcome", 'e') returns 2. Write a test program thatprompts the user to enter a string and a character, and displays the number ofoccurrences for the character in the string.arrow_forwardWrite a static recursive method that returns the number of digits in theinteger passed to it as an argument of type int. Allow for both positiveand negative arguments. For example, –120 has three digits. Do not countleading zeros. Embed the method in a program, and test it.arrow_forwardJAVA Write a static recursive method evenFactors that takes as input two positive integers and prints the even factors of the first integer that are greater than or equal to the second integer. For example, evenFactors(18,1) prints 2, 6, and 18, since the even factors of 18 that are greater than or equal to 1 are 2,6, and 18.arrow_forward
- Write a recursive method that returns a string of all positive, prime numbers less than or equal to a given value n. Use this header: String primeList (int n) For example, primeList (10) returns "7532" because the range 10 down to 0 includes the prime numbers 7, 5, 3, 2. Here are a few more examples. Returned Input n Notes String Any value less than 2 the returned string is empty 2 "2" "32" 7. "7532" 10 "7532" returned string has values 13, 11, 7, 5, 3, 2 15 "13117532" Note: A prime number is a whole number greater than 1, and it is divisible only by itself or the number 1. For example, 2, 3, 5, 7, 11, 13, 17, etc are all prime numbers. Here is a method that checks if a given number is prime: boolean isPrime(int n) { /method returns true if n is prime, otherdse it returns false if(n<2) return false; for (Int 1- 2; i < n; i++) //condition could also be icMath.sqrt(n) if(n %i -- 0) return false; return true;arrow_forwardWrite a recursive method that will reverse the order of the characters in agiven string and return the result as a new string. For example, if "book" isthe argument, the result would be "koob".arrow_forwardWrite a program that has a main() function that calls a recursive method repeatPrint(String s, .....) that prints out the strings as shown in the example below: repeat Print ("CHICAGO", ....); C CH CHI CHIC CHICA CHICAG CHICAGO CHICAGO HICAGO ICAGO CAGO AGO GO 0arrow_forward
- 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_forwardDo not use static variables to implement recursive methods. USING JAVA: // P6 public static int countSubstrings(String s1, String s2) { } Write a recursive method countSubstrings that counts the number of non-overlapping occurrences of a string s2 in a string s1. Use the method indexOf() from String class. As an example, with s1=”abab” and s2=”ab”, countSubstrings returns 2. With s1=”aaabbaa” and s2=”aa”, countSubstrings returns 2. With s1=”aabbaa” and s2=”AA”, countSubStrings returns 0. Show the output on the following strings: s1 = “Java is a programming language originally developed by James Gosling at Sun Microsystems and released in 1995 as a core component of Sun Microsystems Java platform. The language derives much of its syntax from C and C++ but has a simpler object model and fewer low-level facilities. Java applications are typically compiled to bytecode (class file) that can run on any Java Virtual Machine (JVM) regardless of computer architecture.…arrow_forwardWrite a recursive method to determine whether a String contains a 'q' not immediately followed by a 'u' (ignoring capitalization). In other words: • the word does contain at least one 'q' • and that q is followed by anything except a 'u' Carefully review the provided driver program to see example test cases. The method header is: public static boolean qNotFollowedByU(String word)arrow_forward
- 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)].arrow_forwardWrite a recursive method tripleChar(String s, char c) that takes a string s and a character c. The method returns the string s with all the occurrences of c repeated 3 times.arrow_forwardWrite a recursive method to print all the permutations of astring. For example, for the string abc, the permutation isabcacbbacbcacabcba public static void displayPermutation(String s)public static void displayPermutation(String s1, String s2)The first method simply invokes displayPermutation(" ", s). The secondmethod uses a loop to move a character from s2 to s1 and recursively invokesit with new s1 and s2. The base case is that s2 is empty and prints s1 to theconsole.Write a test program that prompts the user to enter a string and displays all itspermutations.arrow_forward
arrow_back_ios
SEE MORE QUESTIONS
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
Introduction to Big O Notation and Time Complexity (Data Structures & Algorithms #7); Author: CS Dojo;https://www.youtube.com/watch?v=D6xkbGLQesk;License: Standard YouTube License, CC-BY