从同一个java项目中的另一个类调用枚举值 [英] Calling a Enum value from another class within the same java project

查看:205
本文介绍了从同一个java项目中的另一个类调用枚举值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Java类的介绍中有一个任务,我们假定从文件中读取数据,然后将其输出到另一个文件。我似乎有文件I / O部分下来。我所遇到的问题是数学运算阶段。我们被给了一个类,称为SimpleMath,并且假设从它调用方法。这里是给定的类:

  import java.text.DecimalFormat; 

public class SimpleMath {

//操作枚举
public enum eOperation {
PERIMETER,AREA
}

//转换枚举
public枚举eScale {
CELSIUSFAHRENHEIT,FAHRENHEITCELSIUS
}


// computeHypotenuse方法
public static double computeHypotenuse (双相邻,双对){

//毕达哥拉斯定理
返回Math.sqrt(相邻*相邻+对面*相反);
}

// solveQuadratic方法
public static double solveQuadratic(double a,double b,double c){

//二次公式
double minusRoot =(-b - Math.sqrt((b * b) - (4.0 * a * c)))/(2.0 * a);
double plusRoot =(-b + Math.sqrt((b * b) - (4.0 * a * c)))/(2.0 * a);
return(plusRoot> = minusRoot?plusRoot:minusRoot);
}

// convertTemperature方法
public static double convertTemperature(eScale scale,double degrees){

//缩放转换
if (scale == eScale.CELSIUSFAHRENHEIT)
return(((9.0 / 5.0)*度)+ 32.0);
else
return((5.0 / 9.0)*(degrees - 32.0));
}

// geometryCircle方法
public static double geometryCircle(eOperation op,double radius){

//基本几何
if (op == eOperation.PERIMETER)
return(Math.PI * radius * 2.0);
else
return(Math.PI * radius * radius);
}
}

我一直在使用的代码是我的代码当我尝试从SimpleMath类调用geometryCircle和convertTemperature方法。这是因为这两种方法在调用进入该类的变量时使用枚举变量。事实证明,无论我看起来如何,我正在工作的P5类都是在类之间传递这些枚举变量的问题。这是我迄今为止写的P5代码:

  import java.io.File; 
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Scanner;

import SimpleMath.eOperation;
import SimpleMath.eScale;


public class P5
{

//这些是使用的输入变量
double相邻,相反,CoEff0,CoEff1,CoEff2华氏,半径;

//这些是用于输出值的变量
double Hypotenuse,Root,Celsius,Area;

//这是类P5 main方法的主要方法,它告诉这个类中其他方法在哪里找到它们的变量。
public static void main(String [] args)
{
//将类P5实例化为变量p5。
P5 p5 = new P5();

//发送readFile方法,同时传递存储在类中的参数。
p5.readFile(args [0]);

//启动没有添加变量的computeMath方法。
p5.computeMath();

//启动writeFile方法,并将要写入的新文件传递给作为类的一部分存储的所需名称。
p5.writeFile(args [1]);

}


/ *此方法告诉计算机读取文件
*,它在参数选项卡下的运行配置窗口中指定在这个类的文件中,
*然后它将从文件中提取的值分配给适当的输入变量
* /
private void readFile(String InputFile)
{
try
{
//这告诉计算机如何以及在哪里寻找指定的输入文件。
扫描仪ReadFile =新扫描仪(新文件(InputFile));

//这些设置变量,以便稍后可以分配文件中的所有行。
String Line1 = ReadFile.nextLine();
String Line2 = ReadFile.nextLine();
String Line3 = ReadFile.nextLine();
String Line4 = ReadFile.nextLine();
String Line5 = ReadFile.nextLine();
String Line6 = ReadFile.nextLine();
String Line7 = ReadFile.nextLine();

//这些是变量,这里它们正在从正在读取的文件中分配值。
Adjacent = Double.valueOf(Line1);
Opposite = Double.valueOf(Line2);
CoEff0 = Double.valueOf(Line3);
CoEff1 = Double.valueOf(Line4);
CoEff2 = Double.valueOf(Line5);
Fahrenheit = Double.valueOf(Line6);
Radius = Double.valueOf(Line7);

//这让用户看到已经写入变量的值,主要用于错误检查。
System.out.println(相邻:+相邻);
System.out.println(Opposite:+对面);
System.out.println(CoEff0:+ CoEff0);
System.out.println(CoEff1:+ CoEff1);
System.out.println(CoEff2:+ CoEff2);
System.out.println(Fahrenheit:+ Fahrenheit);
System.out.println(Radius:+ Radius);

//关闭指定的文件,并将其从流中断。
ReadFile.close();
}
catch(IOException e)

{
System.out.println(Can not Read File:+ InputFile);
System.exit(0);
}
}

//该方法使用ReadFile方法中的值,并对它们进行数学运算。
private void computeMath()
{
//这调用
Hypotenuse = SimpleMath.computeHypotenuse(相邻,相对);
Root = SimpleMath.solveQuadratic(CoEff0,CoEff1,CoEff2);
Celsius = SimpleMath.convertTemperature(eScale.FAHRENHEITCELSIUS,Fahrenheit);
Area = SimpleMath.geometryCircle(eOperation.AREA,Radius);
}


/ *此方法将所有在整个程序
*中确定的变量值,并将其写入RunConfigurations窗口中指定的文件
*此类的参数选项卡。
* /
private void writeFile(String OutputFile)
{
try
{
//这将启动PrintWriter类,允许程序写入一份文件。
PrintWriter Output = new PrintWriter(new File(OutputFile));

//这些是程序在指定文件中写入的文本行。
Output.println(Adjacent =+ Adjacent);
Output.println(Opposite =+ Opposite);
Output.println(Coefficent 0 =+ CoEff0);
Output.println(Coefficent 1 =+ CoEff1);
Output.println(Coefficent 2 =+ CoEff2);
Output.println(Fahrenheit =+ Fahrenheit);
Output.println(Radius Of Circle =+ Radius);

//关闭指定的文件,并将其从流中断。
Output.close();

}
catch(IOException e)



{
System.out.println(Can not Write File: + OutputFile);
System.exit(0);
}
}

}

