A 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 friendVasya
with 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, friendVasya
it 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 friendVasya
and 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
main
and activating the methodSystem.out.println
// Wanted a console, met a friend; - passed an argument
int money = 500
// Transferred money to a friend; - launching the method
friendVasya
and processing the argumentint money
received from the methodmain
// Vasya goes to the store and tries to buy a set-top box with the money given; - returning
String purchase
to the methodmain
as an argument passedreturn
to the methodprintln
// Vasya reports whether he succeeded in doing this or not.
GO TO FULL VERSION