将项目拆分为两个文件

时间:2015-03-18 23:38:58

标签: java file split

我正在开发一个需要两个文件类(实现和测试)的I项目。我已经完成了我的代码,但我不知道如何将它们分成两个文件。这是我的代码:

public class Implementation 
{       
    public static void main(String[] args)
    {        
        double degree = 0;
        String celsius = null;
        String fahrenheit; 
        String[] days = {"Very Cold", "Cold", "Mild", "Very Mild", "Warm", "Very Warm", "Hot"};       
        if (args.length != 3)
        {
            System.out.println("Error! Please try again.");
            System.exit(0);
        }
        else
        {
            degree = Double.parseDouble(args[0]);
            celsius = args[1]; 
            fahrenheit = args[2];        
        }

        switch (celsius)    
        {
            case "c":
            System.out.printf("%n%s Celsius is %.5s Fahrenheit **%s**\n", args[0], fahrenheit(degree), days[checkDegree(fahrenheit(degree))]);
            break;
            case "f":
            System.out.printf("%n%s Fahrenheit is %.5s Celsius **%s**\n", args[0], celsius(degree), days[checkDegree(degree)]);
            break;
            default:
            System.out.println("Error! Shutting down");
            break;
        }
    }
    public static double celsius(double fahrenheitTemperature)
    {
        return ( 5.0 / 9.0 * ((double) fahrenheitTemperature - 32));
    }
    public static double fahrenheit(double celsiusTemperature)
    {
        return  ( 9.0 / 5.0 * (double) celsiusTemperature + 32);
    }
    public static int checkDegree(double degree) 
    {
        int myReturn = 0;
        if (degree < 0)
            {    
            myReturn = 0;
            }
            if (degree >= 0 && degree <= 32)
            {    
            myReturn = 1;
            }
            if (degree > 32 && degree <= 50)
            {    
            myReturn = 2;
            }
            if (degree > 50 && degree <= 60)
            {    
            myReturn = 3;
            }
            if (degree > 60 && degree <= 70)
            {    
            myReturn = 4;
            }
            if (degree > 70 && degree <= 90)
            {    
            myReturn = 5;
            }
            if (degree > 90)
            {    
            myReturn = 6;
            }   
            return myReturn;
    }
}

Test文件中将有足够的最小代码来运行该程序。谁能帮帮我吗?谢谢!

1 个答案:

答案 0 :(得分:2)

我能想到的最简单的方法是将main方法移动到Test.java并在实现中保持您的业务逻辑,

package com.stackoverflow.readfileinfinitely;

public class Test {
public static void main(String[] args)
{        

    double degree = 0;
    String celsius = null;
    String fahrenheit; 
    String[] days = {"Very Cold", "Cold", "Mild", "Very Mild", "Warm", "Very Warm", "Hot"};       
    if (args.length != 3)
    {
        System.out.println("Error! Please try again.");
        System.exit(0);
    }
    else
    {
        degree = Double.parseDouble(args[0]);
        celsius = args[1]; 
        fahrenheit = args[2];        
    }

    switch (celsius)    
    {
        case "c":
        System.out.printf("%n%s Celsius is %.5s Fahrenheit **%s**\n", args[0], Implementation.fahrenheit(degree), days[Implementation.checkDegree(Implementation.fahrenheit(degree))]);
        break;
        case "f":
        System.out.printf("%n%s Fahrenheit is %.5s Celsius **%s**\n", args[0], Implementation.celsius(degree), days[Implementation.checkDegree(degree)]);
        break;
        default:
        System.out.println("Error! Shutting down");
        break;
    }
}
}

但您真的想了解更多有关实际测试的信息,请参阅JUnit测试教程,http://www.tutorialspoint.com/junit/

希望这有帮助!!!