Starting Out with Java: From Control Structures through Data Structures (4th Edition) (What's New in Computer Science)
4th Edition
ISBN: 9780134787961
Author: Tony Gaddis, Godfrey Muganda
Publisher: PEARSON
expand_more
expand_more
format_list_bulleted
Textbook Question
Chapter 15, Problem 6AW
Convert the following iterative method to one that uses recursion:
public static void sign(int n)
{
while (n > 0)
{
System.out.println("No Parking");
n--;
}
}
Expert Solution & Answer
Want to see the full answer?
Check out a sample textbook solutionStudents have asked these similar questions
import java.util.Scanner;
public class LabProgram { // Recursive method to draw the triangle public static void drawTriangle(int baseLength, int currentLength) { if (currentLength <= 0) { return; // Base case: stop when currentLength is 0 or negative }
// Calculate the number of spaces needed for formatting int spaces = (baseLength - currentLength) / 2;
if (currentLength == baseLength) { // If it's the first line, don't output spaces before the first '*' System.out.println(" ".repeat(spaces) + "*".repeat(currentLength)); } else { // Output spaces and asterisks System.out.println(" ".repeat(spaces) + "*".repeat(currentLength)); }
// Recursively call drawTriangle with the reduced currentLength drawTriangle(baseLength, currentLength - 2); }
public static void drawTriangle(int baseLength) { drawTriangle(baseLength, baseLength); }
public…
import java.util.Scanner;
public class LabProgram { // Recursive method to draw the triangle public static void drawTriangle(int baseLength, int currentLength) { if (currentLength <= 0) { return; // Base case: stop when currentLength is 0 or negative }
// Calculate the number of spaces needed for formatting int spaces = (baseLength - currentLength) / 2;
if (currentLength == baseLength) { // If it's the first line, don't output spaces before the first '*' System.out.println("*".repeat(currentLength) + " "); } else { // Output spaces and asterisks System.out.println(" ".repeat(spaces) + "*".repeat(currentLength) + " "); }
// Recursively call drawTriangle with the reduced currentLength drawTriangle(baseLength, currentLength - 2); } public static void drawTriangle(int baseLength) { drawTriangle(baseLength, baseLength); }
public static…
10.
public static void methodl (int i, int num) {
for (int j = 1; j <= i; j++) {
" ");
System.out.print (num +
num *= 2;
}
System.out.println();
}
public static void main(String[] args) {
int i = 1;
while (i <= 6) {
methodl (i, 2);
i++;
}
Chapter 15 Solutions
Starting Out with Java: From Control Structures through Data Structures (4th Edition) (What's New in Computer Science)
Ch. 15.2 - It is said that a recursive algorithm has more...Ch. 15.2 - Prob. 15.2CPCh. 15.2 - What is a recursive case?Ch. 15.2 - What causes a recursive algorithm to stop calling...Ch. 15.2 - What is direct recursion? What is indirect...Ch. 15 - Prob. 1MCCh. 15 - This is the part of a problem that can be solved...Ch. 15 - This is the part of a problem that is solved with...Ch. 15 - This is when a method explicitly calls itself. a....Ch. 15 - Prob. 5MC
Ch. 15 - Prob. 6MCCh. 15 - True or False: An iterative algorithm will usually...Ch. 15 - True or False: Some problems can be solved through...Ch. 15 - True or False: It is not necessary to have a base...Ch. 15 - True or False: In the base case, a recursive...Ch. 15 - Find the error in the following program: public...Ch. 15 - Prob. 1AWCh. 15 - Prob. 2AWCh. 15 - What will the following program display? public...Ch. 15 - Prob. 4AWCh. 15 - What will the following program display? public...Ch. 15 - Convert the following iterative method to one that...Ch. 15 - Write an iterative version (using a loop instead...Ch. 15 - What is the difference between an iterative...Ch. 15 - What is a recursive algorithms base case? What is...Ch. 15 - What is the base case of each of the recursive...Ch. 15 - What type of recursive method do you think would...Ch. 15 - Which repetition approach is less efficient: a...Ch. 15 - When recursion is used to solve a problem, why...Ch. 15 - How is a problem usually reduced with a recursive...Ch. 15 - Prob. 1PCCh. 15 - isMember Method Write a recursive boolean method...Ch. 15 - String Reverser Write a recursive method that...Ch. 15 - maxElement Method Write a method named maxElement,...Ch. 15 - Palindrome Detector A palindrome is any word,...Ch. 15 - Character Counter Write a method that uses...Ch. 15 - Recursive Power Method Write a method that uses...Ch. 15 - Sum of Numbers Write a method that accepts an...Ch. 15 - Ackermarms Function Ackermanns function is a...Ch. 15 - Recursive Population Class In Programming...
Additional Engineering Textbook Solutions
Find more solutions based on key concepts
The ____________ is always transparent.
Web Development and Design Foundations with HTML5 (8th Edition)
Test Average Write a program that asks the user to enter three test scores. The program should display each tes...
Starting Out with Java: Early Objects (6th Edition)
What would be the output in Self-Test Exercise 5 if the assignment were changed to the following? intextra=0;
Absolute Java (6th Edition)
Using an example of a component that implements an abstract data type such as a stack or a list, show why it is...
Software Engineering (10th Edition)
What is the evaluation order for the Boolean operators (AND, OR, NOT) in an SQL command? How can a query writer...
Modern Database Management (12th Edition)
What do the Ada and COBOL languages have in common?
Concepts Of Programming Languages
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
- Code 16-1 /** This class has a recursive method. */ public class EndlessRecursion { public static void message() { System.out.println("This is a recursive method."); message(); } } Code 16.2 /** This class has a recursive method message, which displays a message n times. */ public class Recursive { public static void messge (int n) { if (n>0) { System.out.println (" This is a recursive method."); message(n-1); } } } Task #1 Tracing Recursive Methods 1. Copy the file Recursion.java (see Code Listing 16.1) from the Student Files or as directed by your instructor. 2. Run the program to confirm that the generated answer is correct. Modify the factorial method in the following ways: a. Add these lines above the first if statement: int temp; System.out.println("Method call -- " + "calculating " + "Factorial of: " + n); Copyright © 2019 Pearson Education, Inc., Hoboken NJ b. Remove this line in the recursive section at the end of the method: return…arrow_forwardPROBLEM STATEMENT: An anagram is a word that has been rearranged from another word, check tosee if the second word is a rearrangement of the first word. public class AnagramComputation{public static boolean solution(String word1, String word2){// ↓↓↓↓ your code goes here ↓↓↓↓return true;}} Can you help me with this java question the language is java please use the code I gavearrow_forwardprivate static String extractVowelHelper(String str, int start, String vowels) { // base case // recursive step }arrow_forward
- What is output? public class RecursiveFactorial { public static int factorial(int factValue) { if (factValue <= 0) { return 1; } else { return factValue*factorial(factValue-1); } public static void main (String args) { int userVal = -5; %3D System.out.println("Factorial = " + factorial (userVa %3D 1)); }arrow_forward8.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_forwardpublic static int countIt (int n) { int count = 0; for (int i = n; i > 0; i /= 2) for (int j = 0; j < 10; j++) count += 1; return count; } Big-O notation: Explanation:arrow_forward
- int n = 1; int k - 2; int r = n; if (k < n) { r - karrow_forwardPLZ help with the following: In Java Write a recursive method named contains with the following header:public static boolean contains(String haystack, String needle) The method should return true if needle is contained within haystack and false if needle is not in haystack. For example, contains("Java programming", "ogr") should return true contains("Java programming", "grammy") should return false You are not allowed to use the substring method to find a match.arrow_forwardinhinclude Rsing namespace std; int maxResult( ) int maxVal for (int i { for (int j = 0; j <= n 0; i <= n; i += a) i; j += b) %3D %3D float z = (float)(n (i + j)) / (float)(c); if (floor(z) { int x = int y ceil(z)) i / a; j/ b; maxVal = max(maxVal, x + y (int)z); return maxVal; } int main() { max cout << maxResult( ); return 0; 1 } Input Compilation failed due to fellowing ons main.cpp:7:23: error: 'n' was not declared in this scope 7| for (int i = 0; i <= n; i a) main.cpp:7:31: error: 'a was not declared in this scope for (int i = 0; i <= n; i t= a) %3D main.cpp:9:36: error: b' was not declared in this scope | 6 for (int j = 0; j <= n - i; j - b) %3D main.cpp:11:45: error: 'c was not declared in this scopearrow_forward
- a) FindMinIterative public int FindMin(int[] arr) { int x = arr[0]; for(int i = 1; i < arr.Length; i++) { if(arr[i]< x) x = arr[i]; } return x; } b) FindMinRecursive public int FindMin(int[] arr, int length) { if(length == 1) return arr[0]; return Math.Min(arr[length - 1], Find(arr, length - 1)); } What is the Big-O for this functions. Could you explain the recurisive more in details ?arrow_forward// The language is java Part I: Use of recursive method (4 points for students who need recursion/extracredit) Description: Code a class called RecursiveMultiply in which it will accept two integer arguments entered from user; pass these integers to a recursive method that returns the multiplication result of these two integers. The multiplication can be performed as repeated addition, for example, if two integers are 7 and 4: 8 * 4 = 4 + 4 + 4 + 4 + 4 + 4 + 4 + 4 You will write the application with GUI and recursion to display the result in a noneditable text field, based on the user’s entry from two text fields, while an OK button is pressed. You will utilize the exception handling code as you did in your Lab 4 and Lab 5 to verify if data entered in the text fields are valid (numerical and positive data only). You will make your own decision if there is any explanation that is not described in this specification. Finally, code a driver program that will test your class…arrow_forwardFind error and correct it : public class ProgramLayout { public static void main (String[] args) { final double PI = 3.14159; double x; double sum = x+PI ; System.out.println (sum); } // end classarrow_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