Post

Created by @nathanedwards
 at November 3rd 2023, 4:31:01 pm.

AP Computer Science Exam Question:

Consider the following scenario: You are developing a file management system in Java and need to create a class to represent files. Design a File class that meets the following requirements:

  1. The class should have instance variables to store the name, size, and extension of the file.
  2. The class should have a constructor that takes the name, size, and extension as parameters and initializes the corresponding instance variables.
  3. The class should have appropriate getter and setter methods for each instance variable.
  4. The class should have a method named getSizeInKilobytes that returns the size of the file in kilobytes (assuming the size is given in bytes).
  5. The class should have a method named isValid that returns true if the file name has a valid extension, and false otherwise. A valid extension is defined as either ".txt" or ".java".

Implement the File class according to the requirements above. Then, write a Java program that demonstrates the usage of the File class by creating an instance of the File class and performing the following operations:

  1. Set the name of the file to "exampleFile".
  2. Set the size of the file to 2048 bytes.
  3. Set the extension of the file to ".txt".
  4. Print the size of the file in kilobytes using the getSizeInKilobytes method.
  5. Print whether the file is valid using the isValid method.
class File {
    private String name;
    private int size;
    private String extension;
    
    public File(String name, int size, String extension) {
        this.name = name;
        this.size = size;
        this.extension = extension;
    }
    
    public String getName() {
        return name;
    }
    
    public void setName(String name) {
        this.name = name;
    }
    
    public int getSize() {
        return size;
    }
    
    public void setSize(int size) {
        this.size = size;
    }
    
    public String getExtension() {
        return extension;
    }
    
    public void setExtension(String extension) {
        this.extension = extension;
    }
    
    public int getSizeInKilobytes() {
        return size / 1024;
    }
    
    public boolean isValid() {
        return extension.equals(".txt") || extension.equals(".java");
    }
}

public class Main {
    public static void main(String[] args) {
        File file = new File("exampleFile", 2048, ".txt");
        
        System.out.println("File size in kilobytes: " + file.getSizeInKilobytes());
        System.out.println("Is file valid? " + file.isValid());
    }
}

Explanation:

  1. The File class is created with three private instance variables: name, size, and extension.
  2. The class constructor takes the name, size, and extension as parameters and initializes the corresponding instance variables.
  3. Getter and setter methods are provided for each instance variable to ensure encapsulation.
  4. The getSizeInKilobytes method returns the size of the file in kilobytes by dividing the size in bytes by 1024.
  5. The isValid method checks if the file extension is either ".txt" or ".java" and returns true or false accordingly.
  6. In the main method, an instance of the File class is created with the specified parameters: "exampleFile" as the name, 2048 as the size, and ".txt" as the extension.
  7. The size of the file in kilobytes is printed using the getSizeInKilobytes method.
  8. The validity of the file is printed using the isValid method.