Day 12 : Parameters and Arguments

Chi-Tse Chiang
2022-09-27 23:25:48

Intro

Continuing from the previous day's function, today we will continue to introduce arguments and parameters.
Here is a brief review of the biggest difference between the two:

1. Parameters (Parameters): variables written when defining a function.

2. Arguments (Arguments): values passed in when calling a function.

A very related concept is that we can call the formula that defines a function a declaration and the formula that calls a function or assigns it to another variable an expression.
Next, I will share the concept of writing object-oriented programs. We usually create two different classes. One main class stores all variables, properties, and functions, and the other is responsible for controlling the code to be executed. However, since we have not yet introduced the syntax for creating objects, they are placed in the same file here and divided into different classes.

Supplementary introduction If you are using a terminal to execute the java file, the following command is attached to execute the two files together:

The following uses the attached code to continue the introduction of argument parameters and usage methods:

public class First{                               /*first class*/
	static void statment(){
	System.out.println("Hello, Classes !!!");
  }

	static void average(int gradeA, int gradeB, int gradeC){
	float num;
    num = ((float)x + (float)y + (float)z)/3;     //強制轉型
    return num;
	}

  static float calculate(int x, int y, int z){    // x & y are Parameters
	float ave;
    System.out.println("The lowest grade is : " + Math.min(x,y,z));
	ave = average(x,y,z);
	return ave;
  }

	/*public int sum(int a, int b,int c){
		int summary = a + b + c;
		return summary;
	}*/
}

public class Second{                           //second class
  public static void main(String[] args){
     int aStudent = 92;
     int bStudent = 75;
     int cStudent = 68;
     First.statment();                // Using method in the other "class"
     float average = calculate(aStudent, bStudent, cStudent);
     System.out.println("The average is " + average);

    /*System.out.println("The total is " + sum(aStudent, bStudent, cStudent));*/
  }
}

Program analysis: This program is used to store functions in the first class, and the second class is used to hold the program to be executed.

1. In the first class, we have written a total of three functions. The first function can be found in the second class and is used directly with the instruction className . method_name(). Then we call the calculate function and pass in the three student grades as arguments, first printing the lowest score.

2. It is worth noting that in this function, we call the average function of the same type again to calculate the average (this is to help you understand some basic usage of functions). Then it returns to the original function and finally returns to the Second type to print it.

3. Finally, we see the parts that have been commented out in the two types. It seems to be fine, but the result is an error?
->The reason is that the function's modifiers, which we mentioned in the previous chapter, require an object to execute the method and cannot be used statically.

If the function is not changed, it can be used like this in the second category

First total = new First();    //Class_Name  Object_name  = new Class_name() ;
System.out.println("The total is " + total.sum(aStudent, bStudent, cStudent));

There will be a more detailed introduction to objects later. Here, remember that you may need to use the keyword new to create a new object.