Question

In: Computer Science

Please submit a Netbeans project with a class called RecursionExercises.java implementing the following methods: public static...

Please submit a Netbeans project with a class called RecursionExercises.java implementing the following methods:

public static int reverse(int number)

This method returns the positive integer obtained when the digits of the parameter is reversed. For example, when called with the parameter 12345, this method would return 54321. (Do this with proper integer arithmetic instead of just simply converting to String, reversing that and then using parseInt to extract the result).

Hint: The number of digits of a number can be obtained by taking the integer part of the logarithm in base 10 of the number + 1 (i.e, num digits = log10(n) + 1).

public static int countPaths(int n, int m)

You are standing at the point (n,m) of the grid of positive integers and want to go to origin (0,0) by taking steps either to left or down: that is, from each point (n,m) you are allowed to move either to the point (n-1,m) or the point (n,m-1). Implement the method that based on recursion counts the number of different paths from the point (n,m) to the origin.

public static String ordString(int number)

Write a recursive method that given an integer number n that returns a String based on the following pattern:

number = 0, method returns {}

number = 1, method returns {{}}

number = 2, method returns {{}, {{}}}

number = 3, method returns {{}, {{}}, {{}, {{}}}}

In other words, given number > 0, the ordString method returns all ordStrings from 1 to number. Write a recursive method that accomplish this.

Solutions

Expert Solution

Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks

// RecursionExercises.java

public class RecursionExercises {

      // recursive method to reverse a number, assuming number is positive

      public static int reverse(int number) {

            // if number is 0, returning 0 (base case 1)

            if (number == 0) {

                  return 0;

            }

            // finding number of digits

            int numDigits = (int) (Math.log10(number) + 1);

            // if number of digits is 1, returning number (base case 2)

            if (numDigits == 1) {

                  return number;

            }

            // extracting last digit

            int digit = number % 10;

            // removing last digit from number

            number = number / 10;

            // multiplying digit with power of 10^(digits-1) and adding the reverse

            // of number to it, returning this value

            return (int) ((digit * Math.pow(10, numDigits - 1)) + reverse(number));

      }

      // method to count all different paths from (n,m) to (0,0)

      public static int countPaths(int n, int m) {

            // if n or m is outside the bounds, returning 0 (base case 1)

            if (n < 0 || m < 0) {

                  return 0;

            }

            // if n and m are 0, we reached the destination, returning 1 (base case

            // 2)

            if (n == 0 && m == 0) {

                  return 1;

            }

            // counting number of paths going left and number of paths going down

            // from this point, summing and returning the result

            return countPaths(n - 1, m) + countPaths(n, m - 1);

      }

      // returns the pattern based on number, assuming number is always positive

      public static String ordString(int number) {

            // if number is 0, returning "{}"

            if (number == 0) {

                  return "{}";

            }

            String temp = "";

            // looping from i=0 to i=number-1

            for (int i = 0; i < number; i++) {

                  // if this is not the first value, appending a comma and space to

                  // separate the ordStrings

                  if (i > 0) {

                        temp += ", ";

                  }

                  // appending ordString of i to temp

                  temp += ordString(i);

            }

            // wrapping temp inside a pair of parenthesis and returning the result

            return "{" + temp + "}";

      }

      public static void main(String[] args) {

            // testing

            System.out.println("Reverse of 12304 is " + reverse(12304));

            System.out.println("Count paths from (3, 2): "+countPaths(3,2));

            System.out.println("ordString (3): "+ordString(3));

      }

}

/*OUTPUT*/

Reverse of 12304 is 40321

Count paths from (3, 2): 10

ordString (3): {{}, {{}}, {{}, {{}}}}


Related Solutions

