The concepts of functions and methods
A function is a part of a program that has its own name.
This name can be used in the program as a command (this command is called a function call).
When a function is called, the commands of which it consists are executed. A function call can return a value (similar to an operation) and therefore can be used in an expression along with operations.
A method is a function that is part of a class that can perform operations on data of this class. In the Java language, the entire program consists only of classes and functions can be described only inside them. That is why all functions in the Java language are methods.
Concept of method
Methods are always defined inside classes:
minOfTwoNumbers is the method we defined in the Main class, let's consider it.
public - access type (the method can be called from another class). There are other types of access, for example private (the method is available only inside the class) and protected.
static - means that the method is static, it belongs to the class Main, and not to a specific instance of the class Main. We can call this method from another class like this: Main.minOfTwoNumbers ().
Parsing code
public class
public static
}
public static void main(String[] args) {
// Your software code is here
}
}
Parsing code
In lines 3 through 11, the minOfTwoNumbers method is described.
On lines 5-9 performs a comparison of the values of a and b
On line 13, the method is called to determine the minimum of two numbers (4 and -6).
Task
Create a method that imposes a maximum of two numbers.
System.out.println("max = "+maxOfTwoNumbers(0, -99));
2. Create a method to calculate the 3rd power of the number.
System.out.println("7*7*7 = "+Cube(7));
Homework
Create a method that defines even and odd numbers.
Create a method that determines the area and circumference of the circle.
Материалы на данной страницы взяты из открытых источников либо размещены пользователем в соответствии с договором-офертой сайта. Вы можете сообщить о нарушении.