EBK BUILDING JAVA PROGRAMS
4th Edition
ISBN: 9780134323718
Author: Stepp
Publisher: PEARSON CUSTOM PUB.(CONSIGNMENT)
expand_more
expand_more
format_list_bulleted
Expert Solution & Answer
Chapter 12, Problem 15E
Explanation of Solution
Method definition:
//method definition
public static int permut(int n, int r)
{
//condition to validate the value
if (r == 0)
{
//return the value
return 1;
} else
{
>&#x...
Expert Solution & Answer
Want to see the full answer?
Check out a sample textbook solutionStudents have asked these similar questions
Write a RECURSIVE method called “sequence” that takes a single int parameter (n) and returns the int value of the nth element of the sequence S = 2, 4, 6, 12, 22, 40, 74, 136, 250, 460, …
Where S is defined by the recursive formula:
For n >= 0S(0) = 2; // Base case 1S(1) = 4; // Base case 2S(2) = 6; // Base case 3S(N) = 2 * ( S(N-1)/2 + S(N-2)/2 + S(N-3)/2)
The nth harmonic number is defined non-recursively as H(n) = 1+1/2+1/3+1/4+⋯+1/n Come up with a recursive definition and use it to guide you to write a method definition for a double-valued method named “harmonic” that accepts an int parameter n and recursively calculates and returns the nth harmonic number. Write a test program that displays the harmonic numbers, H(n), for n = 1,2,3,⋯,10.
java
Write a recursive method largestDigitthat accepts an integer parameter and returns the largest digit value that appears in that integer. Your method should work for both positive and negative numbers. If a number contains only a single digit, that digit's value is by definition the largest. The following table shows several example calls:
Call
Value Returned
largestDigit(14263203)
6
largestDigit(845)
8
largestDigit(52649)
9
largestDigit(3)
3
largestDigit(0)
0
largestDigit(-573026)
7
largestDigit(-2)
2
Obey the following restrictions in your solution:
You may not use a String, Scanner, array, or any data structure (list, stack, map, etc.).
Your method must be recursive and not use any loops (for, while, etc.).
Your solution should run in no worse than O(N) time, where N is the number of digits in the number.
Chapter 12 Solutions
EBK BUILDING JAVA PROGRAMS
Ch. 12.1 -
What is recursion? How does a recursive method...Ch. 12.1 - Prob. 2SCPCh. 12.1 - Prob. 3SCPCh. 12.1 - Prob. 4SCPCh. 12.1 - Prob. 5SCPCh. 12.1 - Prob. 6SCPCh. 12.1 - Prob. 7SCPCh. 12.2 - Prob. 8SCPCh. 12.2 -
What would be the effect if the code for the...Ch. 12.2 -
What would be the effect if the code for the...
Ch. 12.3 - Prob. 11SCPCh. 12.3 - Prob. 12SCPCh. 12.3 - Prob. 13SCPCh. 12.3 - Prob. 14SCPCh. 12.3 - Prob. 15SCPCh. 12.3 - Prob. 16SCPCh. 12.3 - Prob. 17SCPCh. 12.3 - Prob. 18SCPCh. 12.3 - Prob. 19SCPCh. 12.4 - Prob. 20SCPCh. 12.4 - Prob. 21SCPCh. 12.5 - Why is recursion an effective way to implement a...Ch. 12.5 - Prob. 23SCPCh. 12.5 - Prob. 24SCPCh. 12.5 - Prob. 25SCPCh. 12.5 - Prob. 26SCPCh. 12.5 - Prob. 27SCPCh. 12.5 - Prob. 28SCPCh. 12 - Prob. 1ECh. 12 - Write a method called writeNums that takes an...Ch. 12 - Write a method called writeSequence that accepts...Ch. 12 - Write a recursive method called doubleDigits that...Ch. 12 - Prob. 5ECh. 12 - Prob. 6ECh. 12 - Prob. 7ECh. 12 - Write a recursive method called multiplyEvens that...Ch. 12 - Prob. 9ECh. 12 - Prob. 10ECh. 12 - Prob. 11ECh. 12 - Write a recursive method called isReverse that...Ch. 12 - Prob. 13ECh. 12 - Prob. 14ECh. 12 - Prob. 15ECh. 12 - Prob. 16ECh. 12 - Prob. 17ECh. 12 - Prob. 18ECh. 12 - Prob. 19ECh. 12 - Prob. 20ECh. 12 - Prob. 21ECh. 12 - Prob. 22E
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 that gets two parameters as input: an array of integers called nums and an integer called index. The purpose of this method is to return the following: nums[index] * nums[index+ 1] * nums[index+ 2] * .. nums[nums.length-1]. Do not use loops. (java code)arrow_forwardThe nth harmonic number is defined non-recursively as: H(n)=1+1/2+1/3+1/4+...+1/n Come up with a recursive definition and use it to guide you to write a method definition for a double-valued method named “harmonic” that accepts an int parameter n and recursively calculates and returns the nth harmonic number. Write a test program that displays the harmonic numbers, H(n)=1,2,3,4...10arrow_forwardhow can i solve this using java?arrow_forward
- The following recursive method get Number Equal searches the array x of 'n integers for occurrences of the integer val. It returns the number of integers in x that are equal to val. For example, if x contains the 9 integers 1, 2, 4, 4, 5, 6, 7, 8, and 9, then getNumberEqual(x, 9, 4) returns the value 2 because 4 occurs twice in x. public static int getNumberEqual(int x[], int n, int val) { if (n< 0) ( return 0; } else { if (x[n-1) == val) { return getNumberEqual(x, n-1, val) +1; } else { return getNumber Equal(x, n-1, val); } // end if ) // end if } // end get Number Equal Demonstrate that this method is recursive by listing the criteria of a recursive solution and stating how the method meets each criterion.arrow_forwardWrite and test a Java/Python recursive method for finding the minimum element in an array, A, of n elements. What the running time? Hint: an array of size 1 would be the stop condition. The implementation is similar to linearSum method in lecture 5 examples. You can use the Math.min method for finding the minimum of two numbers.arrow_forwardWrite a recursive method that displays all permutations of a given array ofintegers. Here is a sample run: Enter the array size : 3[1, 2, 3][1, 3, 2][2, 1, 3][2, 3, 1][3, 1, 2][3, 2, 1]arrow_forward
- Write a recursive method that gets three parameters as input: an array of integers called nums, an integer called index, and an integer called value. The purpose of this method is to count how many times value appears in the array starting at nums[index]. Do not use loops. (Java code)arrow_forwardGiven a list of integers, you want to know whether it is possible to divide the integers into two sets, so that the sum of one set is odd, and the sum of the other set is a multiple of 10. Every integer must be in one set or the other. You can write a recursive helper method that takes any number of arguments and then call it inside the method, but you cannot use any loops. Test cases: oddAndTen([5, 5, 3]) true oddAndTen ([5, 5, 4]) oddAndTen ([5, 5, 4, 1]) false truearrow_forwardGiven the sequence, S2 = 1, 2, 4, 5, 7, 8, 10, 11, 13, 14, … Write a RECURSIVE method called “sequence2” that takes a single int parameter (n) and returns the int value of the nth element of the sequence S2. You will need to determine any base cases and a recursive case that describes the listed sequence. Use the following code to test your answers to questions 3 and 4the output should print the two sequences given (S & S2): public class TestSequences { public static void main(String[] args) { for(int i = 0; i < 10; i++) { System.out.print(sequence(i) + " "); // 2, 4, 6, 12, 22, 40, 74, 136, 250, 460 } System.out.println(); for(int i = 0; i < 10; i++) { System.out.print(sequence2(i) + " "); // 1, 2, 4, 5, 7, 8, 10, 11, 13, 14 } } // *** Your method for sequence here *** // *** Your method for sequences2 here *** } // end of TestSequences classarrow_forward
- Write a recursive method that returns the value of N! (N factorial) using the definition given in this chapter. Explain why you would not normally use recursion to solve this problem.arrow_forwardWrite a recursive function that finds the minimum value in an ArrayList. Your function signature should be public static int findMinimum(ArrayList<Integer>) One way to think of finding a minimum recursively is to think “the minimum number is either the last element in the ArrayList, or the minimum value in the rest of the ArrayList”. For example, if you have the ArrayList [1, 3, 2, 567, 23, 45, 9], the minimum value in this ArrayList is either 9 or the minimum value in [1, 3, 2, 567, 23, 45] ================================================ import java.util.*; public class RecursiveMin{public static void main(String[] args){Scanner input = new Scanner(System.in);ArrayList<Integer> numbers = new ArrayList<Integer>();while (true){System.out.println("Please enter numbers. Enter -1 to quit: ");int number = input.nextInt();if (number == -1){break;}else {numbers.add(number);}} int minimum = findMinimum(numbers);System.out.println("Minimum: " + minimum);}public static int…arrow_forwardWrite a recursive function that finds the minimum value in an ArrayList. Your function signature should be public static int findMinimum(ArrayList<Integer>) One way to think of finding a minimum recursively is to think “the minimum number is either the last element in the ArrayList, or the minimum value in the rest of the ArrayList”. For example, if you have the ArrayList [1, 3, 2, 567, 23, 45, 9], the minimum value in this ArrayList is either 9 or the minimum value in [1, 3, 2, 567, 23, 45] Hint:The trick is to remove the last element each time to make the ArrayList a little shorter. import java.util.*; public class RecursiveMin{public static void main(String[] args){Scanner input = new Scanner(System.in);ArrayList<Integer> numbers = new ArrayList<Integer>();while (true){System.out.println("Please enter numbers. Enter -1 to quit: ");int number = input.nextInt();if (number == -1){break;}else {numbers.add(number);}} int minimum =…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