Can someone compile this and name the it A4MA5331550.java and compile to make .c
ID: 3885659 • Letter: C
Question
Can someone compile this and name the it A4MA5331550.java and compile to make .class file. Will need to name . class file A4MA5331550 also when done put both files in a zipped folder named A4MA5331550 and email the folder to ensaye@gmail.com
here is the program:
import java.util.Scanner;
/**
* 09/17/2017
* Dakota Mammedaty
* MA5331550
* Bubble sorted
*/
public class MA5331550 {
public static void main(String[] args) throws IOException {
Sort st =new Sort();
st.getData();
System.out.println("=================Sorting Algorithms=================");
System.out.println("1. Bubble Sort");
System.out.println("2. Selection Sort");
System.out.println("3. Insertion Sort");
System.out.println("Enter your choice");
Scanner scan = new Scanner(System.in);
int choice = scan.nextInt();
if(choice == 1)
st.bubble();
else if(choice == 2)
st.select();
else if(choice ==3)
st.insert();
st.print();
}
}
Explanation / Answer
Sort.java
import java.util.Scanner;
public class Sort {
//Declaring variables
int size;
int arr[];
//Zero argumented constructor
public Sort() {}
//This method will display the elements in the array after sorting
public void print() {
System.out.println("Displaying the Numbers :");
for (int i = 0; i < size; i++) {
System.out.print(arr[i] + " ");
}
}
/* This method will sort the elements in the
* array using bubble sort algorithm
*/
public void bubble() {
//This Logic will Sort the Array of elements in Ascending order
int temp;
for (int i = 0; i < arr.length; i++) {
for (int j = 1; j < (arr.length - i); j++) {
if (arr[j - 1] > arr[j]) {
temp = arr[j - 1];
arr[j - 1] = arr[j];
arr[j] = temp;
}
}
}
}
/* This method will sort the elements in the
* array using selection sort algorithm
*/
public void select() {
int small;
for (int i = 0; i < arr.length; i++) {
int m = i;
for (int j = i + 1; j < arr.length; j++) {
if (arr[j] < arr[m])
m = j;
}
small = arr[m];
arr[m] = arr[i];
arr[i] = small;
}
}
/* This method will sort the elements in the
* array using insert sort algorithm
*/
public void insert() {
for (int m = 1; m < arr.length; m++) {
int val = arr[m];
int n = m - 1;
while ((n > -1) && (arr[n] > val)) {
arr[n + 1] = arr[n];
n--;
}
arr[n + 1] = val;
}
}
public void getData() {
/*
* Creating an Scanner class object which is used to get the inputs
* entered by the user
*/
Scanner sc = new Scanner(System.in);
System.out.print("How many no of Elements you want to enter :");
size = sc.nextInt();
arr = new int[size];
for (int i = 0; i < size; i++) {
System.out.print("Enter number#" + (i + 1) + ":");
arr[i] = sc.nextInt();
}
}
}
______________________
MA5331550.java
import java.io.IOException;
import java.util.Scanner;
/**
* 09/17/2017 Dakota Mammedaty MA5331550 Bubble sorted
*/
public class MA5331550 {
public static void main(String[] args) throws IOException {
//Creating the Sort class Objects
Sort st = new Sort();
//Calling the method which will get the inputs from the user
st.getData();
//Displaying the menu
System.out.println("=================Sorting Algorithms=================");
System.out.println("1. Bubble Sort");
System.out.println("2. Selection Sort");
System.out.println("3. Insertion Sort");
System.out.println("Enter your choice");
//getting the choice entered by the user
Scanner scan = new Scanner(System.in);
int choice = scan.nextInt();
//Based on the user choice the corresponding block of code will executed
if (choice == 1)
//calling the method on the Sort class
st.bubble();
else if (choice == 2)
//calling the method on the Sort class
st.select();
else if (choice == 3)
//calling the method on the Sort class
st.insert();
/* calling the method on the Sort class
* to display the numbers after sorting
*/
st.print();
}
}
________________________
Output:
How many no of Elements you want to enter :10
Enter number#1:34
Enter number#2:56
Enter number#3:11
Enter number#4:9
Enter number#5:8
Enter number#6:3
Enter number#7:59
Enter number#8:90
Enter number#9:81
Enter number#10:17
=================Sorting Algorithms=================
1. Bubble Sort
2. Selection Sort
3. Insertion Sort
Enter your choice
1
Displaying the Numbers :
3 8 9 11 17 34 56 59 81 90
_____________Could you rate me well.Plz .Thank You