如何从签名实现操作

时间:2015-09-11 21:32:52

标签: java uml

我在实现某些操作时遇到问题,这些操作在几分之一秒内将小数转换为度数。

这是我正在使用的UML课程:

类别:

Angle

字段:

-MINUTES_PER_DEGREE:final int = 60
-SECONDS_PER_MINUTE:final int = 60
-SECONDS_PER_DEGREE:final int = 3600
-DEGREE_SYMBOL:final char = '\u00B0'
-degrees: int
-minutes: int
-seconds: double

方法:

+add(left: Angle, right: Angle): Angle
+subtract(left: Angle, right: Angle): Angle
+Angle()
+Angle(fractionalDegrees:double)
+Angle(degrees: int, minutes: int, seconds: double)
+getDegrees(): int
+getMinutes(): int
+getSeconds(): double
+getAngleInFractionalDegrees(): double
+setDegrees(degrees: int): void
+setMinutes(minutes: int): void
+setSeconds(seconds: double): void
+setAngle(degrees: int, minutes: int, seconds: double): void
+setAngleToFractionalDegrees(fractionalDegrees: double): void
+addAngle(angle: Angle): void
+addDegrees(degrees: int): void
+addMinutes(minutes: int): void
+addSeconds(seconds: double): void
+subtractAngle(angle: Angle): void
+subtractDegrees(degrees: int): void
+subtractMinutes(minutes: int): void
+subtractSeconds(seconds: double): void
+toString(): String

这就是我到目前为止所做的事情。注释掉的部分是我遇到的问题。主要是我的问题围绕加减。我不确定我是否正确使用UML或者我还缺少什么。

class Angle
{

    private static final int MINUTES_PER_DEGREE = 60;
    private static final int SECONDS_PER_MINUTE = 60;
    private static final int SECONDS_PER_DEGREE = 3600;
    private static final char DEGREE_SYMBOL = '\u00B0';

    private int degrees;
    private int minutes;
    private double seconds;

  //  public static Angle add(Angle left, Angle right)
  //  {
  //      Angle add = left.getAngle + right.getAngle;
  //      return add;
  //  }
  //  
  //  public static Angle subtract(Angle left, Angle right)
  //  {
  //      
  //      Angle subtract = left.getAngle - right.getAngle;
  //      return subtract;
  //  }

    public Angle()
    {

    }

    public Angle(double FractionalDegrees)
    {

    }

    public Angle(int degrees, int minutes, double seconds)
    {

    }

    public int getDegrees()
    {
        return this.degrees;
    }

    public int getMinutes()
    {
        return this.minutes;
    }

    public double getSeconds()
    {
        return this.seconds;
    }

    public double getAngleInFractionalDegrees()
    {
 //       return this.Angle;
    }

    public void setDegrees(int degrees)
    {
        this.degrees = degrees;
    }

    public void setMinutes(int minutes)
    {
        this.minutes = minutes;
    }

    public void setSeconds(double seconds)
    {
        this.seconds = seconds;
    }

    public void setAngle(int degrees, int minutes, double seconds)
    {
        // Not sure what to put here
    }

    public void setAngleToFractionalDegrees(double fractionalDegrees)
    {

        // This doesn't work because fractionalDegrees undefined

//        this.fractionalDegrees = fractionalDegrees;
    }

}

编辑:这是UML类的a picture

UML Diagram

1 个答案:

答案 0 :(得分:2)

实际上应该是直截了当的,第一种方法只是快速组合,一次性设置所有属性:

public void setAngle(int degrees, int minutes, double seconds)
{
    setDegrees(degrees);
    setMinutes(minutes);
    setSeconds(seconds);
}

下一个方法需要从小数值转换为整数值,这样(数学可能有问题,但方法是正确的):

public void setAngleToFractionalDegrees(double fractionalDegrees)
{
    final int degrees = (int) Math.floor(fractionalDegrees);
    final int minutes = (int) Math.floor((fractionalDegrees - degrees) * 60.0);
    final double seconds = (fractionalDegrees - degrees - (minutes / 60.0)) * 3600.0;
    setAngle(degrees, minutes, seconds);
}