Question

In: Computer Science

The Problem Below are a series of problems you need to solve using recursive methods BY...

The Problem

Below are a series of problems you need to solve using recursive methods BY using java . You will write a program that will read commands from an input file, with each command referring to one of the recursive problems to be executed. Each command will be followed (on the same line of input) by the respective parameters required for that problem. (15 points for main method)

  1. DescArrayCheck (15 points)

Write a recursive method that checks whether an array of integers - given as parameter - is sorted in descending order or not.

The result must be 0 if the array is not sorted or if it is sorted in ascending order. 1 if the array is sorted in descending order.

In the main program you need to transform a series of positive integers separated by ';' into an array of integers and pass it as a parameter of this method. This part does not use recursion to transform the string into an array.

Example1:

Command: DescArrayCheck 112;104;52;32;12;10

Result: 1 (Sorted array in descending order).

Example2:

Command: DescArrayCheck 12;14;52;132;212

Result: 0 (Sorted array in ascending order).

Example3:

Command: DescArrayCheck 52;13;21;3

Result: 0 (Unsorted array).

Example4:

Command: DescArrayCheck 14

Result: 1 (Sorted array in descending order).

  1. DecToHex    (15 points)

Write a recursive method that receives a decimal number and converts it to a hexadecimal number. The decimal to hexadecimal conversion can be performed by applying the repeated division and remainder algorithm.

Example1:

Command: DecToHex 4253

Result: 109D

Example2:

Command: DecToHex 314

Result: 13A

  1. Noccurrences    (15 points)

Given a string, a substring, and a number as parameters, write a recursive method that calculates recursively if at least n occurrences of a sub-string exist in string.

Example1:

Command: Noccurrences ababababb bab 2

Result: true (number of occurrence is 3)

Example2:

Command: Noccurrences 2121221222   212   5

Result: false (number of occurrence is 3)

Example3:

Command: Noccurrences yyyyyy yyy   3

Result: true (number of occurrence is 4)

Input File Specifications

You will read in input from a file, "input.in.txt". Have this AUTOMATED. Do not ask the user to the name of the input file. You should read in this automatically. The first line of the input file will have one positive integer, representing the number of commands (lines) inside the input file.

Each of the following n lines will have a command, and each command will be followed by appropriate data as described below (and this data will be on the same line as the command).

The commands (for the 3 recursive methods), and their relevant data, are described below:

DescArrayCheck: This command will be followed by a series of integers separated by ';'.

DecToHex This command will be followed by a single decimal number.

Noccurrences This command will be followed by a string str, a substring subStr and a positive number N representing the number of occurrences of Substr in Str.

Output Format

Your program must output to a file, called "output.out.txt". You must follow the program specifications exactly. Refer to sample output file for exact formatting specifications.

Solutions

Expert Solution

class CheckSorted {
        public static int arraySortedOrNot(int arr[], int n)
        {
                if (n == 1 || n == 0)
                        return 1;
                if (arr[n - 1] > arr[n - 2])
                        return 0;
                return arraySortedOrNot(arr, n - 1);
        }

        public static void main(String[] args)
        {
                int arr[] = { 88, 57, 45, 23, 13, 20 };
                int n = arr.length;
                if (arraySortedOrNot(arr, n) != 0)
                        System.out.println("Yes");
                else
                        System.out.println("No");
        }
}
import java.io.*;
class Convert{
    public static void main(String args[])throws IOException{
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        System.out.print("Enter the number in decimal: ");
        int n = Math.abs(Integer.parseInt(br.readLine()));
        String hex = hexadecimal(n);
        
        System.out.println("Hexadecimal equivalent: " + hex);
    }
    public static String hexadecimal(int num){
        if(num < 10)
            return Integer.toString(num);
        else if(num < 16){
            switch(num){
                case 10:
                return "A";
                case 11:
                return "B";
                case 12:
                return "C";
                case 13:
                return "D";
                case 14:
                return "E";
                case 15:
                return "F";
                default:
                return "";
            }
        }
        else
            return hexadecimal(num / 16) + "" + hexadecimal(num % 16);
    }
}

class three 
{ 

public static int countSubstring(String str1,String str2) 
{ 
        int n1 = str1.length(); 
        int n2 = str2.length(); 

        if (n1 == 0 || n1 < n2) 
                return 0; 
 
        if (str1.substring(0, n2).equals(str2)) 
                return countSubstring(str1.substring(n2 - 1), 
                                                                                        str2) + 1; 

        return countSubstring(str1.substring(n2 - 1), 
                                                                                str2); 
} 

// Driver Code 
public static void main(String args[]) 
{ 
        String str1 = "yyyyyy", 
                str2 = "yyy"; 
        int min_count = 2;
        System.out.println(countSubstring(str1,str2));
        int count= countSubstring(str1,str2);
        if(count>=min_count)
          System.out.println("True");
        else
          System.out.println("False");
          

} 
} 

