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

For this problem set, you need to create two classes to simulate a file system,

ID: 3568776 • Letter: F

Question

For this problem set, you need to create two classes to simulate a file system, and then write a method that will recursively search through the file system for a given file.

The first class should be called File, and have one instance variable called filename, which is a String.

The second class should be called Directory, and have two instance variables. The first is an array of File objects, and the second is an array of Directory objects. Note that either of the arrays can be empty.

To simulate a file system with these classes, you will have a starting Directory. The Directory instances in the array represent sub-directories in the file system, and the File instances in the array represent files in that directory. Each of the sub-directories, might also contain files and sub-directories, etc.

Write a recursive method that takes in a String, representing a filename, and a Directory, representing the starting directory. This method should return true if a File with that filename is contained somewhere (possibly in a sub-directory, sub-sub-directory, etc.) in the starting Directory.

Explanation / Answer

Java Program:

import java.util.Scanner;

class Files
{
String name;
public void setName(String name)
{
  this.name = name;
}
public String getName()
{
  return name;
}
}

class Directory
{
int noF,noD;
Scanner sc = new Scanner(System.in);
Files fileArray[];
String name;
Directory direcArray[];
public Directory(int noF,int noD)
{
  this.noF = noF;
  this.noD = noD;
  fileArray = new Files[noF];
  direcArray = new Directory[noD];
}

public void addFiles()
{
  for(int i=0;i<this.noF;i++)
  {
   System.out.println("Enter the name of file "+(i+1)+" in the "+this.name+" directory");
   String name = sc.next();
   fileArray[i] = new Files();
   fileArray[i].setName(name);
  }
}

public void setName(String name)
{
  this.name = name;
}

public void addContent()
{  
  for(int i=0;i<this.noD;i++)
  {
   System.out.println(" Enter the details of Sub-directories");
   System.out.println(" Enter the name of sub-directory "+(i+1));
   String str = sc.next();
   System.out.println(" Enter the number of sub-directories in "+str+" directory");
   int tnoD= sc.nextInt();
   System.out.println(" Enter the number of files in "+str+" directory");
   int tnoF= sc.nextInt();
   direcArray[i] = new Directory(tnoF,tnoD);   
   direcArray[i].setName(str);
   direcArray[i].addFiles();
   direcArray[i].addContent();   
  }
  
}

public void print()
{
  System.out.println("The number of files are "+fileArray.length);  
  for(int i=0;i<this.noF;i++)
  {   
   System.out.println(this.fileArray[i].getName());
  }
  
  System.out.println("The number of sub-directories in "+this.name+" "+direcArray.length);
  
  for(int i=0;i<this.noD;i++)
  {
   System.out.println("Directory "+(i+1));
   direcArray[i].print();
  }
}

public void search(String nam)
{
  
  for(int i=0;i<this.noF;i++)
  {   
   if(fileArray[i].getName().equalsIgnoreCase(nam))
    System.out.println("File is found at "+this.name+" directory");
  }
    
  for(int i=0;i<this.noD;i++)
  {
   if(direcArray[i].name.equalsIgnoreCase(nam))
    System.out.println("Directory Found at "+this.name+" directory");
   direcArray[i].search(nam);
  }
}
}

public class ga{
public static void main(String arg[])
{
  Scanner sc = new Scanner(System.in);
  System.out.println(" Enter the number of directories in Root directory");
  int noD= sc.nextInt();
  System.out.println(" Enter the number of files in Root directory");
  int noF= sc.nextInt();
  Directory root = new Directory(noF,noD);
  root.setName("Root");
  root.addFiles();
  root.addContent();
  System.out.println("The Total files and folders in Root Directory are:");
  root.print();
  
  System.out.println(" Enter the file/directory name to search");
  String str= sc.next();
  root.search(str);
}
}

------------------------------------------------------------------------------------------------------------------------------------

Enter the number of directories in Root directory
2
Enter the number of files in Root directory
2
Enter the name of file 1 in the Root directory
myNotes.txt
Enter the name of file 2 in the Root directory
myProgram.txt
Enter the details of Sub-directories
Enter the name of sub-directory 1
myMovies
Enter the number of sub-directories in myMovies directory
0
Enter the number of files in myMovies directory
4
Enter the name of file 1 in the myMovies directory
spiderMan
Enter the name of file 2 in the myMovies directory
superMan
Enter the name of file 3 in the myMovies directory
batMan
Enter the name of file 4 in the myMovies directory
titanic
Enter the details of Sub-directories
Enter the name of sub-directory 2
mySoftwares
Enter the number of sub-directories in mySoftwares directory
1
Enter the number of files in mySoftwares directory
3
Enter the name of file 1 in the mySoftwares directory
abodeReader
Enter the name of file 2 in the mySoftwares directory
picasa
Enter the name of file 3 in the mySoftwares directory
msOffice
Enter the details of Sub-directories
Enter the name of sub-directory 1
Drivers
Enter the number of sub-directories in Drivers directory
0
Enter the number of files in Drivers directory
2
Enter the name of file 1 in the Drivers directory
audioDriver
Enter the name of file 2 in the Drivers directory
vedioDriver
The Total files and folders in Root Directory are:
The number of files are 2
myNotes.txt
myProgram.txt
The number of sub-directories in Root 2
Directory 1
The number of files are 4
spiderMan
superMan
batMan
titanic
The number of sub-directories in myMovies 0
Directory 2
The number of files are 3
abodeReader
picasa
msOffice
The number of sub-directories in mySoftwares 1
Directory 1
The number of files are 2
audioDriver
vedioDriver
The number of sub-directories in Drivers 0

Enter the file/directory name to search
audioDriver

File is found at Drivers directory

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote