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
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
The sidebar is recommended
- MySQL reports an error in Navicat. How to solve it?
- Router link cannot jump into router view (Vue)
- Why is the camera preview frame obtained by Android like this
- How to implement JS? The address bar changes the page load and refresh, and the selected conditions of the current page remain unchanged as all by default
- Why did 0.3 become 0.2999999999
- What is the reason for the warning when Vue project is packaged
- Yolov5 deployment final inference report cannot find picture
- How to extract the characters of a paragraph in Notepad?
- Computer network technology IP questions
- MySQL group query gets the data of the first 24 hours in each group
guess what you like
-
What's wrong with this and need to be changed?
-
Write a program to find the number of integers between 200 and 300 that meet the following conditions. Condition: the product of hundreds, tens and individuals is equal to the sum of hundreds, tens and individuals
-
In Python, the condition setting and interpretation of while loop
-
How can jupyter notebook run on the next line of code followed by the above code
-
The page has been loaded. Press F12 at the top to show it. What's going on
-
This CompareTo method is not called. How do you sort the elements in the collection
-
Using pointer method: input three integers ABC and output them in order of size
-
C # new ADO Net data model, error, failed to load file or assembly
-
Input n integers from the keyboard and store them in a pile of arrays. Put all positive numbers in the array in front of the array and negative numbers in the back to output the array
-
Why can't the small gourd be installed.
Random recommended
- Java byte array and string conversion
- Definition of two-dimensional array in C language
- Why is there an extra 0 here?
- Python code programs don't
- Measure the distance with sound wave, and then send the result to the serial port. Do you want to interrupt twice, language interrupt, and ask for an idea
- An error occurs when starting Hadoop. How to solve it?
- What about the problem of remote access server?
- pywintypes. com_ Error: (- 2147023728, 'element not found', none, none)
- Global variables don't work in this program? Why is the answer a
- In Python, there is something wrong with the program and an error is reported
- Simple OJ questions for Freshmen
- Butterfly effect problem, insufficient number of input parameters
- Use of extractall function of Python zipfile module
- After spring imports the database and c3p0 dependency package in idea, it will prompt the database for abnormal error
- Python from introduction to practice 5-10
- 1703792 what does that mean? What's wrong?
- C language and C + are easy to search dogs
- This interface is always used when I use CONDA to install tensorflow? About half an hour. How?
- React summernote rich text editor!! Why do some function icons of rich text editor fail after I pack them
- How does silly girl robot log in with mobile phone
- Why can't I see the absolute path of the command
- Pycharm uses the requests crawler to report the wrong requests exceptions. SSLError: HTTPSConnectionPool
- How to run multiple sleep () simultaneously in C language
- The data requested by Axios in vue3 is put into ecarts, which always shows that setoption is not defined
- What do these two for loops mean
- Java judges whether there are 30 consecutive int arrays with a length of 1000 as the growth trend
- Force deduction 221 question maximum square
- When I set this layout, I prompted an error from line 18
- The import javax. security. auth. message cannot be resolved
- The hottest Christmas tree code. one hundred and twenty-three
- Open the edge browser and display "what if I can't access this page?"?
- Running problems of java simple supermarket system
- Is the data [10] here an address or an element? The concept is always unclear
- Python crawling news content
- How to deal with the problem of general code framework
- Bubble sort, how can there be an extra number in front
- The graph drawing algorithm of Python is described by Raptor flow chart software
- Numpy creates an array of the specified data range
- Python crawler obtains the data of Douban movie top250 fantasy class
- R7-2 minimum spanning tree construction (25 points)