Write a class encapsulating the concept of a file assuming a

Write a class encapsulating the concept of a file, assuming a file has only a single attribute: the name of the file. Include a constructor, the accessors and mutators, and methods to String and equals. Also, code a method returning the extension of the file, that is, the letters after the last dot in the file (for instance, if the file name is Test. Java, then the method should return java); if there is no dot in the file name, then the method should return \"unknown extension.\" Write a client class to test all the methods in your class.

Solution

File.java

public class File {
   //Declaring instance variable
   private String nameOfFile;

   //Parameterized constructor
   public File(String nameOfFile) {
       super();
       this.nameOfFile = nameOfFile;
   }

   //Setters and getters
   public String getNameOfFile() {
       return nameOfFile;
   }

   public void setNameOfFile(String nameOfFile) {
       this.nameOfFile = nameOfFile;
   }

   //toString() method is used to display the contents of an object inside it
   @Override
   public String toString() {
       return \"\"+ nameOfFile;
   }

   //This method checks whether te two file names are equal or not
   @Override
   public boolean equals(Object obj) {
       if (this == obj)
           return true;
       if (obj == null)
           return false;
       if (getClass() != obj.getClass())
           return false;
       File other = (File) obj;
       if (nameOfFile == null) {
           if (other.nameOfFile != null)
               return false;
       } else if (!nameOfFile.equals(other.nameOfFile))
           return false;
       return true;
   }
  
  
   /* This method will return the extension if there is dot in the filename
   * if there is not dot then it return error message
   */
String findExtensionOfFile(String filename)
{
   int dotIndex=-1;
   dotIndex=filename.lastIndexOf(\".\");
   String ext=filename.substring(dotIndex+1);
   if(dotIndex!=-1)
       return ext;
   else
       return \"Unknown Extension\";
  
}

}

__________________________

Test.java

import java.util.Scanner;

public class Test {

   public static void main(String[] args) {

       String filename1,filename2;
      
       //Scanner object is used to get the inputs entered by the user
       Scanner sc=new Scanner(System.in);
      
       //getting the first file name entered by the user
       System.out.print(\"Enter first filename :\");
       filename1=sc.next();

      
       //getting the second file name entered by the user
       System.out.print(\"Enter second filename :\");
       filename2=sc.next();
      
       //Creating the File class objects
       File f1=new File(filename1);
       File f2=new File(filename2);
      
       //calling toString() method on the File Class object
       System.out.println(\"Name of the file#1\"+f1.toString());
       System.out.println(\"Name of the file#2\"+f2.toString());
      
       //checking whether the two files names are equal or not
       System.out.println(\"\ :: Comparing whether the two file name are equal or not ::\");
       boolean bool=f1.equals(f2);
       if(bool)
           System.out.println(filename1+\" and \"+filename2+\" are equal\");
       else
           System.out.println(filename1+\" and \"+filename2+\" are not equal\");
      
       //Displaying the file extensions
       System.out.println(\"\ The Extension of the file 1:\"+f1.findExtensionOfFile(filename1));
       System.out.println(\"The Extension of the file 2:\"+f2.findExtensionOfFile(filename2));


   }

}

___________________

Output#1:

Enter first filename :Test.java
Enter second filename :Test.java
Name of the file#1Test.java
Name of the file#2Test.java

:: Comparing whether the two file name are equal or not ::
Test.java and Test.java are equal

The Extension of the file 1:java
The Extension of the file 2:java

___________

Output#2:

Enter first filename :TestFile
Enter second filename :Test.java
Name of the file#1TestFile
Name of the file#2Test.java

:: Comparing whether the two file name are equal or not ::
TestFile and Test.java are not equal

The Extension of the file 1:Unknown Extension
The Extension of the file 2:java

___________Thank You

 Write a class encapsulating the concept of a file, assuming a file has only a single attribute: the name of the file. Include a constructor, the accessors and
 Write a class encapsulating the concept of a file, assuming a file has only a single attribute: the name of the file. Include a constructor, the accessors and
 Write a class encapsulating the concept of a file, assuming a file has only a single attribute: the name of the file. Include a constructor, the accessors and

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site