Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

/** * Write a description of class Project6 here. * * @author () * @version (a v

ID: 3735743 • Letter: #

Question

/**
* Write a description of class Project6 here.
*
* @author ()
* @version (a version number or a date)
*/
import java.io.File;
import java.io.PrintWriter;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class CountDigits {
public static int countDigits(int n, int d) {
// Base case
if(n <= 0)
return 0;
// Recursive case
else{
int r = n%10;

if(r == d) {
return 1+ countDigits(n/101, d);
}
else{
return countDigits(n/101, d);
}
}
}

public static void main(String[] args) throws FileNotFoundException {
//Opening file
Scanner sc = new Scanner(new File("countDigits.txt"));

// Reading an number
int n = sc.nextInt();

// Reading digit
int d = sc.nextInt();
sc.close();

// calling countDigit function
int count = countDigits(n, d);
System.out.println("Number of times "+d+" is in "+n+" : "+count);
  
}
}

class Pyramids {
public static int pyramid(int n) {
if(n == 0) {
return 0;
} else {
return n + pyramid(n-1);
}
}

public static void main(String[] args) throws FileNotFoundException {
try {
Scanner fin = new Scanner(new File("pyramid.txt"));
if(fin.hasNextInt()) {
System.out.println(pyramid(fin.nextInt()));
}
fin.close();
} catch (FileNotFoundException e) {
System.out.println("pyramid.txt does not exists");
}
}
}

class FillArray {
private static Scanner sc = new Scanner(System.in);
public static void read(int[] arr, int n) {
if(n <= 0) {  
return;
}
arr[n-1] = sc.nextInt();
read(arr, n-1);
}

public static void main(String[] args) throws FileNotFoundException {
// Opening fillArray.txt
Scanner inFile = new Scanner(new File("fillArray.txt"));;

// Reading n values
int n = inFile.nextInt();

// Creating an array
int[] arr =new int[n];

// Taking user input
System.out.println("Enter "+n+" integers");
read(arr, n);

// Output on the terminal and output file "output.txt"
PrintWriter out = new PrintWriter("output.txt");
for(int i=0; i<n; i++){
System.out.print(arr[i]+" ");
out.write(Integer.toString(arr[i])+" ");
}
System.out.println();
sc.close();inFile.close();out.close();
}
}

I need to have one main method that sends the recursive method what it needs. The recursive method will return the results to the main. I also need a menu that lets the user choose the project to use. Menu should have all 3 programs to choose from. I attached the java file of the 3 seperate projects.

Explanation / Answer

Please find my code:

import java.util.Scanner;

public class CountDigits {

   public static int countDigits(int n, int d) {

       // Base case

       if(n <= 0)

           return 0;

       // Recursive case

       else{

           int r = n%10;

           if(r == d) {

               return 1+ countDigits(n/101, d);

           }

           else{

               return countDigits(n/101, d);

           }

       }

   }

}

class Pyramids {

   public static int pyramid(int n) {

       if(n == 0) {

           return 0;

       } else {

           return n + pyramid(n-1);

       }

   }

}

class FillArray {

   private static Scanner sc = new Scanner(System.in);

   public static void read(int[] arr, int n) {

       if(n <= 0) {

           return;

       }

       arr[n-1] = sc.nextInt();

       read(arr, n-1);

   }

}

##########

import java.io.File;

import java.io.FileNotFoundException;

import java.io.PrintWriter;

import java.util.Scanner;

public class MainDeriver {

   public static void main(String[] args) throws FileNotFoundException {

       Scanner keyword = new Scanner(System.in);

       System.out.println("1. Count Digits 2. Pyramids 3. FillArray");

       System.out.print("Enter you option: ");

       int op = keyword.nextInt();

      

       keyword.close();

       if(op == 1) {

           //Opening file

           Scanner sc = new Scanner(new File("countDigits.txt"));

           // Reading an number

           int n = sc.nextInt();

           // Reading digit

           int d = sc.nextInt();

           sc.close();

           // calling countDigit function

           int count = CountDigits.countDigits(n, d);

           System.out.println("Number of times "+d+" is in "+n+" : "+count);

       }else if(op == 2) {

           try {

               Scanner fin = new Scanner(new File("pyramid.txt"));

               if(fin.hasNextInt()) {

                   System.out.println(Pyramids.pyramid(fin.nextInt()));

               }

               fin.close();

           } catch (FileNotFoundException e) {

               System.out.println("pyramid.txt does not exists");

           }

       }else if(op == 3) {

           // Opening fillArray.txt

           Scanner inFile = new Scanner(new File("fillArray.txt"));;

           // Reading n values

           int n = inFile.nextInt();

           // Creating an array

           int[] arr =new int[n];

           // Taking user input

           System.out.println("Enter "+n+" integers");

           FillArray.read(arr, n);

           // Output on the terminal and output file "output.txt"

           PrintWriter out = new PrintWriter("output.txt");

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

               System.out.print(arr[i]+" ");

               out.write(Integer.toString(arr[i])+" ");

           }

           System.out.println();

           inFile.close();out.close();

       }else{

           System.out.println("Invalid option!!!");

       }

   }

}