Synchronization exercise (Java SE)

Keywords: Java Back-end

Case 1: data sorting in string

Requirement: there is a string: "91 27 46 38 50". Please write a program to realize it. The final output result is: "27 38 46 50 91"

Idea: 1. Define a string

        2. Store the numeric data in the string into an array of int type

                Get each numeric data in the string?

                public String[] split(String regex)

                Define an int array and store each element in the String [] array in the int array

                public static int parseInt(String s)

        3. Sort int array

        4. Splice the elements in the sorted int array to get a string. Here, the splicing is implemented by StringBuilder

        5. Output results

package test;

import java.util.Arrays;

public class Test48 {
    public static void main(String[] args) {
        // 1. Define a string
        String s = "91 27 46 38 50";
        // 2. Store the numeric data in the string into an array of int type
        //      Get each numeric data in the string?
        //      public String[] split(String regex)
        //      Define an int array and store each element in the String [] array in the int array
        //      public static int parseInt(String s)
        String[] strArray = s.split(" ");
        int[] arr = new int[strArray.length];
        for (int i = 0; i<strArray.length; i++){
            arr[i] = Integer.parseInt(strArray[i]);
        }
        // 3. Sort int array
        Arrays.sort(arr);
        // 4. Splice the elements in the sorted int array to get a string. Here, the splicing is implemented by StringBuilder
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i<arr.length; i++){
            if (i == arr.length-1){
                sb.append(arr[i]);
            }else{
                sb.append(arr[i]).append(" ");
            }
        }
        String result = sb.toString();
        // 5. Output results
        System.out.println("The result of sorting is:"+result);
    }
}

Case 2: Date tools

Requirement: define a date tool class (DeteUtils), which contains two methods: convert the date to a string in the specified format; Parse the string into a date in the specified format, and then define a test class (DateDemo) to test the methods of the date tool class

Idea: 1. Define date tool class (DateUtils)

        2. Define a dateToString method to convert the date into a string in the specified format

                Return value type: String

                Parameters: date, date, String format

        3. Define a method stringToDate, which is used to parse the string into a date in the specified format

                Return value type: Date

                Parameters: String s, String format

        4. Define the test class DateDemo and call the methods in the date tool class  

Date utility class (DateUtils)

package test.test49;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 1.Define date tool class (DateUtils)
 *      Tool class construction methods are private and member methods are static
 */
public class DateUtils {
    private DateUtils(){

    }

    /**
     * 2.Define a dateToString method to convert the date into a string in the specified format
     *          Return value type: String
     *          Parameters: date, date, String format
     */
    public static String dateToString(Date date,String format){
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        String s = sdf.format(date);
        return s;
    }

    /**
     * 3.Define a method stringToDate, which is used to parse the string into a date in the specified format
     *          Return value type: Date
     *          Parameters: String s, String format
     */
    public static Date stringToDate(String s,String format) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        Date d = sdf.parse(s);
        return d;
    }
}

Test class (DateDemo)

package test.test49;

import java.text.ParseException;
import java.util.Date;

// 4. Define the test class DateDemo and call the methods in the date tool class
public class DateDemo {
    public static void main(String[] args) throws ParseException {
        // Create date object
        Date d = new Date();

        String s1 = DateUtils.dateToString(d, "yyyy year MM month dd day HH:mm:ss");
        System.out.println(s1);

        String s2 = DateUtils.dateToString(d, "yyyy year MM month dd day");
        System.out.println(s2);

        String s3 = DateUtils.dateToString(d, "HH:mm:ss");
        System.out.println(s3);
        System.out.println("--------");

        String s = "2021-11-09 15:05:15";
        Date date = DateUtils.stringToDate(s, "yyyy-MM-dd HH:mm:ss");
        System.out.println(date);
    }
}

Case 3: February day

Demand: get the number of days in February of any year

Idea: 1. Enter any year on the keyboard

        2. Set year, month and day of calendar object

                Year: from keyboard entry

                Month: set to March, and the month starts from 0, so the set value is 2

                Day: set to January

        3. March 1 is pushed forward one day, which is the last day of February

        4. Just get the output of this day

package test;

import java.util.Calendar;
import java.util.Scanner;

public class Test50 {
    public static void main(String[] args) {
        // 1. Enter any year on the keyboard
        Scanner sc = new Scanner(System.in);
        System.out.println("Please enter the year:");
        int year = sc.nextInt();
        // 2. Set year, month and day of calendar object
        //      Year: from keyboard entry
        //      Month: set to March, and the month starts from 0, so the set value is 2
        //      Day: set to January
        Calendar c = Calendar.getInstance();
        c.set(year,2,1);
        // 3. March 1 is pushed forward one day, which is the last day of February
        c.add(Calendar.DATE,-1);
        // 4. Just get the output of this day
        int date = c.get(Calendar.DATE);
        System.out.println(year+"In February"+date+"day");
    }
}

Posted by NogDog on Wed, 10 Nov 2021 04:44:22 -0800