I tried to explain the method

at first

――Thank you for opening this article. ――I'm not good at teaching, so I managed to put it together, so please take a look. ――Please write more and more if you have any requests or suggestions. ――Then, please keep in touch with us for a long time.

What is a method in the first place?

Show the average, highest and lowest scores for the 10-person test.

java=Question_11.java


package Question_04;

public class Question_11 {
	public static void main(String[] args) {
		int[] english = {98,80,78,85,65,86,90,94,70,92};
		int high = english[0];
		int low = english[0];
		int avg = 0;
		for(int i = 0;i < english.length;i++) {
			if(high <= english[i]) {
				high = english[i];
			}else if(low >= english[i]){
				low = english[i];
			}
			avg+=english[i];
		}
		avg/=english.length;
		System.out.println("Average score:"+avg+"point");
		System.out.println("highscore:"+high+"point");
		System.out.println("Lowest point:"+low+"point");
	}
}

――The above program has the following method structure of one main method. --In this program, all the actions of ** "variable declaration" **, ** "calculation part" **, ** "result output" ** are written in the main method.

  1. ** Variable declaration (main method) **: Stores test scores
  2. ** Calculation part **: Compare test scores
  3. ** Result output **: Display test score --One main method is fine, but if you write a large program, the number of lines will be several thousand, and management is ** very difficult **. --Therefore, ** "variable declaration" **, ** "calculation part" **, ** "result output" ** are separated, so the idea of method is used.

What are the criteria for dividing methods?

――Once you have a vague understanding of "whether to divide methods because it is difficult to increase the number of lines", I will explain how to divide them. ――First of all, the programs can be roughly divided as follows.

Variable declaration part

--In the variable declaration part, the value is stored in the variable to be used.

java=Question_11.java


int[] english = {98,80,78,85,65,86,90,94,70,92};//Store points for 10 people in english
int high = english[0];//Declare variables to compare highest points
int low = english[0];//Declare a variable to compare the lowest points
int avg = 0;//Declare a variable to calculate the average score

Calculation part

--In the calculation part, the points are compared and the highest, lowest, and average points are calculated.

java=Question_11.java


for(int i = 0;i < english.length;i++) {//Loop 10 times (for the number of people) to compare the scores for 10 people
    if(high <= english[i]) {//Select the highest point
        high = english[i];
    }else if(low >= english[i]){//Select the lowest point
        low = english[i];
    }
    avg+=english[i];//Store the total score to calculate the average score
}

Result output part

--In the result output part, the operation of "Please display" is performed as if it were a problem statement.

java=Question_11.java


System.out.println("Average score:"+avg+"point");
System.out.println("highscore:"+high+"point");
System.out.println("Lowest point:"+low+"point");

What kind of program will it actually be?

java=Question_11.java


package Question_05;

public class Test {
	public static void main(String[] args) {
		int[] english = {98,80,78,85,65,86,90,94,70,92};
		int highscore = english[0];
		int lowscore = english[0];
		int avgscore = 0;
		int highend = high(english,highscore);
		int lowend = low(english,lowscore);
		int avgend = avg(english,avgscore);
		display(highend,lowend,avgend);
	}
	public static int high(int[] english,int highscore) {
		for(int i = 0;i < english.length;i++) {
			if(highscore <= english[i]) {
				highscore = english[i];
			}
		}
		return highscore;
	}
	public static int low(int[] english,int lowscore) {
		for(int i = 0;i < english.length;i++) {
			if(lowscore >= english[i]){
				lowscore = english[i];
			}
		}
		return lowscore;
	}
	public static int avg(int[] english,int avgscore) {
		for(int i = 0;i < english.length;i++) {
			avgscore+=english[i];
		}
		avgscore/=english.length;
		return avgscore;
	}
	public static void display(int highend,int lowend,int avgend) {
		System.out.println("Average score:"+avgend+"point");
		System.out.println("highscore:"+highend+"point");
		System.out.println("Lowest point:"+lowend+"point");
	}

}

――It can be divided like this.

I understand that the methods can be separated, but what are the arguments and return values?

--Variable declarations made by separating methods can only be called within the method. --Example: Even if you write the code ```int x = 0; `in the main method, or use it like System.out.println (x);` in the high method, x The value of can not be displayed. --In other words, even if you declare a variable, you can use it only in the method. --So, call the value using the argument and the return value.

Reasons to use arguments and return values

--Variables such as english for storing points and high score for comparing points declared in the variable declaration (main method) are declared for use in the calculation part. --So, refer to these variables as arguments. Then, the calculation result is used as the return value and returned to the variable declaration. By doing this, the calculation result can be handled by the main method. --Similarly, the calculation result is required to output the output result. --So, as an argument, refer to the calculation result from the variable declaration returned as the return value earlier. --The output result can be output by these operations.

Finally

――Thank you for your relationship. ――If you have any questions or requests, please write more and more.

Recommended Posts

I tried to explain the method
I tried to explain Active Hash
I tried to summarize the methods used
I wanted to add @VisibleForTesting to the method
I was addicted to the roll method
I tried to implement the Iterator pattern
I tried to summarize the Stream API
I tried to understand how the rails method "redirect_to" is defined
I tried to understand how the rails method "link_to" is defined
[Java] I tried to make a maze by the digging method ♪
[Rails] I tried to raise the Rails version from 5.0 to 5.2
I tried to organize the session in Rails
I tried to set tomcat to run the Servlet.
I tried the Docker tutorial!
I tried the VueJS tutorial!
I tried the FizzBuzz problem
I tried to verify yum-cron
I tried to organize the cases used in programming
I tried to summarize the state transition of docker
I tried to decorate the simple calendar a little
05. I tried to stub the source of Spring Boot
I tried to reduce the capacity of Spring Boot
I want to call the main method using reflection
[Rough commentary] I want to marry the pluck method
I tried to implement the Euclidean algorithm in Java
[JDBC ③] I tried to input from the main method using placeholders and arguments.
How to use the link_to method
I tried to implement the like function by asynchronous communication
I tried to introduce Bootstrap 4 to the Rails 6 app [for beginners]
How to use the include? method
I tried to chew C # (indexer)
[Rails] I tried using the button_to method for the first time
I tried to increase the processing speed with spiritual engineering
[JDBC] I tried to access the SQLite3 database from Java.
I tried to summarize the basics of kotlin and java
I want to expand the clickable part of the link_to method
I tried to summarize iOS 14 support
[Swift] I tried to implement the function of the vending machine
I tried to interact with Java
I want to use the sanitize method other than View.
I tried the Java framework "Quarkus"
[Rails] I tried deleting the application
The story I wanted to unzip
I tried to summarize the basic grammar of Ruby briefly
I tried to build the environment little by little using docker
I tried to build the environment of WSL2 + Docker + VSCode
I tried to summarize Java learning (1)
I tried to understand nil guard
I tried validation to unify the way hashtags are written
I tried to summarize Java 8 now
I tried to chew C # (polymorphism: polymorphism)
I tried upgrading from CentOS 6.5 to CentOS 7 with the upgrade tool
I tried to solve the problem of "multi-stage selection" with Ruby
I tried to summarize the words that I often see in docker-compose.yml
[Metal] I tried to figure out the flow until rendering using Metal
I tried to sort the data in descending order, ascending order / Rails
I tried to build the environment of PlantUML Server with Docker
[Ruby] Tonight, I tried to summarize the loop processing [times, break ...]
I tried to implement the image preview function with Rails / jQuery
Special Lecture on Multi-Scale Simulation: I tried to summarize the 5th
I tried to translate the error message when executing Eclipse (Java)