Atlas v2
Atlas v2

Reputation: 33

Java class without constructor, only static functions

I have created the class angle as shown in the codebox below, I want to calculate the difference( called "minus" in the code) of two angles with the following command.

Angle.degrees(135).minus(Angle.degrees(90)).getDegrees()

Unfortunately, I always get zero as result, because the intern values are always overwritten.

import java.lang.Math;

public class Angle {

    private static double gradmass = 0;
    private static double bogenmass = 0;

    public static Angle degrees(double angle) {
        Angle angleD = new Angle();

       //  gradmass = angle;
       //  bogenmass = Math.toRadians(angle);

        angleD.setDegrees(angle);
        angleD.setRadians(Math.toRadians(angle));
        return angleD;
    }

    public static Angle radians(double angle) {
        Angle angleR = new Angle();

        // gradmass = Math.toDegrees(angle);
        // bogenmass = angle;

        angleR.setDegrees(Math.toDegrees(angle));
        angleR.setRadians(angle);
        return angleR;
    }

    public double getDegrees() {
        return gradmass;
    }

    public void setDegrees(double gradM) {
        gradmass = gradM;

    }

    public double getRadians() {
        return bogenmass;
    }

    public void setRadians(double bogenM) {
        bogenmass = bogenM;
    }

    public Angle plus(Angle other) {
        Angle temp = new Angle();

        temp.setDegrees(this.getDegrees() + other.getDegrees());
        temp.setRadians(other.getRadians() + other.getRadians());

        return temp;
    }

    public Angle minus(Angle other) {
        Angle temp = new Angle();;

        temp.setDegrees(this.getDegrees() - other.getDegrees());
        temp.setRadians(this.getRadians() - other.getRadians());

        return temp;
    }

    public Angle neg() {
        Angle temp = new Angle();
        temp.setDegrees(-this.getDegrees());
        temp.setRadians(-this.getRadians());
        return temp;
    }

    public double sin() {
        double temp;
        temp = Math.sin(this.getDegrees());
        return temp;
    }

    public double cos() {
        double temp;
        temp = Math.cos(this.getDegrees());
        return temp;
    }

    public boolean similarTo(Angle other){
        boolean gleich = false;
        if( 0 == (this.getDegrees() - other.getDegrees()) || this.neg().getDegrees() == other.getDegrees()){
            gleich = true;
        }
        return gleich;

    }

    public String toString(){
        return
                "GradM " + this.getDegrees() + " BogenM " + this.getRadians();
    }
}

I did not make a constructor on purpose! I'm looking for a solution without a constructor or nonstatic methods.

Upvotes: 1

Views: 165

Answers (2)

Turja
Turja

Reputation: 76

Don't do this. Your class won't be thread-safe this way. What you want is kind of something like BigDecimal class in java, I guess. You can check the documentation of BigDecimal class of java if you want something like this. https://docs.oracle.com/javase/8/docs/api/java/math/BigDecimal.html

But if you want the exact same thing you asked, you can see this (not thread safe and not recommended as well).

class Angle {
    static Double angle1 = null;
    static Double angle2 = null;
    private static void setAngle(double angle) {
        if (angle1 == null) angle1 = angle;
        else angle2 = angle; 
    }
    static Angle degrees(Double angle) {
        setAngle(angle);
        return new Angle();
    }
    static Double getDegrees() {
        return angle1;
    }
    static Angle minus(Angle angle) {
        angle1 -= angle2;
        return new Angle();
    }
}

public class SolutionAngle {
    public static void main(String... args) {
        System.out.println(Angle.degrees(135).minus(Angle.degrees(90)).getDegrees());
    }
}

Upvotes: 0

Mureinik
Mureinik

Reputation: 311163

Both your data members are static, meaning there's a single instance of them for the entire class. You should declare them as instance members so that each instance of Angle can have its own values:

public class Angle {

    private double gradmass = 0;
    private double bogenmass = 0;
   
    // rest of the code...

Upvotes: 4

Related Questions