This program determines the name and type of a file when given its string. For example, the file 42-README.txt
has the name README
and the type is txt
. We will ignore the numbering that comes before the "-". The format of a file will thus be "number-name.type"
(1) Develop the getFileName
and getFileType
methods.
a) Copy the following method stubs into your FileNameAndType class.
/**
* Returns of the name of the file. Hint: look up indexOf and substring in the String Class
*
* @param file The complete file String
* @return The name of the file
*/
public static String getFileName(String file) {
//TODO: FILL IN BODY
}
/**
* Returns the type of the file as a lower case string. Hint: look up indexOf and substring in the String Class
*
* @param file The complete file String
* @return The type of the file
*/
public static String getFileType(String file) {
//TODO: FILL IN BODY
}
b) Before implementing the getFileName
and getFileType
methods, think about examples of values (test cases) that might be passed in to file
. Copy the following methods into your FileNameAndType class and replace the /*FIX ME*/
with 3 different calls to the getFileName
and getFileType
methods. Each call should test a different example input to the method. You should have a minimum of 3 tests, but you may have more.
Note: An easy way to develop tests is to print out the expected output of a method on one line and then on the second line print out the actual output of the method you have written. Thus by comparing these two outputs, you will be able to "test" your code by ensuring the outputs are identical.
/**
* Runs tests on the testGetFileName method.
*/
public static void testGetFileName() {
System.out.println(/*FIX ME*/);
System.out.println(/*FIX ME*/);
System.out.println(/*FIX ME*/);
}
/**
* Runs tests on the testGetFileType method.
*/
public static void testGetFileType() {
System.out.println(/*FIX ME*/);
System.out.println(/*FIX ME*/);
System.out.println(/*FIX ME*/);
}
c) Implement the getFileName
and getFileType
methods and use the testGetFileName
and testGetFileType
methods to test them before submitting, respectively. However, be sure to remove or comment out your test method calls before submitting.
(2) Using the following comments, complete the main method.
-
You should create an instance of a Scanner class (Scanner object) with the argument of System.in.
-
Then, for the next three file strings in your Scanner instance, print out (each on their own line) the strings generated by calling
inputvalue returned from getFileNamevalue returned from getFileTypegetFileName
andgetFileType
. Given the following input:122-Homework.txt
Homework
txt
3-Resume.PDF
Resume
pdf
33-HelloWorld.java
HelloWorld
java
-
The output from the main method should be:
Name: Homework, Type: txt
Name: Resume, Type: pdf
Name: HelloWorld, Type: java
Note: the input will always be a String with the format of "number-fileName.fileType".
The output text is made up in the main method, make sure your getFileName
and getFileType
methods only return the corresponding information extracted from the file name and do not contain "Name:" or "Type:" in the returning values to pass the tests.