Write a class called VLPUtility with the following static methods: Java Language 1. concatStrings that will...
Write a class called VLPUtility with the following static methods: Java Language 1. concatStrings that will accept a variable length parameter list of Strings and concatenate them into one string with a space in between and return it. 2. Overload this method with two parameters, one is a boolean named upper and one is a variable length parameter list of Strings. If upper is true, return a combined string with spaces in upper case; otherwise, return the combined string as...
Using maps in Java. Make a public class called ExampleOne that provides a single class (static)...
Using maps in Java. Make a public class called ExampleOne that provides a single class (static) method named firstOne. firstOne accepts a String array and returns a map from Strings to Integer. The map must count the number of passed Strings based on the FIRST letter. For example, with the String array {“banana”, “apples”, “blueberry”, “orange”}, the map should return {“b”:2, “a”:1, “o”:1}. Disregard empty Strings and zero counts. Retrieve first character of String as a char using charAt, or,...
In java. Please explain. Consider the following program: } import java.util.*; public class Chapter7Ex12 { static...
In java. Please explain. Consider the following program: } import java.util.*; public class Chapter7Ex12 { static Scanner console = new Scanner(System.in); public static void main(String[] args) { double num1; double num2; System.out.print("Enter two integers: "); num1 = console.nextInt(); num2 = console.nextInt(); System.out.println(); if (num1 != 0 && num2 != 0) System.out.printf("%.2f\n", Math.sqrt(Math.abs(num1 + num2 + 0.0))); else if (num1 != 0) System.out.printf("%.2f\n", Math.floor(num1 + 0.0)); else if (num2 != 0) System.out.printf("%.2f\n",Math.ceil(num2 + 0.0)); else System.out.println(0); }} a. What is the...
In Lab 4, you made a class with static methods. The static methods converted an integer...
In Lab 4, you made a class with static methods. The static methods converted an integer in binary, Hex, and Octal. To do this, you made a class of static methods. The class did not have any fields. The static methods received the integer value as a parameter. For this lab, make all of the static methods, non-static methods. Lab 5 Requirements Create a class named “Lab5_IntConv_Class Have one private field name intValue. Add two constructors: One is a default...
The following Java program is NOT designed using class/object concept. public class demo_Program4_non_OOP_design { public static...
The following Java program is NOT designed using class/object concept. public class demo_Program4_non_OOP_design { public static void main(String[] args) { String bottle1_label="Milk"; float bottle1_volume=250; float bottle1_capacity=500; bottle1_volume=addVolume(bottle1_label, bottle1_volume,bottle1_capacity,200); System.out.println("bottle label: " + bottle1_label + ", volume: " + bottle1_volume + ", capacity: " +bottle1_capacity); String bottle2_label="Water"; float bottle2_volume=100; float bottle2_capacity=250; bottle2_volume=addVolume(bottle2_label, bottle2_volume,bottle2_capacity,500); System.out.println("bottle label: " + bottle2_label + ", volume: " + bottle2_volume + ", capacity: " +bottle2_capacity); } public static float addVolume(String label, float bottleVolume, float capacity, float addVolume)...
Create a Netbeans project called LineNumbers The program should do the following: –Ask the user for...
Create a Netbeans project called LineNumbers The program should do the following: –Ask the user for how many lines of text they wish to enter –Declare and initialize an array of Strings to hold the user’s input –Use a while loop to prompt for and read the Strings (lines of text) from the user at the command line. Typically, this would be a 'for' loop since we know the number of times to execute the loop based upon the number...
Could you do it with python please? public class BSTTraversal {         public static void main(String[]...
Could you do it with python please? public class BSTTraversal {         public static void main(String[] args) {                 /**                  * Creating a BST and adding nodes as its children                  */                 BSTTraversal binarySearchTree = new BSTTraversal();                 Node node = new Node(53);                 binarySearchTree.addChild(node, node, 65);                 binarySearchTree.addChild(node, node, 30);                 binarySearchTree.addChild(node, node, 82);                 binarySearchTree.addChild(node, node, 70);                 binarySearchTree.addChild(node, node, 21);                 binarySearchTree.addChild(node, node, 25);                 binarySearchTree.addChild(node, node, 15);                 binarySearchTree.addChild(node, node, 94);                 /**         ...
in Java using netbeans create a project and in it a class with a main. We...
in Java using netbeans create a project and in it a class with a main. We will be using the Scanner class to read from the user. At the top of your main class, after the package statement, paste import java.util.Scanner; Part A ☑ In your main method, paste this code. Scanner scan = new Scanner(System.in); System.out.println("What is x?"); int x = scan.nextInt(); System.out.println("What is y?"); int y = scan.nextInt(); System.out.println("What is z?"); int z = scan.nextInt(); System.out.println("What is w?");...
Part A Java netbeans ☑ Create a project and in it a class with a main....
Part A Java netbeans ☑ Create a project and in it a class with a main. We will be using the Scanner class to read from the user. At the top of your main class , after the package statement, paste import java.util.Scanner; As the first line inside your main method, paste Scanner scan = new Scanner(System.in); Remember when you are getting a value from the user, first print a request, then use the Scanner to read the right type...
in Java using netbeans create a project and in it a class with a main. We...
in Java using netbeans create a project and in it a class with a main. We will be using the Scanner class to read from the user. At the top of your main class, after the package statement, paste import java.util.Scanner; Part A ☑ In your main method, paste this code. Scanner scan = new Scanner(System.in); System.out.println("What is x?"); int x = scan.nextInt(); System.out.println("What is y?"); int y = scan.nextInt(); System.out.println("What is z?"); int z = scan.nextInt(); System.out.println("What is w?");...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT