haixi0118 2021-02-10 16:18 采纳率: 80%
浏览 135
已采纳

求问这道题该如何解答呢

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 getFileName and getFileType. Given the following input:

    inputvalue returned from getFileNamevalue returned from getFileType122-Homework.txtHomeworktxt3-Resume.PDFResumepdf33-HelloWorld.javaHelloWorldjava
  • 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.

  • 写回答

1条回答 默认 最新

  • 王二小丷 2021-02-10 16:51
    关注
    import java.util.Scanner;
    
    public class FileNameAndType {
    
    	public static void main(String[] args) {
    		Scanner scanner = new Scanner(System.in);
    		System.out.println("input file string:");
    		// 没太看懂这题目的输入是啥格式,直接来了三次循环读取输入
    		for (int i = 0; i < 3; i++) {
    			if (scanner.hasNext()) {
    				String file = scanner.next();
    				System.out.println("Name: " + getFileName(file) + ", Type: " + getFileType(file));
    			}
    		}
    		scanner.close();
    	}
    
    	/**
    	 * 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) {
    		// 看题目意思应该是输入格式固定不需要考虑异常情况,直接使用了他指定的indexOf() substring()
    		return file.substring(file.indexOf("-") + 1, file.indexOf("."));
    	}
    
    	/**
    	 * 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) {
    		// 看题目意思应该是输入格式固定不需要考虑异常情况,直接使用了他指定的indexOf() substring() 还有转小写
    		return file.substring(file.indexOf(".") + 1).toLowerCase();
    	}
    
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 用hfss做微带贴片阵列天线的时候分析设置有问题
  • ¥50 我撰写的python爬虫爬不了 要爬的网址有反爬机制
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥120 计算机网络的新校区组网设计
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等