Starting from:

$20

CS1331 Homework 13 -Ls-Cat Solved


Problem Description
Still stuck in the future, you’re starting to enjoy it. You’ve helped space explorers improve their data infrastructure, and briefly got launched into the world of Pokémon where you’ve left Professor Oak and the Pokemon Center eternally grateful for your services.

Unfortunately, you learn that “Georgia Tech University” was really just a front for a school named “Georgia

Tech”! Professor Oak reveals himself to be the professor of a class named CS 1331 and tells you to get back to your homework and to stop reading pointless problem descriptions.

Solution Description
For this assignment, create the following files:

•      FileHelper.java

•      CatException.java

•      Tester.java

FileHelper Class:

FileHelper will contain static helper methods that have functionality similar to the ls and cat unix commands.

Methods:

•      public static void printLs(String path)

–    This will print out all of the files and folders that are contained within the folder located at path.

–    You must use the printLsHelper method below.

–    You may assume that the path parameter contains the path to a folder.

•      private static void printLsHelper(String path, int indentationLevel)

–    Must recursively call itself

–    For every indentationLevel, print 4 spaces at the beginning of the line before printing anything else.

–    If the path that is passed in is a path to a file, print out the name of the file.

–    If the path that is passed in is the path to a folder, print out the name of the folder and then, indented a level, print out the contents of the folder (Hint: recursion).

–    This problem must be solved recursively or you will receive a heavy deduction (Hint: if you are not making at least one recursive call per folder, you are probably doing it wrong). – See the example below in testing your code for formatting to follow!

•      public static void printCat(String path)

–    If path isn’t a path to a real file or is a directory, throw a CatException. The message should be "Path invalid." and pass in the path.

–    Otherwise

∗ For each line in the file, print out "{line number}: {line from text file}" ∗ See the example below in testing your code for formatting to follow!

CatException Class:

•      Make this custom Exception be a Checked Exception

•      Take a String message and String problemFile into the constructor. Pass message to super and store problemFile in a private final field called problemFile to keep track of the path to the file.

•      Override getMessage() to include the original message (Hint: use super’s getMessage()) plus the absolute path to the problemFile in the following format: "{original message}. Problem with file {absolute path}!"

–    problemFile may be an absolute path or a relative path. Make sure to print out an absolute path.

Tester Class:

•      Have a main method that takes in a file path in a command line argument. Print the output of ls of the current directory and then print the output of cat with the passed in file.

–    Make sure your path to the current directory is a relative path and not a hard-coded or absolute path.

–    Do not use backslashes (“\”) when indicating the current directory. Use forward slashes instead.

–    Your main method should not handle any Exceptions that could occur when calling printLs or printCat and instead propogate them to the caller of the main method.

Testing your code:

The below examples will use the folder structure

Foo

|- Bar

| |- Biz

| | |- Hi.txt

| | |- Shishi.png

| |- Lambda.java

|- Bat

| |- Scary.gif

|- Stop.txt

|- Spooky.txt

|- FileHelper.java

|- FileHelper.class

|- CatException.java

|- CatException.class

|- Tester.java

|- Tester.class

Assume that Spooky.txt contains:

In the cool of the evenin' when ev'rything is gettin' kind of groovy

I call you up and ask you if you'd like to go with me and see a movie

First you say "no", you've got some plans for the night And then you stop, and say, "all right"

Love is kinda crazy with a spooky little girl like you and Stop.txt contains:

In Defense of Christmas Music in October.

Ah, Christmas music! The warm tunes that remind so many of us of

huddling around a fire surrounded by family.

The sounds that remind of pine scents, mistletoe, and holiday cheer. There's so much good feeling attached to Christmas music that it's nice to dip into the holiday playlists early and get excited about what's to come.

printLs("Foo") would print out

Foo

Bar

Biz

Hi.txt

Shishi.png

Lambda.java

Bat

Scary.gif

Stop.txt

Spooky.txt

FileHelper.java

FileHelper.class

CatException.java

CatException.class

Tester.java

Tester.class printCat("Foo/Spooky.txt") would print out

1: In the cool of the evenin' when ev'rything is gettin' kind of groovy

2: I call you up and ask you if you'd like to go with me and see a movie

3: First you say "no", you've got some plans for the night

4: And then you stop, and say, "all right"

5: Love is kinda crazy with a spooky little girl like you

Running java Tester ./Stop.txt would output the following assuming that you are currently in the Foo directory

Foo

Bar

Biz

Hi.txt

Shishi.png

Lambda.java

Bat

Scary.gif

Stop.txt

Spooky.txt

FileHelper.java

FileHelper.class

CatException.java

CatException.class

Tester.java

Tester.class

1: In Defense of Christmas Music in October.

2: Ah, Christmas music! The warm tunes that remind so many of us of 3: huddling around a fire surrounded by family.

4: The sounds that remind of pine scents, mistletoe, and holiday cheer. 5: There's so much good feeling attached to Christmas music that it's nice to dip 6: into the holiday playlists early and get excited about what's to come.

Depending on how you implement it Foo may just be . or ./ in all of the above examples and that is okay.

Allowed Imports
• You are only allowed to import the following classes:

–    java.util.Scanner

–    java.io.*

–    java.nio.file.*

More products