JavaRush /Java Blog /Random EN /Explanation of method interaction (for beginners)
Alex Ter
Level 14
Великий Новгород

Explanation of method interaction (for beginners)

Published in the Random EN group
Explanation of the interaction of methods (for beginners) - 1A couple of times, I have already come across moments when students do not understand the logic of interaction between methods. In one of the questions for the tasks, the following code was given:

public class Solution {
    public static void main(String[] args) {
        System.out.println(convertCelsiusToFahrenheit(41));
    }
    public static double convertCelsiusToFahrenheit(int celsius) {
        double TC = 41;        
        double TF = 9 / 5.0 * TC + 32;
        return TF;          
    }
}
In fact, following this code, no matter what value we pass to the method convertCelsiusToFahrenheit, we will always get the same output. How should the interaction take place? Let's imagine that there is you. You have some money and want to buy a console, knowing the price. But you are so passionate about learning Java that you don’t have time to buy a console.

public static void main(String[] args) {
But you have a friend who has time and loves to wander around the shops.

public static String friendVasya(int money) {
int price = 300;
In this case, I created a method friendVasyawith a return type String, instead you could set any other data type, it all depends on what you ultimately want to get. When the method runs, friendVasyait will receive the amount of money that you specify in the argument ( int money). Vasya’s friend may have a lot of his own affairs or needs, we would describe all this in the method friendVasya, but we are only interested in whether he will buy us a console. So let's add this feature to him.

public static String friendVasya(int money){
int cash = money - price;
String purchase;
And accordingly, there may be enough money, or there may not be enough. Let's implement different responses for these situations.

public static String friendVasya(int money){
int price = 300;
int cash = money - price;
String purchase;
if (cash>=0){
purchase = "Купил";}
else{
purchase = "Этого мало. Где деньги, Лебовски?";}
return String.format(purchase);}
As a result of calling the method friendVasyaand passing money to it in the form of the money argument, we will be able to buy a console or find out that there is not enough money for the purchase. All that remains is to add a call to this method in the program code, indicating how much money you are willing to give for the console /*in this case it will be 500 */:

public static void main(String[] args) {
System.out.println(friendVasya(500));
Full code:

public class Game {
public static void main(String[] args) {
System.out.println(friendVasya(500));
}
public static String friendVasya(int money){
int price = 300;
int cash = money - price;
String purchase;
if (cash>=0){
purchase = "Купил";}
else{
purchase = "Этого мало. Где деньги, Лебовски?";}
return String.format(purchase);
}
}
In fact, the essence of all this code is:
  • Launching mainand activating the method System.out.println// Wanted a console, met a friend;
  • passed an argument int money = 500// Transferred money to a friend;
  • launching the method friendVasyaand processing the argument int moneyreceived from the method main// Vasya goes to the store and tries to buy a set-top box with the money given;
  • returning String purchaseto the method mainas an argument passed returnto the method println// Vasya reports whether he succeeded in doing this or not.
It seems like I wanted to explain it more simply, but along the way I made it even more complicated) PS First article. I haven't crawled far yet (level 3). So, awaiting criticism on the merits)
Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION