Il mio compito è programmare un file system usando oggetti cartella e oggetti file, quindi utilizzare un oggetto File System per gestire i due oggetti nello stesso albero. Il mio problema è che non riesco a capire come trattare i due oggetti come lo stesso.Estratto Java per trattare due oggetti come gli stessi in un albero
L'assegnazione dice "Potrebbe essere utile disporre di una classe astratta ereditata dal file e dalla cartella, in modo da poterli trattare allo stesso modo." Ma continuo a ricevere errori.
import java.util.*;
public class FileSys {
private Node firstFolder;
public void newFolder(String loc) {
if (firstFolder == null) { // If there are no folders
Node folder = new Folder(loc);
firstFolder = folder;
}
else { // If there are folders
String s = loc;
String[] folders = s.split("\\\\"); // Each file/folder name is put into an array
boolean found; // Flag if found
Node current = firstFolder; //Sets the first folder to the current
int n = 0;
while (folders.length - 1 > n) { // To find the folder being added to
int i = 0;
found = false; // Resets flag
while (current.size > i) { // To search through the names of the folders
if (current.next[i].name.equalsIgnoreCase(folders[n])) {
current = current.next[i];
found = true; // Raises flag
break;
}
i++;
}
if (!found) // incomplete. Add Exception.
System.out.println("ERROR");
n++;
}
Node folder = new Folder(folders[folders.length - 1]);
current.next[current.size] = folder;
}
}
abstract class Node {
String name;
Node(String name) {
this.name = name;
}
}
private class File extends Node {
String data;
File(String nm, String data) {
super(nm);
this.data = data;
}
}
private class Folder extends Node {
private static final int ARRAYSIZE = 20; // default array size
private int size = 0;
private Node[] next = new Node[ARRAYSIZE];
public Folder(String nm) {
super(nm);
next[0] = null;
}
}
// Main method omitted
}
Apprezzo qualsiasi aiuto nella giusta direzione! Sento che è un errore estremamente semplice, ma non ho abbastanza esperienza con oggetti e abstract per sapere cosa c'è che non va. Ho provato a trasmettere, ma questo porta a più errori durante il runtime. Grazie!
Edit:
FileSys.java:55: error: cannot find symbol
while(current.size > i)
^
symbol: variable size
location: variable current of type FileSys.Node
FileSys.java:57: error: cannot find symbol
if(current.next[i].name.equalsIgnoreCase(folders[n]))
^
symbol: variable next
location: variable current of type FileSys.Node
FileSys.java:59: error: cannot find symbol
current = current.next[i];
^
symbol: variable next
location: variable current of type FileSys.Node
FileSys.java:76: error: cannot find symbol
current.next[current.size] = folder;
^
symbol: variable next
location: variable current of type FileSys.Node
FileSys.java:76: error: cannot find symbol
current.next[current.size] = folder;
^
symbol: variable size
location: variable current of type FileSys.Node
5 errors
Qual è esattamente il problema? – immibis
Quali errori ottieni? La configurazione della tua classe ha senso. – ApproachingDarknessFish
Non so come trattare due oggetti come uguali. Ho creato una classe Node astratta ma ogni volta che utilizzo Node folder = new Folder(); Non riesco a utilizzare nessuno dei dati che si trovano nell'oggetto Cartella, come next o size. – Bleu