Java foundation consolidation Day5 job

1, Basic case

Topic 1

Define a method that can accept an integer (this integer is greater than 3) and print all even numbers from 0 to this integer (inclusive)
If the accepted number is 6, the even number printed after calling the method is 0 2 4 6
If the accepted number is 5, the even number printed after calling the method is 0 2 4

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		//1.1
		printEvenNumber(6);
	}
	public static void printEvenNumber(int num) {
		for(int i=0; i<num+1;i++) {
			if(i%2 ==0) {
				System.out.print(i+" ");
			}
		}
		System.out.println();
	}

Topic 2

Two integers are randomly generated. The range of random numbers is [1100]. Define a method to sum the two integers and print the sum value.

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Random r=new Random();
		int a = r.nextInt(100)+1;
		int b = r.nextInt(100)+1;
		int sum = sum(a,b);
		System.out.println("The values of the two random numbers are:"+sum);
		
	}
	public static int sum(int a, int b) {
		return a+b;
	}

Topic 3

Given array int[] arr= {10,20,30,40,50,60} in the main method; Define a method that can accept the given array
And returns the minimum value of the element in the array

	public static void main(String[] args) {
		int[] arr= {10,20,30,40,50,60};
		int min = findMinValue(arr);
		System.out.println("min:"+min);
	}
	public static int findMinValue(int[] arr) {
		int min = arr[0];
		for(int i=1;i<arr.length;i++) {
			if(arr[i]<min) {
				min = arr[i];
			}
		}
		return min;
	}

Topic 4

A method is defined that can accept an integer,
If the range is [90100], the method returns' A '
If it is [80,90), the range method returns' B '
If it is [70,80), the range method returns' C '
If it is [60,70), the range method returns'D '
If it is [0,60), the range method returns' E '
If the integer is not in the above range, return 'F'

	public static void main(String[] args) {
			Scanner sc = new Scanner(System.in);
		System.out.println("Please enter 0-100 Integer of:");
		int num = sc.nextInt();
		String n = judgeNumber(num);
		System.out.println(n);
		
	}
	public static String judgeNumber(int num) {
		if(num>=90 && num<=100) {
			return "A";
		}else if(num>=80 && num<90) {
			return "B";
		}else if(num>=70 && num<80) {
			return "C";
		}else if(num>=60 && num<70) {
			return "D";
		}else if(num>=0 && num<60) {
			return "E";
		}else {
			return "F";
		}
	}

Topic 5

Define a method to find the number of times a given number appears in a given int array. If it does not appear once, it returns 0.
For example, given the number 3, find 3 in the array int[] arr = {3,4,3,5,7,9}; Number of occurrences in

	public static void main(String[] args) {
		int[] arr = {3,4,3,5,7,9};
		int num =3;
		int count = count(num,arr);
		System.out.println(num+"In array arr Appeared in"+count+"second");
	}
	public static int count(int num,int[]arr) {
		int count =0;
		for(int e:arr) {
			if(num == e) {
				count++;
			}
		}
		return count;
		
	}

Topic 6

Define a method to find the position where the specified number appears in the array (if it appears multiple times, print multiple times)
For example, if the number to be found in array [1232] is 2, the index values 1 and 3 will be printed inside the method
If the number to be found in array [1232] is 5, each method will print "there is no such number in the array"

	public static void main(String[] args) {
		int[] arr= {1,2,3,2};
		findElementCount(5, arr);
	}
	public static void findElementCount(int e,int[] arr) {
		int count =0;
		for(int i=0;i<arr.length;i++) {
			if(arr[i] == e) {
				count++;
				System.out.print(i+" ");
			}
			if(count!=0 && i == arr.length-1) {
				System.out.println();
			}
		}
		if(count == 0) {
			System.out.println("This number is not in the array");
		}
		
	}

Topic 7

Define a method to find the results of addition, subtraction, multiplication and division of two integers at the same time, and return the four results at the same time (put the four numbers into an array and return)

	public static void main(String[] args) {
		int[] result = caculate(8,2);
		for(int e:result) {
			System.out.print(e+" ");
		}
	}
	public static int[] caculate(int a,int b) {
		int[] result = new int[4];
		int add = a+b;
		result[0] = add;
		int subtraction = a-b;
		result[1]=subtraction;
		int multiply = a*b;
		result[2] = multiply;
		int division = a/b;
		result[3] =division;
		return result;
	}

2, Extended case

Topic 1

Given array int[] arr= {10,20,30,40,50,60} in the main method; This array has no duplicate elements Define a method that can accept the given array and return the index value of the largest element value in the array

	public static void main(String[] args) {
		int[] arr= {10,20,30,40,50,60};
		int index =findMaxValue(arr);
		System.out.println("array arr The maximum index is:"+index);
	}
	public static int findMaxValue(int[] arr) {
		int max = 0;
		for(int i=1;i<arr.length;i++) {
			if(arr[i]>arr[max]) {
				max = i;
			}
		}
		return max;
	}

Topic 2

