Java program Do not use static variables to implement recurs
Java program.
Do not use static variables to implement recursive methods.
Problem 4
Implement a recursive method min that accepts an array and returns the minimum element in the array. The recursive step should divide the array into two halves and find the minimum in each half.
Demonstrate the output of min on the array
int [] a = { 2, 3, 5, 7, 11, 13, 17, 19, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 23, 29, 31, 37, 41, 43 }
Problem 6
Write a recursive method countSubstrings that counts the number of non-overlapping occurrences of a string s2 in a string s1. Use the method indexOf() from String class.
As an example, with s1=”abab” and s2=”ab”, countSubstrings returns 2. With s1=”aaabbaa” and s2=”aa”, countSubstrings returns 2. With s1=”aabbaa” and s2=”AA”, countSubStrings returns 0.
Show the output on the following strings:
s1 = “Java is a programming language originally developed by James Gosling at Sun Microsystems and released in 1995 as a core component of Sun Microsystems Java platform. The language derives much of its syntax from C and C++ but has a simpler object model and fewer low-level facilities. Java applications are typically compiled to bytecode (class file) that can run on any Java Virtual Machine (JVM) regardless of computer architecture. Java is a general-purpose, concurrent, class-based, object-oriented language that is specifically designed to have as few implementation dependencies as possible. Java is currently one of the most popular programming languages in use, particularly for client-server web applications, with a reported 10 million users.”
s2= “Java”.
Solution
Problem 4:-
CalcMin.java:
public class CalcMin {
public static void main(String[] args) {
int a[] = { 2, 3, 5, 7, 11, 13, 17, 19, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 23, 29, 31, 37, 41, 43 };
System.out.println(\"The min value is \"+ min(a));
}
public static int min(int a[]){
if(a.length==1) return a[0];
else if(a.length==2) return Math.min(a[0], a[1]);
else{
int a1[],a2[];
int n=a.length/2;
a1=new int[n];
a2=new int[a.length -n];
for(int i=0;i<n;i++){
a1[i]=a[i];
}
for(int i=n;i<a.length;i++){
a2[i-n]=a[i];
}
int min1=min(a1), min2=min(a2);
return Math.min(min1,min2);
}
}
}
Output:
The min value is 2
Problem 6:-
SubstringCount.java:
public class SubstringCount {
public static void main(String[] args) {
String s1=\"Java is a programming language originally developed by James Gosling at Sun Microsystems and released in 1995 as a core component of Sun Microsystems Java platform. The language derives much of its syntax from C and C++ but has a simpler object model and fewer low-level facilities. Java applications are typically compiled to bytecode (class file) that can run on any Java Virtual Machine (JVM) regardless of computer architecture. Java is a general-purpose, concurrent,class-based, object-oriented language that is specifically designed to have as few implementation dependencies as possible. Java is currently one of the most popular programming languages in use, particularly for client-server web applications, with a reported 10 million users.\";
String s2=\"Java\";
System.out.println(\"The number of times s2 appears in s1 is: \" + countSubstrings(s1,s2,0));
}
public static int countSubstrings(String s1, String s2, int index){
if(s1.indexOf(s2,index)==-1){
return 0;
}
else{
int n=s1.indexOf(s2, index);
return 1 + countSubstrings(s1,s2,n+1);
}
}
}
Output:
The number of times s2 appears in s1 is: 6

