This is what you described would look like in Java. You will have to create a wrapper class which represents your degrees. Internally however, we will just be using an Integer
object (or int
primitive would work fine).
public class Degree {
private Integer degrees;
// Constructor, takes an Integer object or int primitive
public Degree(Integer deg){
setDegrees(deg);
}
// Get degrees from your wrapper object
public Integer getDegrees(){
return degrees;
}
// Set degress of your wrapper object
public void setDegrees(Integer deg){
if(deg<361 && deg>-1){ degrees = deg; }
else if(deg>360){ degrees = 360; }
else if(deg<0){ degrees = 0; }
}
}
To use this in code somewhere else you would just use;
Degree d = new Degree(31);
You will probably want to override the toString();
method as well as the equals();
method, for ease of using your new Degree
class. I hope this helps.
Edit: The behaviour where you declare a class and use the =
operator to declare it a primitive is called autoboxing. A behaviour that has been possible since Java 5, but only works with a pre-set list of wrapper classes defined in the Java 5 specification.
That list includes Boolean
, Byte
, Short
, Character
, Integer
, Long
, Float
, Double
.