Java. Может ли одна функция выполнять две задачи? [закрыто]JAVA

Программисты JAVA общаются здесь
Ответить
Гость
 Java. Может ли одна функция выполнять две задачи? [закрыто]

Сообщение Гость »


My course instructor told us in a previous lesson not to use one function for two tasks. A few lessons later, the code we are reviewing has functions in it that perform two tasks. I'm brand new to Stack Overflow so I'm hoping this is the right place to ask
Maybe a function can handle two tasks but it isn't a good practice to do so?
Thanks in advance for any advice/help
Obviously I can create another function to handle printing out the output but I didn't know what was proper.
Here's the code

Код: Выделить всё

public static void main(String[] args) {

calculateArea(length, width);
calculatePerimeter(length, width);

}

public static void calculateArea(double param1, double param2) {
// Local variable to store the area (accessible only within this function)
double area = param1 * param2;
System.out.println("Area of the rectangle: " + area);
}

public static void calculatePerimeter(double param1, double param2) {
// Local variable to store the perimeter (accessible only within this function)
double perimeter = 2 * (param1 + param2);
System.out.println("Perimeter of the rectangle: " + perimeter);
}
This is the very last lesson. A few lessons ago, he said this code is not proper

Код: Выделить всё

public class ReturnValuesNew {

public static void main(String[] args) {
calculateArea(2.3, 3.6);
calculateArea(1.6, 2.4);
calculateArea(2.6, 4.2);
}

public static void calculateArea(double length, double width) {
double area = length * width;
System.out.println("Area: " + area);
}
}


Источник: https://stackoverflow.com/questions/781 ... -two-tasks
Ответить

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

Вернуться в «JAVA»