所以任何帮助将是很好的,甚至猜测为什么这个代码的行为方式是这样的。谢谢

解决方案

只需从枚举中添加类名称作为下面。当他们被宣布为公开时,你应该会很好。

 摄氏= SimpleMath.convertTemperature(
SimpleMath.eScale.FAHRENHEITCELSIUS ,华氏);
Area = SimpleMath.geometryCircle(SimpleMath.eOperation.AREA,Radius);


I have an assignment in my introduction to Java class where we are suppose to read from a file, conduct mathematical operations on it, then output it to another file. i seem to have the file I/O portion down. The problem that I am having is in the mathematical operation stage. We were given a class, called SimpleMath, and are suppose to call methods from it. here is the given class:

import java.text.DecimalFormat;

public class SimpleMath {

    // Operation enumeration
    public enum eOperation {
        PERIMETER, AREA
    }

    // Conversion enumeration
    public enum eScale {
        CELSIUSFAHRENHEIT, FAHRENHEITCELSIUS
    }    


    // computeHypotenuse method
    public static double computeHypotenuse(double adjacent, double opposite) {

        // Pythagorean theorem
        return Math.sqrt(adjacent * adjacent + opposite * opposite);
    }

    // solveQuadratic method
    public static double solveQuadratic(double a, double b, double c) {

        // Quadratic formula
        double minusRoot = (-b - Math.sqrt((b * b) - (4.0 * a * c))) / (2.0 * a);
        double plusRoot  = (-b + Math.sqrt((b * b) - (4.0 * a * c))) / (2.0 * a);
        return (plusRoot >= minusRoot ? plusRoot : minusRoot);
    }

    // convertTemperature method
    public static double convertTemperature(eScale scale, double degrees) {

        // Scale conversion
        if (scale == eScale.CELSIUSFAHRENHEIT)
            return (((9.0 / 5.0) * degrees) + 32.0);
        else
            return ((5.0 / 9.0) * (degrees - 32.0));
    }

    // geometryCircle method
    public static double geometryCircle(eOperation op, double radius) {

        // Basic geometry
        if (op == eOperation.PERIMETER)
            return (Math.PI * radius * 2.0);
        else
            return (Math.PI * radius * radius);
    }
}

The Problem that I have been having is in my code when i try to call the methods geometryCircle and convertTemperature from the SimpleMath class. This is because the two methods use enum variables in the calling of the variables entering the class. It turns out that no matter what I seem to do the P5 class that I am working in is having problems passing these enum variables between classes. Here is the P5 code i have written so far:

  import java.io.File;
    import java.io.IOException;
    import java.io.PrintWriter;
    import java.util.Scanner;

    import SimpleMath.eOperation;
    import SimpleMath.eScale;


public class P5 
{   

// These are the input variables used
    double Adjacent,Opposite,CoEff0, CoEff1, CoEff2, Fahrenheit,Radius;

