-2

The task is: Write an Angle class that has attributes specified in radians. The class should enable: creation of an angle (implies 0), creation of an angle based on a given number of degrees, minutes and seconds, operations and printing in radians and degrees.

(P.S. in addition to the questions I will ask now, I would be grateful if someone wants to do a comparison).

So the task is above, I did it in the following way, but I can't deal with the method for returning from radians to degree value, which stings my eyes when I see that I initialized with e.g. (0,20,30)(0 deg 20 min) , 30 sec) and I only get 0.

package Popravak;
import java.text.DecimalFormat;

public class Angle {

    
    
    private double angleInRad;
    
    
    
    
    public Angle() {
        this.angleInRad=0;
        
    }
    public Angle(double angleInRad) {
    this.angleInRad=angleInRad;
        
        
    }
    public Angle(int deg, int min, int sec) {
        double rad=Ugao.degToRad(deg, min, sec);
        this.angleInRad=rad;
    }   
    public Angle(final Angle u) {
        this(u.angleInRad);
    }


    public int deg() {
    return (int)(Math.toDegrees(angleInRad));
    
} 
    public int min() {
    double ste=Math.toDegrees(angleInRad)-deg();
        return (int)(ste*60);
        
}
    public int sec() {
        double ste=Math.toDegrees(angleInRad)-deg();
        return (int)(((ste*60)-min())*60);
}

public static double degToRad(int deg, int min, int sec) {
        double n,m;
         if(sec>=60) {
            m=(int)(sec/60);
            n=sec%60;
            sec=0;
            sec+=n;
            min+=m;
        }
        if(min>=60) {
            m=(int)(min/60);
            n=min%60;
            min=0;
            min+=n;
            
            deg+=(deg<0)? -m:m;
        } 
         if(Math.abs(deg)>=360) {// pazi
             
            n=deg%360;
            deg=0;
            deg+=((deg<0)? -n:n);}
        
        //--------------
        min+=sec/60;
        deg+=((deg<0)?   -min/60:min/60);
        
        
        return deg*(Math.PI/180);
        
        
        
    }
public  void reduce() {
    double adicija=this.angleInRad;
    if(adicija>=Math.PI*2) {
        double n=adicija%(Math.PI*2);
        adicija=0;
        adicija=n;}
    if(adicija<0) {
        double n=Math.abs(adicija);
        int m=(int)(n/(Math.PI*2));
        adicija+=(m+1)*(Math.PI*2);
        }
}   
public void moveAnglefinal Angle ad) {
    this.angleInRad=    (this.angleInRad+=ad.angleInRad);
        this.reduce();
        
    }
public void mnozi(int broj) {
    this.degInRad*=broj;
    
}   

@Override

public String toString() {
    return "Angle in:"+"\n"+"angle in rad is "+this.angleInRad+"\n"+"angle in deg is "+deg()+"."+min()+"."+sec();
    
} 


}```
  • Open the test class and try to initialize it with the Angle(deg, min, sec) constructor, and with deg=0, you get that the whole angle is 0 – Sergej Lav Bojanić 2022 Aug 30 '23 at 07:50
  • The cast to int will remove everything after the `.`. If you need to keep that, the method needs to return `double` instead. – Rob Spoor Aug 30 '23 at 07:54
  • Yes, I did that with the intention of throwing out the remainder that does not represent seconds, minutes or degrees, because the remainder is always the one that is of lower rank, for minutes I need the decimal remainder of degrees, and for seconds the decimal remainder of minutes, by multiplying by 60 and converting I get mathematically speaking the solution – Sergej Lav Bojanić 2022 Aug 30 '23 at 08:41
  • My opinion is that the problem is not in the conversion, give an example of how you would solve it without it. – Sergej Lav Bojanić 2022 Aug 30 '23 at 08:43

0 Answers0