Back to 111 Main Page

Mobius strip

Assignment 4


Create a Filename class, which will hold filenames such as readme.txt, song.mp3, and In doing so, you will use a number of the methods of the String class.


Write a Filename class that includes the following details:

Instance variables (private):

  • String filename -- Contains the complete filename (as passed to the constructor).

Constructor (public):

  • Filename(String name)

Methods (public):

  • String toString() -- Returns the complete filename.
  • int getLength() -- Returns the length (number of characters) in the complete filename.
  • String getName() -- Returns only the name part of the filename, which means all characters up to (but not including) the last '.' character in the complete filename.
  • String getExtension() -- Returns the extension part of the file name, which means all characters after the last '.' character in the complete filename.
  • String getType() -- Returns the same as getExtension(), but uppercased (ie, all in capital letters).
  • String getSaferFilename() -- Returns the complete filename all lowercased, with all spaces (' ') replaced by underscores ('_').

You can assume that every Filename that is created will be correctly passed a name that includes at least one alpha-numeric character, followed by a '.', followed by at least one more alpha-numeric.

Now, to test the new Filename class, write a main method in the Filename class. In this method, you should:

  • Prompt the user to enter a filename.
  • Use the Scanner class to read what the user types at the keyboard.
  • Use the user-entered String to create a new Filename object.
  • Print out the results of some of your method calls on this Filename object.

To ensure correctness, you should run my If you get any compile errors, fix your Filename class to match the specifications above. When you run FilenameTester, you should get exactly the same results as are shown on each line in [brackets].

What to Submit

Attach your file to an email.


What's with this getSaferFilename method?
Some operating systems are case-insensitive and some have trouble with spaces in a filename. This method circumvents some of these problems, and would be good to use for most filenames that will be part of a URL.

However, it is not completely safe. There are a number of special characters that cannot appear in a filename. Also, some operating systems (such as MS-DOS) have limits on a filename's length (such as eight characters, a dot, and then 3 characters). But checking for these is a bit beyond your skill level at this time. :)

Where's that example code we wrote in lab?
Here: (Now updated to include the new methods from Wednesday.)
How do I replace all spaces with underscores?
There is a method in the String class that will replace all instances of the first parameter with the second parameter. It is called something like this: str.replace(" ", "_"); See the Java API documentation for more details.


Out of 10 points:

1 - Submission
Follows required submission policies.
1 - Correct constructor
5 - Correct methods
toString and getLength worth 0.5 points; all others worth 1.
1 - Main method as described
You must use a Scanner to collect data from the user, create a Filename object with it, and call at least one method on it (printing the results to the screen).
2 - FilenameTester compiles and runs correctly using your Filename.


~ztomasze Index : TA Details: ICS111: Assignment 4
Last Edited: 05 Oct 2007
©2007 by Z. Tomaszewski.