Methods of the class examples

Methods of the class
1. Write a method, following the welcome text:

************
**  Java  **
************

public class Demo {
	public void Welcome() {
		System.out.println("************");
		System.out.println("**  Java  **");
		System.out.println("************");
	}
}

public class Main {
	public static void main(String[] args) {
		Demo de=new Demo();
		de.Welcome();
	}
}

Parameter Method
1. Write a method of seeking a product of a number of 3. Then using this method, the required product 29.2,19,39.

public class Demo {
	public void multiple(double a,double b,double c) {
		double result=a*b*c;
		System.out.println("结果是:"+result);
	}
}

public class Main {
	public static void main(String[] args) {
		Demo de=new Demo();
		de.multiple(29.2, 19, 39);	// 注意:输出结果是21637.199999999997,而不是 21637.2,为什么?
        							// 21637.199999999997 和  21637.2 在实际意义上是相等的,因为double表示连续数,而连续数本身存在测量误差。	
	}
}

2. Write a method, the following functions: enter a number a, find its square, and then use this method, a squared 193.819.

public class Demo {
	public void showSquare(double a) {
		double result=a*a;
		System.out.println(result);
	}
}

public class Main {
	public static void main(String[] args) {
		Demo de=new Demo();
		de.showSquare(101.11);
	}
}

3. Write a method, the absolute value of a number. Then the absolute value of -123 using this method.

Note: the first to write class and add methods in class.

public class Demo {
	public void showAbs(double num) {
		double result=num;
		if(num<0) {
			result=0-num;			
		}
		System.out.println(result);
	}
}

public class Main {
	public static void main(String[] args) {
		Demo de=new Demo();
		de.showAbs(-123);
	}
}

4. Given an array, the array and find all elements. Step: the first to write class and add methods in class.

public class Demo {
	public void array(int[] arr) {
		int sum=0;
		int length=arr.length;
		for(int i=0;i<length;i++) {
			sum+=arr[i];
		}
		System.out.println(sum);
	}
}
	
public class Main {
	public static void main(String[] args) {
		int[] arr= {1,2,3,4,5};
		Demo de=new Demo();
		de.array(arr);
	}
}

The return value method

1. There are two arrays, find all of them, and the number.

public class Demo {
	public int sum(int[] arr) {
		int result=0;
		int length=arr.length;
		for(int i=0;i<length;i++) {
			result+=arr[i];
		}
		return result;
	}
}

public class Main {
	public static void main(String[] args) {
		int[] arr1= {1,2,3,4,5};
		int[] arr2= {6,7,8,9,10};
		Demo de=new Demo();
		int sum1=de.sum(arr1);
		int sum2=de.sum(arr2);
		int sum=sum1+sum2;
		System.out.println(sum);
	}
}
2. Given an array, for the minimum.
public class Demo {
	public int  getMin(int[] arr) {
		int min=arr[0];
		int length=arr.length;
		for(int i=0;i<length;i++) {
			if(arr[i]<min) {
				min=arr[i];
			}
		}
		return min;
	}
}

public class Main {
	public static void main(String[] args) {
		int[] arr= {8,2,3,4,5};				
		Demo de=new Demo();		
        int	min=de.getMin(arr);
		System.out.println(min);
		
	}		
}

 

Published 35 original articles · won praise 5 · Views 852

Guess you like

Origin blog.csdn.net/m0_43443133/article/details/105129240