Question
Create code to manage a directory tree and provide the user with a basic subset of commands for doing so. We will model this assignment
Create code to manage a directory tree and provide the user with a basic subset of commands for doing so. We will model this assignment on the Unix command line1 . If you are familiar with Linux or Mac OS command line, then you already know how to use these commands; if you are familiar with a Windows command line, then the commands are the same, except “pwd” on Unix is the same as “cd” without any arguments on Windows, and “ls” on Unix is the same as “dir” on Windows. There are two things to keep track of: the directory tree structure and the current directory that the user is in. Initially, there is only the root directory, which has the empty string for its name.
The user may create subdirectories of the current directory (i.e., children) by issuing “mkdir” command; delete subdirectories by issuing “rmdir” command; and change the current directory to a subdirectory by issuing a “cd” command. In this assignment, these commands take a single argument, which is the name of the subdirectory. (In a real shell, they are more sophisticated.) In addition, “cd ..” changes the current directory to the parent directory.
The user may also see all the children of the current directory by issuing the “ls” command, the path from the root to the current directory by issuing the “pwd” command, the subtree rooted at the current directory by issuing the “tree” command, and see the size of the subtree rooted at the current directory (including the current directory) by issuing the “size” command. (Note that “tree” and “size” are not commands present in a typical command line.)
Here is an example interaction:
> pwd / > ls > size 1 > mkdir cs > mkdir math > size 3 > cd math > pwd /math > mkdir calculus > size 2 > mkdir algebra > mkdir geometry > ls calculus algebra geometry > tree math calculus algebra geometry > cd .. > pwd / > size 6 > mkdir cs Directory already exists > cd cs > pwd /cs > mkdir cryptography 2 > mkdir graphics > mkdir networking > cd databases No such directory > mkdir databases > ls cryptography graphics networking databases > cd cryptography > pwd /cs/cryptography > ls > mkdir public-key > mkdir symmetric-key > cd public-key > pwd /cs/cryptography/public-key > cd .. > tree cryptography public-key symmetric-key > cd .. > tree cs cryptography public-key symmetric-key graphics networking databases > mkdir badLanguages > cd badLanguages > mkdir Basic > cd .. > tree cs cryptography public-key symmetric-key graphics networking databases badLanguages Basic > rmdir badLanguages > tree cs cryptography 3 public-key symmetric-key graphics networking databases > cd .. > tree cs cryptography public-key symmetric-key graphics networking databases math calculus algebra geometry > cd .. No such directory > exit Goodbye.
Make sure your output matches verbatim, including indentation by two spaces for every level of the tree. We provide you with user-interface code in SimpleShell.java — no need to modify it (but feel free to if you need for testing—we won’t grade it). Implement and submit DirectoryTree.java.
TODO code:
package hw6;
public class DirectoryTree {
// TODO nothing here is implemented, these are just empty
// shells so the compiler doesn't complain
// Implement private members, public methods,
// and private helper methods, etc., as needed,
// in order to get the behavior described on the pset
// You will want to maintain two private members:
// the root and the current directory.
public boolean mkdir (String name) {
return false;
}
public boolean cd (String name) {
return false;
}
public boolean cdUp() {
return false;
}
public boolean rmdir (String name) {
return false;
}
public String ls () {
return "";
}
public String printSubTree() {
return "";
}
public String pwd () {
return "";
}
public int numNodes() {
return 0;
}
}
Simple shell:
package hw6;
import java.util.Scanner;
public class SimpleShellDriver {
public static void main(String [] args) {
final String prompt = "> ";
Scanner sc = new Scanner(System.in);
DirectoryTree t = new DirectoryTree();
System.out.print(prompt);
while (sc.hasNext()) {
String cmd = sc.next();
if (cmd.equals("ls")) {
System.out.print(t.ls());
}
else if (cmd.equals("tree")) {
System.out.print(t.printSubTree());
}
else if (cmd.equals("cd")) {
String dir = sc.next();
boolean result;
if (dir.equals(".."))
result = t.cdUp();
else
result = t.cd(dir);
if (result==false)
System.out.println("No such directory");
}
else if (cmd.equals("rmdir")) {
if (t.rmdir(sc.next())==false)
System.out.println("No such directory");
}
else if (cmd.equals("mkdir")) {
if (t.mkdir(sc.next())==false)
System.out.println("Directory already exists");
}
else if (cmd.equals("pwd")) {
System.out.println(t.pwd());
}
else if (cmd.equals("size")) {
System.out.println(t.numNodes());
}
else if (cmd.equals("exit")) {
System.out.println("Goodbye.");
sc.close();
return;
}
else
System.out.println("Invalid command. Valid commands are ls, tree, cd, rmdir, mkdir, pwd, exit.");
System.out.print(prompt);
}
sc.close();
}
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Samplejava import javautilScanner public class sample public static void mainString args final String prompt Scanner sc new ScannerSystemin tree t new ...Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Document Format ( 2 attachments)
635e1331ce81c_181230.pdf
180 KBs PDF File
635e1331ce81c_181230.docx
120 KBs Word File
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started