    // These are the variables used for output values
    double Hypotenuse, Root, Celsius, Area;

    // This is the main method of class P5 main method that tells the other methods in this class where to find their variables.    
    public static void main(String[] args) 
    {
        // Instantiates the Class P5 into variable p5.
        P5 p5=new P5();

        // Initiates the readFile method while passing it the arguments that are stored in the class.
        p5.readFile(args[0]);

        // Initiates the computeMath method with no added variables.
        p5.computeMath();

        // Initiates the writeFile method and passes the desired name, which is stored as a part of the class, of the new file being written to. 
        p5.writeFile(args[1]);

    }


    /* This method tells the computer to read in a file,
     * which is specified in the RunConfigurations window under the Arguments tab in the file for this class,
     * and then it assigns the values pulled from the file to the appropriate input variables
     */
    private void readFile(String InputFile)
    {
        try
            {
                // This tells the computer how and where to look for the designated input file.
                Scanner ReadFile = new Scanner(new File(InputFile));

                // These set up variables so that all of the lines in the files can be assigned later.
                String Line1 = ReadFile.nextLine();
                String Line2 = ReadFile.nextLine();
                String Line3 = ReadFile.nextLine();
                String Line4 = ReadFile.nextLine();
                String Line5 = ReadFile.nextLine();
                String Line6 = ReadFile.nextLine();
                String Line7 = ReadFile.nextLine();

                // These are the variables, and here they are being assigned values from the file that is being read.
                Adjacent = Double.valueOf(Line1);
                Opposite = Double.valueOf(Line2);
                CoEff0 = Double.valueOf(Line3);
                CoEff1 = Double.valueOf(Line4);
                CoEff2 = Double.valueOf(Line5);
                Fahrenheit = Double.valueOf(Line6);
                Radius = Double.valueOf(Line7);

                // This lets the user see what values have been written to the variables, mostly for error checking purposes.
                System.out.println("Adjacent: " + Adjacent);
                System.out.println("Opposite: " + Opposite);
                System.out.println("CoEff0: " + CoEff0);
                System.out.println("CoEff1: " + CoEff1);
                System.out.println("CoEff2: " + CoEff2);
                System.out.println("Fahrenheit: " + Fahrenheit);
                System.out.println("Radius: " + Radius);

                // This closes the designated file and disconnects it from the stream.
                ReadFile.close();
            } 
        catch (IOException e)

            {
                System.out.println("Cannot Read File: " + InputFile);
                System.exit(0);
            }
    }

    // This method takes the values found in the ReadFile method and conducts mathematical operations on them.
    private void computeMath()
    {
        // This calls the 
        Hypotenuse = SimpleMath.computeHypotenuse(Adjacent,Opposite);
        Root = SimpleMath.solveQuadratic(CoEff0, CoEff1, CoEff2);       
        Celsius = SimpleMath.convertTemperature(eScale.FAHRENHEITCELSIUS, Fahrenheit);
        Area = SimpleMath.geometryCircle(eOperation.AREA, Radius);
    }


    /* This method takes all of the variables values determined throughout the program
     * and writes them to a file that is specified in the RunConfigurations window under
     * the Arguments tab for this class. 
     */
    private void writeFile(String OutputFile)
    {
        try
            {
                // This initiates the PrintWriter class which allows the program to write to a file.
                PrintWriter Output = new PrintWriter(new File(OutputFile));

                // These are the lines of text in which the program is writing in the designated file. 
                Output.println("Adjacent = " + Adjacent);
                Output.println("Opposite = " + Opposite);
                Output.println("Coefficent 0 = " + CoEff0);
                Output.println("Coefficent 1 = " + CoEff1);
                Output.println("Coefficent 2 = " + CoEff2);
                Output.println("Fahrenheit = " + Fahrenheit);
                Output.println("Radius Of Circle = " + Radius);

                // This closes the designated file and disconnects it from the stream.
                Output.close();

            }   
        catch(IOException e)



            {
                    System.out.println("Cannot Write File: " + OutputFile);
                    System.exit(0);
                }
        }

}

So any help would be nice, even speculations into why this code is behaving the way that it is. Thanks

解决方案

Simply add the class name in from of the enums as below. As they re declared public, you should be fine.

Celsius = SimpleMath.convertTemperature(
                                  SimpleMath.eScale.FAHRENHEITCELSIUS, Fahrenheit);
Area = SimpleMath.geometryCircle(SimpleMath.eOperation.AREA, Radius);

这篇关于从同一个java项目中的另一个类调用枚举值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