Objects and Methods in Java
Previous
|< Classes and Objects |
Next
Inheritance of classes >| |
The method is a set of commands, a part of a program that represents itself as a separate entity and performs a certain task, usually elementary.
We distinguish:
We distinguish:
- Defining the method
- Using the method
Using methods in java.
We can use:
- a method already defined in an existing class
- the method we create (define)
Using the existing Methods
For example:
System.out.println("A("+A.x+","+A.y+")");
System.out.println("A("+A.x+","+A.y+")");
The println method for the object out here is called a space where the text will be printed. This is the standard output.
After the method name is always bracketed "()"
They serve to send the method, if necessary, one or more data, the parameter of the method.
In this example, the method is sent one parameter that represents the text to be printed.
-
This text is obtained by linking a number of smaller texts into which the values of the variables are inserted.
After the method name is always bracketed "()"
They serve to send the method, if necessary, one or more data, the parameter of the method.
In this example, the method is sent one parameter that represents the text to be printed.
-
This text is obtained by linking a number of smaller texts into which the values of the variables are inserted.
Defining Methods in JAVA.
The method consists of a headline and body of the method in which the program commands are executed in that method:
Example defining Methods in java.
- In the previous example, we make a method that calculates the distance between the points
- The formula for calculating the distance between points 1 and 2, whose coordinates are x1, y1 and x2, y2
- d = Math.sqrt (Math.pow (x2-x1, 2) + Math.pow (y1-y2, 2))
Two methods from the Math class were used here:
sqrt - which calculates a square root where the subtracted size is sent as a parameter
pow - method that calculates the degree, the basis and the exhibitor are sent as parameters
sqrt - which calculates a square root where the subtracted size is sent as a parameter
pow - method that calculates the degree, the basis and the exhibitor are sent as parameters
- Commands that make up a method are written in the body of the method, between brackets "{}"
- There is also a method header
Example: Defining methods for computing the distance between two points.
We see that the method is added to the main class because it does not apply to any point individually but to a set of points
This method calculates the distance and then calculates the value using the return command.
This method calculates the distance and then calculates the value using the return command.
Zaglavlje metode.
- public- modifikator privatnosti. Kad je public onda se metoda može pozvati iz druge klase
- double na drugom mestu. Ovo je tip povratne vrednosti. Vrednost koja se vraća iz metode je double rastojanje je naziv metode
Non-returning methods.
I say a void in another place. This is the type of return value. If there is a void at this point then the method does not return anything and no command return.
Methods that print data
Calling methods in Java
Here we see that the method of distance is underlined red (error). This is because the method should belong to the object and call over the object of the main class, and this object is not created. There is only a class and the method must then be bound to the class itself. This is achieved by adding the word static |
Constructors of the class
- These are the special methods that are called at the time of creation of the object. They have no return value and they do not write the word void elsewhere in the header. They serve to give the initial values to the properties of the object.
- They can be without parameters
- They are called the same as the class they are in
Let's add it to the Point class
Constructors with parameters
They are used when we know the object information in advance. Since we know here in advance, the coordinates of the points will be replaced with empty calls, with calls from the constructors with parameters.
Finishing the dots app
Tasks for exercise
1.Car's uniform motion
The car moves evenly towards the place b. Create an object that represents the car, if the initial speed is 4 m / s and the starting position is 5 m from the starting point.
Show speed and position in the starting position.
Calculate the current position after 3 seconds and show the current data again.
In order to give in, the initial speed was 10 m / s.
Show speed and position in the starting position.
Calculate the current position after 3 seconds and show the current data again.
In order to give in, the initial speed was 10 m / s.
2.Uniformly accelerated motion car
The car moves equally accelerated by acceleration a (enters the user) to place b. Create an object that represents a car, if its starting speed is 4 m / s and the starting position is 5 m from the starting point.
Show speed and position in the starting position
calculate the current position after 3 s and show the current data again. In order to be informed, the initial speed was 10m / s
Show speed and position in the starting position
calculate the current position after 3 s and show the current data again. In order to be informed, the initial speed was 10m / s
Previous
|< Classes and Objects |
Next
Inheritance of classes >| |