Analyze the following requirements and implement them in code
1. Enter the length and width of the rectangle with the keyboard
Define a method to calculate the perimeter of the rectangle and print the perimeter in the main method
2. Enter the length and width of the rectangle with the keyboard
Define a method to calculate the area of the rectangle and print the area in the main method
3. Enter the radius of the circle with the keyboard
Define a method to calculate the circumference of the circle and print the circumference in the main method
4. Enter the radius of the circle with the keyboard
Define a method to calculate the area of the circle and print the area in the main method

	public static void main(String[] args){
		Scanner sc = new Scanner(System.in);
		System.out.println("Please enter the length and width of the rectangle to calculate the perimeter of the rectangle:");
		int a = sc.nextInt();
		int b=sc.nextInt();
		System.out.println("The perimeter of the rectangle is:"+rectanglePerimeter(a, b));
		System.out.println("Please enter the length and width of the rectangle to calculate the area of the rectangle:");
		int c = sc.nextInt();
		int d=sc.nextInt();
		System.out.println("The rectangular area is:"+rectangleArea(a, b));
		System.out.println("----------------------------------------");
		System.out.println("Please enter the radius of the circle to calculate the circumference of the circle:");
		int r1 = sc.nextInt();
		System.out.println("The circumference of the circle is:"+circlePerimeter(r1));
		System.out.println("Please enter the radius of the circle to calculate the area of the circle:");
		int r2 = sc.nextInt();
		System.out.println("The circular area is:"+circleArea(r2));
	}
	public static int rectangleArea(int a, int b) {
		return a*b;
	}
	public static int rectanglePerimeter(int a, int b) {
		return (a+b)*2;
	}
	public static double circleArea(int r) {
		return 3.14*r*r;
	}
	public static double circlePerimeter(int r) {
		return 2*3.14*r;
	}

Topic 3

Analyze the following requirements and implement them in code
1. Define a method equals(int[] arr1,int[] arr2). Function: compare whether two arrays are equal (if the length and content are equal, it is considered that the two arrays are the same)
2. Define a method fill(int[] arr,int value), function: change the value of all elements in array arr to value
3. Define a method fill(int[] arr,int fromIndex,int toIndex,int value). Function: change the value of the element in array arr from the index fromIndex to toindex (excluding toIndex) to value
4. Define a method copyOf(int[] arr, int newLength), function: copy the newLength elements in array arr to the new array, and return the new array, starting with index 0
5. Define a method copyOfRange(int[] arr,int from, int to). Function: copy the elements in array arr from index from (including from) to index to (excluding to) to the new array, and return the new array

	public static boolean equals(int[] arr1,int[] arr2) {
		if(arr1.length != arr2.length) {
			return false;
		}
		for(int i =0;i<arr1.length;i++) {
			if(arr1[i] != arr2[i]) {
				return false;
			}
		}
		return true;
	}
	public static void fill(int[] arr,int value) {
		for(int i=0;i<arr.length;i++) {
			arr[i]=value;
		}
	}
	public static void fill(int[] arr,int fromIndex,int toIndex,int value) {
		for(int i=fromIndex;i<toIndex;i++) {
			arr[i]=value;
		}
	}
	public static int[] copyOf(int[] arr, int newLength) {
		int[] newArr = new int[newLength];
		for(int i=0;i<newLength;i++) {
			newArr[i] = arr[i];
		}
		return newArr;
	}
	public static int[] copyOfRange(int[] arr,int from, int to) {
		int[] newArr = new int[to-from];
		for(int i=from;i<to;i++) {
			newArr[i] = arr[i];
		}
		return newArr;
	}

Topic 4

Define a method to print all leap year years between specified two years
For example, pass 2000 and 2005 into the method, and all leap years between them will be printed after the method is executed
2000 and 2004
Tips:
The algorithm for calculating leap years of Gregorian calendar: leap every four years, no leap every 100 years, and leap every 400 years
Translation:
A leap year is one that satisfies any of the following conditions
1) If the year is a hundred, it must be a multiple of 400 to be a leap year (it can be divided by 100 and can be divided by 400)
2) Other years can be 4 leap years
Example: 2000 is a whole hundred and a multiple of 400, so it is a leap year; 2004 is a multiple of 4. It is a leap year
2100 is a multiple of a hundred, but not a multiple of 400, so it is not a leap year

	public static void main(String[] args){
		int a =2000;
		int b=2008;
		String years = judgeYear(a,b);
		System.out.println("stay"+a+"Year to"+b+"Leap years between years have:"+years);
	}
	public static String judgeYear(int a,int b) {
		String year="";
		for(int i=a;i<=b;i++) {
			if(i%100 ==0) {
				if(i%4==0) {
					year=year+i+" ";
				}
			}else if(i%4==0) {
				year=year+i+" ";
			}
		}
		return year;
	}

Topic 5

