So i posted an original problem and it worked now i need to make it OOP. The pro
ID: 3836627 • Letter: S
Question
So i posted an original problem and it worked now i need to make it OOP. The program runs and compiles I just need to seperate and add calls! Use Java programming to write a program to sort a one dimension array. Prompt the user to enter at least 16 names, then have the ability to choose from one of three different sort algorithms from the menu. The program should be able to display the unsorted then sorted Array for an output. OOP must be implemented in your project. /// I'm using BlueJ if that helps you help me! Thanks! Here's what I have:
import java.util.Scanner;
public class sortmachine2{
public static void printArray(String names[]){
for(int i=0;i<names.length;i++)
System.out.println(names[i]);
System.out.println();
}
public static void bubbleSort(String arr[])
{
int n =arr.length;
for(int i=0;i<n-1;i++)
for(int j=0;j<n-i-1;j++)
if(arr[j].compareTo(arr[j+1])>0)
{
String temp= arr[j];
arr[j]=arr[j+1];
arr[j+1]=temp;
}
}
public static void selectionSort(String arr[])
{
int n=arr.length;
for(int i=0;i<n-1;i++)
{
int min_idx=i;
for(int j=i+1;j<n;j++)
if(arr[j].compareTo(arr[min_idx])<0)
min_idx=j;
String temp=arr[min_idx];
arr[min_idx]=arr[i];
arr[i]=temp;
}
}
public static void insertionSort(String arr[])
{
int n =arr.length;
for(int i=1;i<n;++i)
{
String key =arr[i];
int j =i-1;
{
arr[j+1]=arr[j];
j=j-1;
}
arr[j+1]=key;
}
}
public static void main(String[] args){
Scanner Tom= new Scanner(System.in);
System.out.print("How many names do you want to sort?");
int n=Tom.nextInt();
while(n<16){
System.out.print("Enter at least 16!");
n=Tom.nextInt();
}
String[]names=new String[n];
System.out.println("Enter names:");
for(int i=0;i<n;i++)
names[i]=Tom.next();
System.out.println("Unsorted names:");
printArray(names);
System.out.println("Choose:1.BubbleSort 2.SelectionSort 3.InsertionSort");
int op= Tom.nextInt();
if(op==1){
bubbleSort(names);
}else if(op==2){
selectionSort(names);
}else if(op==3){
insertionSort(names);
}else{
System.out.println("invalid option!!");
System.out.println("Sorting using Bubble Sort by default");
bubbleSort(names);
}
System.out.println(" After sorting:");
printArray(names);
Tom.close();
}
}
Explanation / Answer
import java.util.Scanner;
public class SortNames {
public static void printArray(String names[]){
for(int i=0; i<names.length; i++)
System.out.println(names[i]);
System.out.println();
}
public static void bubbleSort(String arr[])
{
int n = arr.length;
for (int i = 0; i < n-1; i++)
for (int j = 0; j < n-i-1; j++)
if (arr[j].compareTo(arr[j+1]) > 0)
{
// swap temp and arr[i]
String temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
public static void selectionSort(String arr[])
{
int n = arr.length;
// One by one move boundary of unsorted subarray
for (int i = 0; i < n-1; i++)
{
// Find the minimum element in unsorted array
int min_idx = i;
for (int j = i+1; j < n; j++)
if (arr[j].compareTo(arr[min_idx]) < 0)
min_idx = j;
// Swap the found minimum element with the first
// element
String temp = arr[min_idx];
arr[min_idx] = arr[i];
arr[i] = temp;
}
}
/*Function to sort array using insertion sort*/
public static void insertionSort(String arr[])
{
int n = arr.length;
for (int i=1; i<n; ++i)
{
String key = arr[i];
int j = i-1;
/* Move elements of arr[0..i-1], that are
greater than key, to one position ahead
of their current position */
while (j>=0 && arr[j].compareTo(key) > 0)
{
arr[j+1] = arr[j];
j = j-1;
}
arr[j+1] = key;
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("How many name want to sort(>= 16): ");
int n = sc.nextInt();
while(n < 16){
System.out.print("Enter at least 16 : ");
n = sc.nextInt();
}
String[] names = new String[n];
System.out.println("Enter 16 names: ");
for(int i=0; i<n; i++)
names[i] = sc.next();
// displaying unsorted names
System.out.println("Unsorted names: ");
printArray(names);
System.out.println("Choose: 1.BubbleSort 2.SelectionSort 3.InsertionSort");
int op = sc.nextInt();
if(op == 1){
bubbleSort(names);
}else if(op == 2){
selectionSort(names);
}else if(op == 3){
insertionSort(names);
}else{
System.out.println("invalid option!!!!");
System.out.println("Sorting using Bubble Sort by default");
bubbleSort(names);
}
System.out.println(" After sorting: ");
printArray(names);
sc.close();
}
}
/*
Sample run:
How many name want to sort(>= 16): 16
Enter 16 names:
dfekeknjf
mv vbn
ijtyhmlk
tsdqtrwd
okpkpk
tyfvyfyjt
cdrgs
kj
tyytf
eaw
uhuu
mnjb
vhfy
dgfge
afefe
Unsorted names:
dfekeknjf
mv
vbn
ijtyhmlk
tsdqtrwd
okpkpk
tyfvyfyjt
cdrgs
kj
tyytf
eaw
uhuu
mnjb
vhfy
dgfge
afefe
Choose: 1.BubbleSort 2.SelectionSort 3.InsertionSort
2
After sorting:
afefe
cdrgs
dfekeknjf
dgfge
eaw
ijtyhmlk
kj
mnjb
mv
okpkpk
tsdqtrwd
tyfvyfyjt
tyytf
uhuu
vbn
vhfy
*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.