We appreciate your visit to The following program creates objects for folders subfolders and files on a hard disk where the root of the drive is also treated as a. This page offers clear insights and highlights the essential aspects of the topic. Our goal is to provide a helpful and engaging learning experience. Explore the content and find the answers you need!
The following program creates objects for folders, subfolders, and files on a hard disk, where the root of the drive is also treated as a folder. These objects are created as instances of the classes `Folder` and `File`, which are subclasses of the abstract class `Item`. The `Filesystem` class is a Singleton and keeps track of all files and folders using an `ArrayList` of `Item`. Study the explanation and outputs in the comments below. The outputs are given in the underlined comments:
- The `getPath()` method of `Item` is used to obtain the path of a folder or a file.
- The `search()` method of `Filesystem` is used to search for a file or a folder with a given name. If two or more hits are found, all results are shown (the ordering is unimportant). For each found folder, show the path; for each found file, show the path and the file size.
Your task: Finish all missing code in the answer paper. Note: Do not use the `instanceof` operator. Otherwise, you may get 0 marks for this question!
Reminder: Read ALL given code carefully! Don't remove or change any given code! Apply `@Override` where appropriate.
```java
import java.util.ArrayList;
public class Filesystem {
private ArrayList- allItems;
public void search(String name) {
Item.search(allItems, name);
}
public void addItem(Item item) {
allItems.add(item);
}
}
import java.util.ArrayList;
public abstract class Item {
private String name;
private Folder parent;
public Item(Folder parent, String name) {
this.parent = parent;
this.name = name;
Filesystem.getInstance().addItem(this);
}
protected Folder getParent() {
return parent;
}
protected String getName() {
return name;
}
}
public class Folder extends Item {
// Additional folder-related methods and properties
}
public class File extends Item {
// Additional file-related methods and properties
}
```
Please complete the code as necessary, adhering to the guidelines provided.
public void search(String name) {
Item.search(allItems, name);
}
public void addItem(Item item) {
allItems.add(item);
}
}
import java.util.ArrayList;
public abstract class Item {
private String name;
private Folder parent;
public Item(Folder parent, String name) {
this.parent = parent;
this.name = name;
Filesystem.getInstance().addItem(this);
}
protected Folder getParent() {
return parent;
}
protected String getName() {
return name;
}
}
public class Folder extends Item {
// Additional folder-related methods and properties
}
public class File extends Item {
// Additional file-related methods and properties
}
```
Please complete the code as necessary, adhering to the guidelines provided.