# Thread: Trying to understand Objects

1. Member
Join Date
May 2012
Posts
12
Rep Power
0

## Trying to understand Objects

I'm trying to understand the logic of objects, I hope you can help me.

At uni, they are teaching us objects using getters and setters:

Java Code:
```/**
*/
public class Triangulo
{
private double base;
private double altura;

public double getBase()
{
return base;
}

public void setBase(double x)
{
base = x;
}

public double getAltura()
{
return altura;
}

public void setAltura(double x)
{
altura = x;
}

public double superficie()
{
double s;
s = base * altura / 2;
return s;
}

}```
my question is:

1. What's the use of these sets and gets?

2. why within the setters a value x is assigned?

public void setBase(double x)
{
base = x;
}

3. what does return mean?

public double getAltura()
{
return altura;
}

I hope you can help this 100% newbie :P

2. ## Re: Trying to understand Objects

I don't think I can explain it better than the Tutorial does in the Lesson: Classes and Objects (The Java™ Tutorials > Learning the Java Language) so I won't try :)

db

3. Member
Join Date
Mar 2011
Posts
12
Rep Power
0

## Re: Trying to understand Objects

Setters and getters do exactly what they sound like, they set things and get things, such as variables, values, etc., so in the "setBase" method, you are SETTING the variable "base", which is type double, equal to "x", which is also of type double. As far as the "x" variables, those are the names given to the parameters that are passed into the method. So lets say you make a method call to set a base, we'll call it like so, Triangulo.setBase(.53), passing the doouble value of .53 because the method requires a double to perform is function. So when your program jumps into the "setBase" method, .53 will get assigned to the double variable "x". Then the function of the method sets the base to x, which is .53. So upon completion of this method call, the base of the designated Triangulo object will be .53. And the keyword return basically means the product of the method. So the product of the method "getAltura" is a double, we know this from the methods signature (public DOUBLE getAltura). So as you can see this methods returns "altura", which is also of type double. So the product of this methods is a double variable that holds the same exact value as "altura". Also, when the keyword "return" is applied, the program automatically jumps out of the methods. So "getAltura" returns "altura" get immediatly excapes from the method.

Hope this helps. If anything is unclear just feel free to ask more questions!

Much Love syle_

4. Member
Join Date
May 2012
Posts
12
Rep Power
0

## Re: Trying to understand Objects

Originally Posted by syle_q
Setters and getters do exactly what they sound like, they set things and get things, such as variables, values, etc., so in the "setBase" method, you are SETTING the variable "base", which is type double, equal to "x", which is also of type double. As far as the "x" variables, those are the names given to the parameters that are passed into the method. So lets say you make a method call to set a base, we'll call it like so, Triangulo.setBase(.53), passing the doouble value of .53 because the method requires a double to perform is function. So when your program jumps into the "setBase" method, .53 will get assigned to the double variable "x". Then the function of the method sets the base to x, which is .53. So upon completion of this method call, the base of the designated Triangulo object will be .53. And the keyword return basically means the product of the method. So the product of the method "getAltura" is a double, we know this from the methods signature (public DOUBLE getAltura). So as you can see this methods returns "altura", which is also of type double. So the product of this methods is a double variable that holds the same exact value as "altura". Also, when the keyword "return" is applied, the program automatically jumps out of the methods. So "getAltura" returns "altura" get immediatly excapes from the method.

Hope this helps. If anything is unclear just feel free to ask more questions!

Much Love syle_
the kind of answer I was expecting, thanks a lot!!!

5. Moderator
Join Date
Feb 2009
Location
New Zealand
Posts
4,717
Rep Power
16

## Re: Trying to understand Objects

I am probably saying something similar but (for better or worse...) this is how I think about it.

I go to the Triangulo shop and buy a dozen of the things, once I have them home and have cleared away all the packaging material I set them up on the table to see what they are and what they will do. I ignore the shiny white finish and rounded corners: the important things are two slots, labelled "base" and "altura" and three buttons labelled "base", "altura" and "superficie" next to small cavity like a drink vending machine. I'm hoping the Triangulos will produce something tasty.

It turns out, not. The manuals have gone with all the rest of the packaging, but the operation is pretty straight forward: I can feed in numbers into the slots corresponding to the base and height of any small triangles I may have about the house. After the paper disappears into the machine it clicks in a solid and reassuring way. And I can also push the buttons in which case after some whirring a piece of paper emerges from the machine. If I push the "base" or "altura" buttons it is not very exciting as the machine seems only to repeat the numbers I fed in. But the "superficie" button is rather neat: the number on the paper that emerges is the area of the triangle.

-----

There are various things here which seem to be significant:

The machines each do 5 things: two slots (aka setters) and three buttons (getters?) as described. That is their behaviour.

Two of the buttons merely return what I fed in, perhaps these should be considered getters, while the "superficie" button is something else? In truth, I have no idea what is going on inside the machine. The behaviour (as described) is everything - perhaps there is actually some storage area that is "set" by the slots and "got" by the buttons as suggested by the solid click sound. Or perhaps not. The logical distinction between slots and buttons is clear enough: I can choose what to send the machine via its slots, but I must accept what I get after pushing the buttons. But the question of whether a button actually gets (returns) something previously set cannot be decided without pulling the machine apart.

(If I were the engineer who designed the Tringulo machines I would probably be very interested in what the internal machanism (the state) actually is. And I might describe two of the buttons with "get" to signify their close relationship with the machine's base and altura variables. But as the machine's user I don't care and can't find out about any of that: I regard the three buttons as logically similar. This "can't find out" property of the internal machanics and the fact that the user has only the class's behaviour to work with is often called encapsulation.)

It is the individual machines that behave in this way, not the Triangulo class as a whole. The class describes what is common to the behaviour - it captures the general formula 0.5(b*h) - but I am free to feed in and get out different numbers from different instances.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•