All of these programs will use recursion. You will NOT use loops for these metho
ID: 3731354 • Letter: A
Question
All of these programs will use recursion. You will NOT use loops for these methods.
You'll have one main method that sends the recursive method what it needs. The recursive method will return the results to the main.
You will print the results to the terminal window and to an output file (except for Program 4).
All input for this program will be stored in input files. You will create an input file for testing each program. Be aware that unknown input files will be used to grade your programs.
Pick 3 of the following 4 programs to implement in Java. If you want extra credit, be sure to pick Program 4. Create a menu and let the user choose the Program to complete. Your menu should have at least 3 items to choose.
Program 1:
pyramid – use input file named pyramid.txt
Pyramids will be created from blocks. You'll take the n (number of rows) given to you, your program will then determine the total number of blocks based on how many rows the pyramid has.
Examples:
pyramid where n = 1; returns 1
pyramid where n = 2; returns 3
pyramid where n = 0; returns 0
pyramid where n = 5; returns 15
Program 2 – do EITHER option a) or option b):
2a) countSubString – use input file named countSubString.txt
Count the number of times the substring sub is included within the string str, no matter what the case (Case is not a factor here.) The string and substring will be stored in an input file on separate lines. Your program will create an output file. Send each pair to the recursive method.
As an example: your input file might contain:
There are Strings and there are strings. How many times is the word 'the' found in this string?
the
Sent to the method: "There are Strings and there are strings. How many times is the word 'the' found in this string?", "the"
Output for countTheSubstrings method
4
2b) stopToGo – use input file named stopToGo.txt
Whenever you see the word stop in a string, change it to go. Case is not a factor. For example:
String sent to the method: StoP, I Said Stop!
String returned from the method is: Go, I said Go!
Program 3:
3) countDigits– use input file named countDigits.txt
Read in a number and a digit.
Count the number of times you find the digit in the number.
Example:
Sent to the method: 10203040586970 0
Returned the number of times it finds a match: 5
Program 4:
4a) fillArray– use input file named fillArray.txt
Read in the number of elements that will be read in.
Create an array of integer values (the number read in will be the max for your array).
Read in that number of integers to fill it with numbers - recursively.
Sent to the method: the empty array
Print your array recursively to the terminal window and to the output file.
Extra Credit- Add to Program 4a above:
4b) (3 points maximum) Request a number from the user (that might be in the array)
Count the number of times you find a match.
Sent to the method: the array and the integer value to match.
Return the number of times it finds a match.
Explanation / Answer
//prgram 1 pyramid file
// Java Program reading from File pyramid
// BufferedReader
import java.io.*;
public class pyramid
{
public static void main(String[] args)throws Exception
{
// We need to provide file path as the parameter:
File file = new File("pyramid.txt");
BufferedReader br = new BufferedReader(new FileReader(file));
int count=0;
String st;
//count the number of lines
while ((st = br.readLine()) != null) count++;
//printing the total number of blocks
System.out.println(sumR(count));
}
//loop to count the total number of words in the pyramid file
public static int sumR(int num)
{
int sum;
if(num==1) return(1) ;
else sum=num + sumR(num-1); // function calls itself
return sum;
}
}// suppose file has 5 lines
OUTPUT OF THE CODE:
dps@machine:~/Documents/Chegg$ javac pyramid.java
dps@machine:~/Documents/Chegg$ java pyramid
15
//2b
// Java Program replacing stop to Go
import java.io.File;
import java.util.Scanner;
public class stopToGo
{
public static void main(String[] args) throws Exception
{
String read;
// pass the path to the file as a parameter
File file =
new File("stopToGo.txt");
Scanner sc = new Scanner(file);
System.out.print("Changed string to Go is: ");
//reading word by word
while (sc.hasNext()) {
//saving the value in a varible
read = sc.next();
read = read.replaceAll("(?i)stop", "Go ");
//printing Go if found the stop
System.out.print(read );
System.out.print(" ");
}
// for printng the newline for increasing readability in output
System.out.println();
}
}
OUTPUT OF THE CODE:
dps@machine:~/Documents/Chegg$ javac stopToGo.java
dps@machine:~/Documents/Chegg$ cat stopToGo.txt
StoP, I Said Stop!
dps@machine:~/Documents/Chegg$ java stopToGo
changed string to go is:
Go , I Said Go !
//3
// Java Program number
import java.io.File;
import java.util.Scanner;
public class countDigits
{
public static void main(String[] args) throws Exception
{
// String read, find;
int number, compare;
int count=0;
// pass the path to the file as a parameter
File file = new File("countDigits.txt");
Scanner inputFile = new Scanner(file);
//Read a number in the file
number = inputFile.nextInt();
compare = inputFile.nextInt();
int covert = number ;
// while (number > 0) {
// if ((number % 10) == compare)
// count++;
// number /= 10;
// }
System.out.println(" Total occurence of letter:"+ digits(number,compare));
}
//function that recursively abstract the each digit
public static int digits(final int number, final int digit){
if(number < 10) {
// if the number is less than 10, return if the digit is present, and don't recurse any deeper
return number == digit ? 1 : 0;
} else {
// if the right-most digit is correct
if(number%10 == digit) {
// return 1 + the value from the remaining digits (recursion)
return 1 + digits(number/10, digit);
} else {
// else just return the value from the remaining digits (recursion)
return digits(number/10, digit);
}
}
}
}
OUTPUT OF CODE:
dps@machine:~/Documents/Chegg$ javac countDigits.java
dps@machine:~/Documents/Chegg$ cat countDigits.txt
1215151534 1
dps@machine:~/Documents/Chegg$ java countDigits
Total occurence of letter:4