current position:Home>Java interface class program, define an interface and define two classes to implement the interface, then use the sorting method in the class, then enter an array with length of 8 in main, and call newsort with the two implementation classes as arguments

Java interface class program, define an interface and define two classes to implement the interface, then use the sorting method in the class, then enter an array with length of 8 in main, and call newsort with the two implementation classes as arguments

2022-02-03 01:03:27 CSDN Q & A

The static method code for sorting integer arrays is as follows :
class SortedInts {
public static void newsort(int[] numbers, SortMethod s) {
s.sort(numbers);
for (int n : numbers) {
System.out.printf("%d ", n);
}
System.out.println();
}
}
among SortMethod It's an interface , Please define this interface , And define 2 A class implements the interface , In these two implementation classes, direct insertion sort and bubble sort are used to realize sort Method .
And then in main Method, enter a length of 8 Array of , Use the objects of the two implementation classes as the actual parameters to call newsort Method to sort . for example :
int[] ns = new int[8];
......
InsertSort is = new InsertSort();
SortedInts.newsort(ns, is);
The program input and output are as follows :
9 3 5 2 1 7 23 8
1 2 3 5 7 8 9 23
1 2 3 5 7 8 9 23
( Be careful : Finally, there is a blank line )




Refer to the answer 1:
import java.util.*;public class Test {    public static void main(String[] args){        int[] ns1 = new int[8];        int[] ns2 = new int[8];        Scanner sc = new Scanner(System.in);        for(int i = 0;i < 8;i++){            ns1[i] = sc.nextInt();            ns2[i] = ns1[i];        }        sc.close();        InsertSort is = new InsertSort();        SortedInts.newsort(ns1, is);        BubbleSort bs = new BubbleSort();        SortedInts.newsort(ns2, bs);                    }}class SortedInts {    public static void newsort(int[] numbers, SortMethod s) {        s.sort(numbers);        for (int n : numbers) {            System.out.printf("%d ", n);        }        System.out.println();    }}
public interface SortMethod {    public void sort(int[] number);}
public class InsertSort implements SortMethod{    @Override    public void sort(int[] number) {        for(int i=1; i<number.length; i++){            for(int j=i; j>0; j--){                if(number[j] < number[j-1]){                    int temp = number[j-1];                    number[j-1] = number[j];                    number[j] = temp;                }            }        }    }}
public class BubbleSort implements SortMethod{    @Override    public void sort(int[] number) {        for(int i = 0;i < number.length - 1;i++){            for(int j = 0;j < number.length - 1 - i;j++){                if(number[j] > number[j+1]){                    int temp = number[j];                    number[j] = number[j+1];                    number[j+1] = temp;                }            }        }    }}



Refer to the answer 2:



Refer to the answer 3:

SortMethod

public interface SortMethod {    void sort(int[] numbers);}

PopSort

public class PopSort implements SortMethod{    @Override    public void sort(int[] num) {        int temp ;        for(int i = 0 ; i < num.length-1; i++){            for(int j = 0; j <num.length-1;j++){                if(num[j]>num[j+1]){                    temp = num[j];                    num[j] = num[j+1];                    num[j+1] = temp;                }            }        }    }}

InsertSort

public class InsertSort implements SortMethod {    @Override    public void sort(int[] num) {        int j;        for (int i = 1; i < num.length ; i++) {            int temp = num[i];            for (j = i - 1;j >= 0&&num[j] > temp ;j--)            {                num[j + 1] = num[j];            }            num[j + 1] = temp;        }    }}

Test Test class

public class Test {    public static void main(String[] args) {        newsort(new int[]{
   
   
   
   9 ,3, 5, 2, 1, 7, 23, 8}, new InsertSort());        newsort(new int[]{
   
   
   
   9 ,3, 5, 2, 1, 7, 23, 8}, new PopSort());    }    public static void newsort(int[] numbers, SortMethod s) {        s.sort(numbers);        for (int n : numbers) {            System.out.printf("%d ", n);        }        System.out.println();    }}

There are four files

img




Refer to the answer 4:



Refer to the answer 5:

Interface

public interface SortMethod {    int[] sort(int[] numbers);}

Bubble sort implementation class

public class BubbleSort implements SortMethod {    @Override    public int[] sort(int[] numbers) {        for(int i=0;i<numbers.length-1;i++)        {            for(int j=0;j<numbers.length-1-i;j++)            {                if(numbers[j]>numbers[j+1])                {                    int temp=numbers[j];                    numbers[j]=numbers[j+1];                    numbers[j+1]=temp;                }            }        }        return numbers;    }}

Insert sort implementation class

public class InsertSort implements SortMethod {    @Override    public int[] sort(int[] numbers) {        for(int index = 1; index<numbers.length; index++){// The outer layer is to the right index, That is, as the data of the comparison object index            int temp = numbers[index];// Data used for comparison             int leftindex = index-1;            while(leftindex>=0 && numbers[leftindex]>temp){// When you're on the far left or when you're on the far left temp Small data , End of cycle                 numbers[leftindex+1] = numbers[leftindex];                leftindex--;            }            numbers[leftindex+1] = temp;// hold temp Put it in the empty space         }        return numbers;    }}

Use class

public class SortedInts {    public static void newsort(int[] numbers, SortMethod s) {        s.sort(numbers);        for (int n : numbers) {            System.out.printf("%d ", n);        }        System.out.println();    }}

Experimental code

@Test    public void test4(){        Random random = new Random();        Set<Integer> integerSet = new HashSet<>();        int[] ns = new int[8];        for (int i = 0 ; i < 8 ; i++){            integerSet.add(random.nextInt(50));        }        System.out.println(" Primitive sequence of numbers ");        System.out.println(integerSet);        int i = 0;        for (Integer integer : integerSet){            ns[i] = integer;            i++;        }        System.out.println(ns.toString());        InsertSort is = new InsertSort();        System.out.println(" Insertion sort ");        SortedInts.newsort(ns, is);        BubbleSort bubbleSort = new BubbleSort();        System.out.println(" Bubble sort ");        SortedInts.newsort(ns, bubbleSort);    }

Running results

 Primitive sequence of numbers [0, 32, 33, 49, 21, 10, 11, 43][[email protected] Insertion sort 0 10 11 21 32 33 43 49  Bubble sort 0 10 11 21 32 33 43 49 

Exactly according to your request , I hope the subject will adopt ^-^




Refer to the answer 6:

copyright notice
author[CSDN Q & A],Please bring the original link to reprint, thank you.
https://en.primo.wiki/2022/02/202202030103251790.html

Random recommended