I'm trying to learn Java, and I have a simple piece of code that I just don't understand. I think the basic piece of the puzzle I'm missing is what "return new ...." does.
This is a simple driver and class that's supposed to store an angle as radians (passed as degrees or radians), then allow you to do stuff like get the sin, cos, add angles, etc. Here's a simple piece that I just don't understand. HOW DO I USE THIS "sin" method to actually get the sin of an angle in a format that I can print out ?
I understand the first part (how to use Angle to make a new Object which saves the angle in radians in the variable called "radians").
I don't understand how to call the "sin" method. I tried the following:
Angle a45 = new Angle (45, Angle.DEGREES );
System.out.println(a45.sin());
but instead of the sin of 45 degrees, it printed out : Angle@1a125f0
If someone could explain how to use this, I think I'll be on my way... The other piece (if you don't mind) is the method to ADD two angles... I have no idea how to use this ... (see below) ...
Thanks !!!
public class Angle
{
final static public int RADIANS=0;
final static public int DEGREES=1;
private double radians = 0.0;
public Angle(double angle, int units)
{
if (units==RADIANS)
radians=angle;
else
radians=(angle * 3.14159) / 180;
}
public Angle sin()
{
return new Angle(Math.sin(radians), RADIANS);
}
//=======================================
// add two angles together
//=======================================
public Angle plus(Angle other)
{
return new Angle(radians + other.getRadians(), RADIANS);
}
This is a simple driver and class that's supposed to store an angle as radians (passed as degrees or radians), then allow you to do stuff like get the sin, cos, add angles, etc. Here's a simple piece that I just don't understand. HOW DO I USE THIS "sin" method to actually get the sin of an angle in a format that I can print out ?
I understand the first part (how to use Angle to make a new Object which saves the angle in radians in the variable called "radians").
I don't understand how to call the "sin" method. I tried the following:
Angle a45 = new Angle (45, Angle.DEGREES );
System.out.println(a45.sin());
but instead of the sin of 45 degrees, it printed out : Angle@1a125f0
If someone could explain how to use this, I think I'll be on my way... The other piece (if you don't mind) is the method to ADD two angles... I have no idea how to use this ... (see below) ...
Thanks !!!
public class Angle
{
final static public int RADIANS=0;
final static public int DEGREES=1;
private double radians = 0.0;
public Angle(double angle, int units)
{
if (units==RADIANS)
radians=angle;
else
radians=(angle * 3.14159) / 180;
}
public Angle sin()
{
return new Angle(Math.sin(radians), RADIANS);
}
//=======================================
// add two angles together
//=======================================
public Angle plus(Angle other)
{
return new Angle(radians + other.getRadians(), RADIANS);
}