Related Solutions

The Problem Below are a series of problems you need to solve using recursive functions. You...
The Problem Below are a series of problems you need to solve using recursive functions. You will write a program that will read commands from an input file, with each command referring to one of the recursive problems to be executed. Each command will be followed (on the same line of input) by the respective parameters required for that problem. Implementation Each recursive function MUST have a wrapper function enclosing it where you will do input/output file processing as well...
Solving Problems Using Recursion (Python): To solve the problem, you have to use recursion and cannot...
Solving Problems Using Recursion (Python): To solve the problem, you have to use recursion and cannot use for or while loops to solve the problems as well as not using global variables. 1. Create a function that takes a positive integer and returns it with neighboring digits removed. Do not convert the integer to a list. Ex. Input = [5555537777721] Output = [53721]
For this problem, solve the problems by answering a) state the claim using a sentence, b)...
For this problem, solve the problems by answering a) state the claim using a sentence, b) Ho H1 and place the claim with either one of the two, c) Test statistic, show and label the formula you use, d) find critical value(s), and reject Ho or fail to reject Ho. You may use p-value. e) Write a formal conclusion and final statement (Please show all work and label answer a,b,c,d,e) The Pew Research Center claims that at least 54% of...
For this problem, solve the problems by answering a) state the claim using a sentence, b)...
For this problem, solve the problems by answering a) state the claim using a sentence, b) Ho H1 and place the claim with either one of the two, c) Test statistic, show and label the formula you use, d) find critical value(s), and reject Ho or fail to reject Ho. You may use p-value. e) Write a formal conclusion and final statement (Please show all work and label answer a,b,c,d,e) A sample of 106 body temperatures with a mean of...
For this problem, solve the problems by answering a) state the claim using a sentence, b)...
For this problem, solve the problems by answering a) state the claim using a sentence, b) Ho H1 and place the claim with either one of the two, c) Test statistic, show and label the formula you use, d) find critical value(s), and reject Ho or fail to reject Ho. You may use p-value. e) Write a formal conclusion and final statement (Please show all work and label answer a,b,c,d,e) A golf balls manufacturer requires that the weights of its...
Description: For this assignment, you are required to write 3 recursive methods described below. Create a...
Description: For this assignment, you are required to write 3 recursive methods described below. Create a class named HW06 and implement the following 3 methods . The implementation MUST use recursive calls. - static int multiplyPositiveIntegers(int a, int b) //multiplies two positive integers using recursion (no use of * allowed). Returns the result of multiplication. (30 points) - static void recursiveBubbleSort(int[] arr) //Sorts an array using bubble sort in a recursive manner. Hint: do one pass(which puts the biggest element...
Solve the initial value problem once using power series method and once using the characteristic method....
Solve the initial value problem once using power series method and once using the characteristic method. Please show step for both 3) 3y”−y=0, y(0)=0,y’(0)=1 Note that 3y” refers to it being second order differential and y’ first
Using the method of separation of variables and Fourier series, solve the following heat conduction problem...
Using the method of separation of variables and Fourier series, solve the following heat conduction problem in a rod. ∂u/∂t =∂2u/∂x2 , u(0, t) = 0, u(π, t) = 3π, u(x, 0) = 0
I have the answer for this problem but I need to solve it using Excel's PV...
I have the answer for this problem but I need to solve it using Excel's PV function for both products and I cannot seem to figure it out. What would I insert for each portion of the equation into excel? It has been answered on another question as number 2. http://www.chegg.com/homework-help/questions-and-answers/lou-barlow-divisional-manager-sage-company-opportunity-manufacture-sell-one-two-new-produc-q12342389?trackid=297667da&strackid=464d9914&ii=8
Solve the following problems: Using the balance sheet below: create a balance sheet for 2016 and...
Solve the following problems: Using the balance sheet below: create a balance sheet for 2016 and 2017. make a list of working capital accounts. what is the net working capital for 2016 and 2017? What is the change in net working capital for 2017? Balance Sheet Accounts of SimpleTec Corporation Account Balance 12/31/2013 Balance 12/31/2014 Accumulated Depreciation $2,030 $2,680 Accounts Payable $1,810 $2,070 Accounts Receivable $2,490 $2,700 Cash $1,310 $1,100 Common Stock $5,000 $5,000 Inventory $5,810 $6,040 Long-Term Debt $7,810...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT