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();
}
}```