Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java abstract to treat two objects as the same in a tree

My assignment is to program a file system using folder objects and file objects, then to use a File System object to manage the two objects in the same tree. My issue is that I can't figure out how to treat the two objects as the same.

The assignment says "You may find it helpful to have an abstract class that the file and folder inherit from, so that you can treat them alike." But I keep getting errors.

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
}

I appreciate any help in the right direction! I feel like it's an extremely simple mistake, but I don't have enough experience with objects and abstracts to know what's wrong. I've tried casting, but it leads to more errors during runtime. Thank you!

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
like image 516
Bleu Avatar asked Nov 11 '22 00:11

Bleu


1 Answers

When declare a variable as a class, you can only call it with methods defined for that class.

Node n = new Folder();
n.name; //Fine, all nodes have the name attribute
n.next; //Not fine, nodes do not have a next attribute

The solution is type casting. Type casting is a way of telling the compiler, "I know that this object will be of this type at runtime".

Folder f = (Folder) n; //We are telling the compiler that n is a Folder
f.next; //Fine, because folders have the next attribute

There is one last problem: we need to differentiate between folders and files. We can use the instanceof operator.

if(n instanceof Folder){
    doFolderStuff();
}else if (n instanceof File){
    doFileStuff()
}
like image 76
Joshua Avatar answered Nov 14 '22 21:11

Joshua