Question

I apologize for the badly worded title, but i cannot find a better way to describe my question.

I'm making a custom Degree class that will only hold any number between 0 and 360, any additions higher will be resolved down to their 0-360 equivalent. I was wondering if it was possible to do what primitive types do I.e Int a = 1.

I doubt this is possible, but if it is, how can i do it?

Was it helpful?

Solution

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.

OTHER TIPS

I decided i would post my finished class here in case anyone wanted it for reference.

public class Degree extends Number implements Comparable<Degree>{

    float degreeFloat;

    public Degree(){
        degreeFloat = 0;
    }

    public Degree(float degrees){
        this.set(degrees);
    }

    public Degree(int degrees){
        this.set(degrees);
    }

    public Degree(Degree degrees){
        this.set(degrees);
    }

    @Override
    public int intValue() {
        return (int)degreeFloat;
    }

    @Override
    public long longValue() {
        return (long)degreeFloat;
    }

    @Override
    public float floatValue() {
        return degreeFloat;
    }

    @Override
    public double doubleValue() {
        return (double)degreeFloat;
    }

    @Override
    public int compareTo(Degree compTo) {
        if(compTo.get() > degreeFloat)
            return 1;
        else if(compTo.get() < degreeFloat)
            return -1;
        else
            return 0;
    }

    public Boolean equals(Degree compTo){
        if(degreeFloat == compTo.get()) return true;
        else return false;
    }

    public float get(){
        return degreeFloat;
    }

    public void set(Degree setTo){
        this.degreeFloat = setTo.get();
    }

    public void set(float setTo){
        this.degreeFloat = setNormalise(setTo);
    }

    public void set(int setTo){
        this.degreeFloat = setNormalise((float)setTo);
    }

    public void set(double setTo){
        this.degreeFloat = setNormalise((float)setTo);
    }

    public float setNormalise(float setTo){
        float value = setTo;

        while(setTo > 360f)
            setTo -= 360F;

        while(setTo < 0f)
            setTo += 360f;

        return value;
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top