Synchronization exercise (Java SE)

Keywords: Java Back-end

Case 1: method exercise

Requirements: design a method to print the larger of the two numbers

Analysis: 1. Define a method to print the larger of two numbers, such as getMax()

        2. Two variables are defined in the method to save two numbers

        3. Use branch statements to handle the size relationship between two numbers

        4. invoke a well-defined method in the main() method.

package test;

public class Test13 {
    public static void main(String[] args) {
        // 4. invoke a well-defined method in the main() method.
        getMax();
    }

    // 1. Define a method to print the larger of two numbers, such as getMax()
    public static void getMax(){
        // 2. Two variables are defined in the method to save two numbers
        int number1 = 10;
        int number2 = 20;
        // 3. Use branch statements to handle the size relationship between two numbers
        if (number1 > number2){
            System.out.println("The maximum number is:"+number1);
        }else{
            System.out.println("The maximum number is:"+number2);
        }
    }
}

Case 2: method exercise with parameters

Requirements: design a method to print the larger of the two numbers, and the data comes from the method parameters

Analysis: 1. Define a method to print the larger of two numbers, such as getMax()

        2. Define two parameters for the method to receive two numbers

        3. Use branch statements to handle the size relationship between two numbers

        4. call the defined method (using constant) in the main() method.

        5. call the defined method (using variables) in the main() method.

package test;

public class Test14 {
    public static void main(String[] args) {
        // 4. call the defined method (using constant) in the main() method.
        getMax(10,20);
        // 5. call the defined method (using variables) in the main() method.
        int number1 = 10;
        int number2 = 20;
        getMax(number1,number2);
    }

    // 1. Define a method to print the larger of two numbers, such as getMax()
    // 2. Define two parameters for the method to receive two numbers
    public static void getMax(int number1,int number2){
        // 3. Use branch statements to handle the size relationship between two numbers
        if (number1 > number2){
            System.out.println("The maximum number is:"+number1);
        }else{
            System.out.println("The maximum number is:"+number2);
        }
    }
}

Case 3: method exercise with return value

Requirements: a method is designed to obtain the larger of the two numbers, and the data comes from the method parameters

Analysis: 1. Define a method to print the larger of two numbers, such as getMax()

        2. Define two parameters for the method to receive two numbers

        3. Set the corresponding return value results in the two cases according to the question design

        4. call the definition number in the main() method and save it with variables.

        5. in the main() method, call the defined method and print it directly.

package test;

public class Test15 {
    public static void main(String[] args) {
        // 4. call the definition number in the main() method and save it with variables.
        int max = getMax(10, 20);
        System.out.println("The maximum number is"+max);
        // 5. in the main() method, call the defined method and print it directly.
        System.out.println("The maximum number is"+getMax(10,20));
    }

    // 1. Define a method to print the larger of two numbers, such as getMax()
    // 2. Define two parameters for the method to receive two numbers
    public static int getMax(int number1,int number2){
        //3. Set the corresponding return value results in the two cases according to the question design
        if (number1 > number2){
            return number1;
        }else{
            return number2;
        }
    }
}

Case 4: method overload exercise

Requirements: use the idea of method overloading to design a method to compare whether two integers are the same, and be compatible with all integer types (byte,short,int,long)

Analysis: 1. Define the compare() method to compare whether the values of two numbers are the same, and select two int parameters for the parameters

        2. Define the corresponding overload method, change the corresponding parameter type, and change the parameter to two long parameters

        3. Define all overloaded methods, two byte type and two short type parameters

        4. Complete the method call and test the running results

package test;

public class Test16 {
    public static void main(String[] args) {
        // 4. Complete the method call and test the running results
        System.out.println(compare(10,20));
        System.out.println(compare((byte)10,(byte)20));
        System.out.println(compare((short)10,(short)20));
        System.out.println(compare(10L,20L));
    }

    // 1. Define the compare() method to compare whether the values of two numbers are the same. Select two int parameters for the parameters
    public static boolean compare(int number1,int number2){
        System.out.println("int");
        return number1 == number2;
    }
    // 2. Define the corresponding overload method, change the corresponding parameter type, and change the parameter to two long parameters
    public static boolean compare(long number1,long number2){
        System.out.println("long");
        return number1 == number2;
    }
    // 3. Define all overloaded methods, two byte type and two short type parameters
    public static boolean compare(byte number1,byte number2){
        System.out.println("byte");
        return number1 == number2;
    }
    public static boolean compare(short number1,short number2){
        System.out.println("short");
        return number1 == number2;
    }
}

Case 5: array traversal

Requirements: design a method for array traversal. The traversal result is required to be on one line. For example: [11,22,33,44,55]

Analysis: 1. Because the results are required to be output on one line, you need to learn a new output statement System.out.print("content");

                System.out.println("content"); Output content wrap

                 System.out.print("content"); Output content does not wrap

                 System.out.println(); Line feed

        2. Define an array and initialize the array elements with static initialization

        3. Define a method to traverse the array in the general format of array traversal

        4. Modify the traversal operation with the new output statement

        5. Call traversal method

package test;

public class Test17 {
    public static void main(String[] args) {
        // 1. Because the result is required to be output on one line, you need to learn a new output statement System.out.print("content");
        //  System.out.println("content"); Output content wrap
        //  System.out.print("content"); Output content does not wrap
        //  System.out.println(); Line feed
        // 2. Define an array and initialize the array elements with static initialization
        int[] arr = {11,22,33,44,55};
        // 5. Call traversal method
        printArray(arr);
    }

    // 3. Define a method to traverse the array in the general format of array traversal
    public static void printArray(int[] arr){
        System.out.print("[");
        // 4. Modify the traversal operation with the new output statement
        for (int i = 0; i<arr.length; i++){
            if (i == arr.length-1){
                System.out.print(arr[i]);
            }else{
                System.out.print(arr[i]+",");
            }
        }
        System.out.println("]");
    }
}

Case 6: array maximum

Requirements: design a method to obtain the maximum value of elements in the array, call the method and output the result

Analysis: 1. Define an array and initialize the array elements with static initialization. 2

        2. Define a method to obtain the maximum value in the array

        3. Call the method to get the maximum value and accept the returned results with variables

        4. Output the results to the console

package test;

public class Test18 {
    public static void main(String[] args) {
        // 1. Define an array and initialize the array elements with static initialization
        int[] arr = {12,45,98,73,60};
        // 3. Call the method to get the maximum value and accept the returned results with variables
        int max = getMax(arr);
        // 4. Output the results to the console
        System.out.println("The maximum value in the array is:"+max);
    }

    // 2. Define a method to obtain the maximum value in the array
    public static int getMax(int[] arr){
        int max = arr[0];
        for (int i = 1; i<arr.length-1; i++){
            if (arr[i] > max){
                max = arr[i];
            }
        }
        return max;
    }
}

Posted by inkel on Sun, 31 Oct 2021 17:00:52 -0700