1. Enter an integer (positive or negative numbers are OK, but the sign bit is not a valid digit)
2. Define a method whose function is to calculate how many digits the number is and return the digits
3. Print the number of digits in the main method
4. The presentation format is as follows:
(1) Demo 1:
Please enter an integer: 1234
Console output: 1234 is a 4-digit number
(2) Demo 2:
Please enter an integer: - 34567
Console output: - 34567 is a 5-digit number

	public static void main(String[] args){
		Scanner sc = new Scanner(System.in);
		System.out.print("Please enter an integer:");
		int num = sc.nextInt();
		System.out.println(num+"yes"+digitNum(num)+"Digit number");
	}
	public static int digitNum(int num) {
		int count =0;
		while(num>=1) {
			num=num/10;
			count++;
		}
		return count;
	}

Topic 6

Analyze the following requirements and implement them in code (each small requirement needs to be encapsulated into a method)
1. Sum two data (integer and decimal)
2. Judge whether two data are equal (integer and decimal)
3. Get the larger of the two numbers (integer and decimal)
4. Get the smaller of the two numbers (integer and decimal)
5. Can the two functions of 3 and 4 be realized in one method

	//1.
	public static int sum(int a, int b) {
		return a+b;
	}
	public static double sum(double a, double b) {
		return a+b;
	}
	//2.
	public static boolean isEqual(int a, int b) {
		return a==b;
	}
	public static boolean isEqual(double a, double b) {
		return a==b;
	}
	//3.
	public static int max(int a, int b) {
		if(a>b) {
			return a;
		}else{
			return b;
		}
	}
	public static double max(double a, double b) {
		if(a>b) {
			return a;
		}else{
			return b;
		}
	}
	//4.
	public static int min(int a, int b) {
		if(a<b) {
			return a;
		}else{
			return b;
		}
	}
	public static double min(double a, double b) {
		if(a<b) {
			return a;
		}else{
			return b;
		}
	}
	//5.
	public static int findMaxMinInNumbers(int a, int b,boolean flag) {
		//Find the maximum value when flag is true and the minimum value when flag is false
		if(flag) {
			if(a>b) {
				return a;
			}else{
				return b;
			}
		}else {
			if(a<b) {
				return a;
			}else{
				return b;
			}
		}
	}
	public static double findMaxMinInNumbers(double a, double b,boolean flag) {
		//Find the maximum value when flag is true and the minimum value when flag is false
		if(flag) {
			if(a>b) {
				return a;
			}else{
				return b;
			}
		}else {
			if(a<b) {
				return a;
			}else{
				return b;
			}
		}
	}

Topic 7

Define a method, pass in an int type array, and output each number in the array and the number of occurrences
For example, the print result of the input array [1,2,2,2,3,3,4,4,4,4] is:
The number 1 appears once
The number 2 appears three times

	public static void main(String[] args){
		int[] arr = {4,2,6,3,1,2,2,2,3,4,4,4};
		countArr(arr);
	}
	public static void countArr(int[] arr) {
		for(int i=0;i<arr.length-1;i++) {
			for(int j=0;j<arr.length-i-1;j++) {
				if(arr[j]>arr[j+1]) {
					int t = arr[j];
					arr[j]=arr[j+1];
					arr[j+1] = t;
				}
			}
		}
		for(int i=0;i<arr.length;i++) {
			if(i>0 && arr[i]==arr[i-1]) {
				continue;
			}
			int count = 1;
			for(int j=i+1;j<arr.length;j++) {
				if(arr[i] == arr[j]) {
					count++;
				}
			}
			System.out.println("number"+arr[i]+"There it is"+count+"second");
		}
	}

Topic 8

Analyze the following requirements and implement them in code
1. Create two arrays with length of 10. The elements in the array are randomly generated and non repeated integers between 1 and 100.
2. Define a method to pass in two arrays. The method splices the different elements of the two arrays into a string, and outputs the string and the length of the string to the console;
If not, output "sorry, all elements of the two arrays are the same"

	public static void main(String[] args){
		int[] arr1 = new int[10];
		int[] arr2 = new int[10];
		addE(arr1);
		addE(arr2);
		arrsToString(arr1,arr2);
	}
	public static void addE(int[] arr) {
		Random r = new Random();
		for(int i=0;i<arr.length;i++) {
			arr[i]=r.nextInt(100)+1;
		}
	}
	public static void arrsToString(int[] a, int[] b) {
		String result = "";
		int count=0;
		for(int i=0;i<a.length;i++) {
			boolean flag1 = false;
			for(int j=0;j<b.length;j++) {
				if(a[i]==b[j]) {
					flag1=true;
				}
			}
			if(!flag1) {
				result=result+a[i]+" ";
				count++;
			}
		}
		for(int i=0;i<b.length;i++) {
			boolean flag2 = false;
			for(int j=0;j<a.length;j++) {
				if(b[i]==a[j]) {
					flag2=true;
				}
			}
			if(!flag2) {
				result=result+b[i]+" ";
				count++;
			}
		}
		if(count == 0) {
			System.out.println("Sorry, all elements of the two arrays are the same!");
		}else {
			System.out.println("The string composed of two arrays is:"+result);
			System.out.println("The length of the string is:"+count);
		}
	}
		


Added by oskare100 on Fri, 24 Dec 2021 02:37:52 +0200