问题
I'm Having problems implementing some operations converting fractional to degrees in minutes and seconds.
Here's the UML Class I am working with:
Class:
Angle
Fields:
-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
Methods:
+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
And here's what I have done so far. The portions commented out are what I am having issue with. Mostly my issues surround the add and subtract. I'm not sure if I am using the UML properly or what else I am missing.
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;
}
}
EDIT: Here's a picture of the UML Class:
回答1:
actually is should be straight forward, first method is just quick combination to set all properties at once:
public void setAngle(int degrees, int minutes, double seconds)
{
setDegrees(degrees);
setMinutes(minutes);
setSeconds(seconds);
}
next method requires conversion from fractional values to integer ones, like this (math could have problems, but approach is correct):
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);
}
来源:https://stackoverflow.com/questions/32532422/how-to-implement-operations-from-signatures