Write a main method and a call method for the following JAVA program that has an
ID: 3631137 • Letter: W
Question
Write a main method and a call method for the following JAVA program that has an output of :/** Return the max - print largest number between two numbers */
// Define a Method and Call a Method
public class Test {
/** Main method */
public static void main(String[] args) {
int k = 0;
max (2, 9, k);
}
/** Return the max between two numbers */
public static void max(int value1, int value2, int k){
if (value1 > value2) // if the condition is true then it continues to next statement and else
k = value1;
else
k = value2;
System.out.println();
}
}
/* A retrun statemnt is not needed for a void method,
but it can be used to terminiating the method and retruning to the method caller
*/
Explanation / Answer
Answer 1: (no return) public class Test { public static void main(String[] args) { int k = 0; int val1 = 2; int val2 = 9; max(val1, val2, k); } public static void max(int value1, int value2, int k) { if (value1 > value2) k = value1; else k = value2; System.out.println(k); } } Answer 2: (Uses Return Statement) public class Test { public static int k, val1, val2; public static void main(String[] args) { k = 0; val1 = 2; val2 = 9; System.out.println(max()); } public static int max() { if (val1 > val2) k = val1; else k = val2; return k; } }
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.