basic Java question .. (I think)

adleeds

Newcomer
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);
}
 
The sin() method is returning a new Angle object that contains the result of your sin operation. What you see when you do the System.out.println( a45.sin()) is just a reference to the resulting Angle object.

Simple fix:

add a toString() method to your Angle class

public String toString()
{
return "" + radians;
}

Simplistic explanation: By default, when you do the System.out.println(..), Java looks for a toString() method in your Angle class in order to get a text representation of your object. If it doesn't find one, it outputs something like "Angle@1a125f0", which you might think of as "this is an Angle object and it is located at memory location 1a125f0".
 
the toString method is obviously the preferred way to do it, and it's a really fundamental part of Java that you need to learn. however, if you want a simple hack and you have a getter method for angle (let's say, getAngle) that returns a double:

Angle a45 = new Angle (45, Angle.DEGREES );
System.out.println(a45.sin().getAngle());

but yes, writing your own toString method is the best choice.
 
Well -- that sure works ... THANKS !!..

If someone could just explain to me how to call the "plus" method, I'd be eternally grateful. For example, I want to use it to add the angles 20 degrees and 30 degrees. I actually found a way to do it using the "plus" method I was given, but it returns (prints) the results in radians. I understand why it is doing radians, but I'm having trouble trying to figure out how to combine the "getDegrees method with the results of this "plus" method to convert the results to Degrees.

The methods I am using follow, as well as my feable attempt at using this:

//=====================================================
// find the sum of a 20 degree angle and a 30 degree angle
//=====================================================
Angle a20 = new Angle(20, Angle.DEGREES);
Angle a30 = new Angle(30, Angle.DEGREES);
System.out.println(a20.plus(a30));

//======================================================
// class methods

public class Angle
{
final static public int RADIANS=0;
final static public int DEGREES=1;
private double radians = 0.0;


//=======================================
// add two angles together
//=======================================

public Angle plus(Angle other)
{
return new Angle(radians + other.getRadians(), RADIANS);
}

//
// getDegrees
//

public double getDegrees()
{
return ((radians * 180) / 3.14159);
}


// constructor
public Angle(double angle, int units)
{
if (units==RADIANS)
radians=angle;
else
radians=(angle * 3.14159) / 180;
}
 
Three ways to do it. First, the easiest way: make a new object. Change your main method to:

Angle a20 = new Angle(20, Angle.DEGREES);
Angle a30 = new Angle(30, Angle.DEGREES);
Angle result = a20.plus(a30));
System.out.println(result.getRadians());

Second, create a new object but change one of the references you previously used. You just add

a30 = a30.plus(a20);

and then a30 would have the total angle. note that it doesn't have to be like that: you could have the following lines just as easily:

Angle a20 = new Angle(20, Angle.DEGREES);
Angle a30 = new Angle(30, Angle.DEGREES);
Angle a40 = new Angle(40, Angle.DEGREES);
Angle a25 = new Angle(25, Angle.DEGREES);
a30 = a40.plus(a25);
System.out.println(a30.getRadians());

you can do that because you create a new object in the return of plus.

third method: do it all in the System.out.println argument, which is crappy code, but hey, it's your choice.

System.out.println(a20.plus(a30).getDegrees());

not very readable, makes no difference in terms of amount of memory used or anything (that I can tell; I don't know if it would make a difference for garbage collection), so it's probably not a good idea. I'd say one (the first time you compute a result) and two (every other time).
 
And just for reference, check out the Java API documentation...that should answer most if not all your questions.
 
Back
Top