Parametric methods and parameter passing in Java se basic learning

Posted by ijmccoy on Sat, 11 Sep 2021 03:45:41 +0200

Review object orientation:

Object, class.
Solution:
1. Abstract class (noun)
2. Abstract properties
3. Abstract method: one function, one method
4. Create object:
Class name object name = new class name ();
Use the mutual cooperation between objects to complete the task;
Message passing mode: attribute, method call (return value, parameter);

Parametric method

Access control character (public) return value method name (parameter list){
Method body
}
Return value: whether to return data to the caller
Parameter list: data type variable name, data type variable name
Formal parameter list: when defining a method, data type, variable name, data type, variable name,,, do not need assignment
Argument list: when calling a method, write an expression directly (it can be used only after assignment)
The argument list must access the data according to the requirements of the formal parameter list.

Homework after class

Merge two ascending permutations

package Daican;

import java.util.Arrays;

public class Work2 {
	public int[] hebing(int[] num1,int[] num2) {
		int[] num3=new int[num1.length+num2.length];
		for (int i=0;i< num1.length; i++) {
			num3[i]=num1[i];
		}
		for (int i=0;i< num2.length;i++) {
			num3[i+num1.length]=num2[i];
		}
		Arrays.sort(num3);
		return num3;
	}
	public static void main(String[] Args) {
		Work2 er=new Work2();
		int num1[]= {20,26,28,16,18};
		int num2[]= {66,68,88,56,181};
		int num3[]=er.hebing(num1,num2);
		for (int i : num3) {
			System.out.println(i+"");
		}
	}
}
	


Find PI:

public class PICal {
	public double PIcal(int n) {
		double result=0;
		for(int i=1;i<=n;i++) {
			if(i%2==1) {
				result+=4*(1.0/(2*i-1));	
			}else {
				result-=4*(1.0/(2*i-1));
		}
	}
		return result;
	}
public static void main(String[] agrs) {
	PICal a=new PICal();
	double P=a.PIcal(20000);
	System.out.println(P);
}
}


Variable length parameter means that the number of parameters can be 0-more
Methods use variables in the body, just like arrays
Limitations: the data types of parameters should be consistent
There is at most one variable length parameter in the formal parameter list
Variable length and common parameters can exist at the same time, and the variable length parameter must be the last.
Ticket buying exercise: according to different age groups of tourists, the price they need to pay is different:

public class Ticket {
	tourist t=new tourist();
	int money;
	int TouristNumbers;
	int count=0;
	int tmoney;
	int population=0;
	
	public int price() {
		t.PersonalInformation();
		population++;
		if(t.age<60&& t.age>6) {
			count++;
			money=20;
		}else {
			System.out.print(t.name+"Free ticket");
		}
		tmoney=count*money;
		return tmoney;
	}
	public void Allmoney() {
		price();
		System.out.println("common"+population+"People buy tickets");
		System.out.println("The number of free tickets is:"+(population-count));
		System.out.println("Actual number of ticket buyers:"+count);
		System.out.println("---------------------------");
		System.out.println("Tourists to whom this ticket belongs:");
		System.out.println("\t full name:"+t.name);
		System.out.println("\t Age:"+t.age);
		System.out.println("\t Ticket Price:"+money);
		System.out.println("---------------------------");
		System.out.println("Total payment required:"+tmoney);
	}
	

}

public class TextTicket {

	public static void main(String[] args) {
		Ticket tour=new Ticket();
		tour.Allmoney();

	}

}


Parameter transfer

Formal parameter list: data type variable name, data type variable name
Argument list: according to the requirements of formal parameters (number, data type, order)
Relationship between argument and formal parameter:
When the formal parameter changes, will the argument change?
Method: the change of formal parameter does not affect the value of argument

Topics: Java