In: Computer Science
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)
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).
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
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.
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");
}
}