How does the established int A and B get attributed to the prewritten code with the numbers in the second method?
You must be signed in to leave a comment
22 June 2021, 12:46
int m2; if (a < b) m2 = a; else m2 = b; return m2; }
16 April 2021, 01:35
Methods are the way different aspects of code communicate with each other. The main purpose is class to class definition, but that complicates a simple construct a bit much when you first are learning it. So: A method is a callable piece of code that you pass input to and either does something with the input, or return a value that typically is the result of doing something with that input. A method is defined with four required parts and one optional: optional static modifier required an access modifier - either public, private, protected, or it can be left blank, which is still a modifier, and is called default required return type - must be included. Use void if the method does not return anything requiredmethod name - follows Javas standard naming rules requiredmethod parameters - inside the parentheses, what is passed to the method from the calling codeaccess modifier - public static - included return type - int method name - min method parameters - (int, int) with variable names a and b To successfully call any method you must use its name and then pass it arguments (what you call the values in parentheses from the calling code) that match the methods parameters (just the parameter types). You can see that being done in the main method: ↑the arguments are (int, int) which match the match parameter of (int, int)
16 April 2021, 01:47
It is VERY important that you match the argument types exactly in order as the method parameter. If you had a method with parameters (String, int) and tried calling that method with the arguments (15, "Test String"), you would end up creating an error and the code will fail because the arguments of (int, String) do not match the method parameter of (String, int) As I said way above in the first post, methods are a way of communicating. When you call a method and pass it arguments, the code goes to the method and the variables in the method become the arguments passed to the method. The method's return type is the result of processing those methods with whatever logic you need (in this task just figuring which is the smaller of the two), which is then returned to the calling code location. Another VERY important bit of information is that if a method has a return type specified then ALL code paths in the method must reach a return statement that returns the correct type. If you had a return type of an int, and then coded it to returned a string then the code would fail to compile. If all the return statements are effectively behind logic gates where cases exist where a return statement might not be reached then the code fails to compile. For example:Here is some simple code of a method that checks if the number passed to it is even by returning a boolean (true or false). This is done by subtracting 2 from the number until you are left with either 1 or 0. If you end up with 0 then the number is even. The problem with this code is that if you end up with 1 then there are no additional return statements. This would cause the code to fail.
16 April 2021, 01:52
Simple recap: - methods are ways that programs communicate with other aspects and each other - this communication is done with arguments and return values - arguments are what are passed from calling code, they must match the method parameters - method parameters are part of the method declaration and define what arguments the method accepts - when a method has a return value then the logic in the method MUST reach a valid return statement