Please help me write this code in Java 8 . Please make sure that the code meets
ID: 3743966 • Letter: P
Question
Please help me write this code in Java 8. Please make sure that the code meets all the needed requirements mentioned in the question under each part such as in INPUT FORMAT, OUTPUT FORMAT, AND CONSTRAINTS. Can you also make sure to comment the code? Please include screenshots of your input and output, which should match the ones in the question. Thank You!
This is the sample input that a user will input:
The output for the SELECTION SORT algorithm with the above input will be:
The output for the INSERTION SORT algorithm with the above input will be:
The output for the BUBBLE SORT algorithm with the above input will be:
Explanation / Answer
Please find belowmentioned code with comments and sample output for your reference.
Please get back to me if incase you need any further help,Thank you.
Code:
package learning;
import java.util.Arrays;
import java.util.Scanner;
public class SortingStudents {
public static void main(String[] args) {
//Variable to store input size
int size;
//Scanner object to take input from user
Scanner in = new Scanner(System.in);
System.out.println("Enter the size");
size = in.nextInt();
//Students array to store details of Students
Student students[] = new Student[size];
System.out.println("Enter student details");
//For loop totake details of students from user
for(int i=0;i<size;i++)
{
System.out.println("Enter first name");
String fname = in.next();
System.out.println("Enter last name");
String lname = in.next();
System.out.println("Enter graduation year");
int gYear = in.nextInt();
students[i]= new Student(fname,lname,gYear);
}
//print the original array before sorting
Print p = new Print();
p.print(students,size);
System.out.println("**************************BUBBLE SORT********************");
//calling bubble sort method on temporary array
BubbleSort bs = new BubbleSort();
Student[] temp = Arrays.copyOf(students, size);
bs.bubbleSort(temp);
//p.print(temp,size);
System.out.println("**************************SELECTION SORT********************");
//calling selection sort method on temporary array
SelectionSort ss = new SelectionSort();
temp = Arrays.copyOf(students, size);
ss.selectionSort(temp);
//p.print(temp,size);
System.out.println("**************************INSERTION SORT********************");
//calling insertion sort method on temporary array
InsertionSort is = new InsertionSort();
temp = Arrays.copyOf(students, size);
is.insertionSort(temp);
//p.print(temp,size);
// print the original array
// for(int r=0;r<size;r++)
// {
// System.out.println(students[r].toString());
// }
}
}
//Student class to store details od each student like firstname,last name and graduation year
class Student implements Comparable<Student>{
//class Variabes
String fName;
String lName;
Integer gYear;
//Getter and setter methods
public String getfName() {
return fName;
}
public void setfName(String fName) {
this.fName = fName;
}
public String getlName() {
return lName;
}
public void setlName(String lName) {
this.lName = lName;
}
public Integer getgYear() {
return gYear;
}
public void setgYear(Integer gYear) {
this.gYear = gYear;
}
//Constructor with parameters
public Student(String fName, String lName, Integer gYear) {
super();
this.fName = fName;
this.lName = lName;
this.gYear = gYear;
}
//Default constructor
public Student() {
super();
}
//Tostring method to print details of student
@Override
public String toString() {
return fName + " " + lName + " " + gYear;
}
//ComapreTo method to compare 2 students based on student variables
//in the order of graduation year,first name and then last name
@Override
public int compareTo(Student s) {
if(this.gYear < s.getgYear())
return -1;
else if(this.gYear > s.getgYear())
return 1;
else
{
if(this.fName.compareTo(s.fName) < 0)
return -1;
else if(this.fName.compareTo(s.fName) > 0)
return 1;
else
{
if(this.lName.compareTo(s.lName) < 0)
return -1;
else if(this.lName.compareTo(s.lName) > 0)
return 1;
else
return 0;
}
}
}
}
//Bubble sort class for sorting students array using bubblesort
class BubbleSort
{
Print p = new Print();
//bubblesort method to sort array using bubble sort
void bubbleSort(Student[] students)
{
int n = students.length;
Student temp = new Student();
for(int i=0; i < n; i++){
//for each student we will compare it with remaing elements in the array succeeding this and swap if required
for(int j=1; j < (n-i); j++){
if(students[j-1] .compareTo( students[j]) > 0){
//swap elements
temp = students[j-1];
students[j-1] = students[j];
students[j] = temp;
}
System.out.println("After iteration "+i+j);
p.print(students,n);
}
}
p.print(students,n);}
}
//Selection sort class for sorting students array using selectionsort
class SelectionSort{
Print p = new Print();
//selection sort method to sort array using selection sort
void selectionSort(Student[] students)
{
int n = students.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 (students[j] .compareTo(students[min_idx]) < 0)
min_idx = j;
// Swap the found minimum element with the first
// element
Student temp = students[min_idx];
students[min_idx] = students[i];
students[i] = temp;
System.out.println("After iteration ");
p.print(students,n);
}
p.print(students,n);
}
}
//Insertion Sort class for sorting students array using insertion sort
class InsertionSort
{
Print p = new Print();
/*Function to sort array using insertion sort*/
void insertionSort(Student[] students)
{
int n = students.length;
for (int i=1; i<n; ++i)
{
Student key = students[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 && students[j].compareTo(key)>0)
{
students[j+1] = students[j];
j = j-1;
}
students[j+1] = key;
System.out.println("After iteration ");
p.print(students,n);
}
p.print(students,n);
}
}
class Print{
void print(Student[] students,int size)
{
System.out.println("************PRINTING THE ARRAY*********************");
for(int r=0;r<size;r++)
{
System.out.println(students[r].toString());
}
System.out.println("*********************DONE**************************");
}
}
Sample output:
Enter the size
4
Enter student details
Enter first name
ak
Enter last name
pe
Enter graduation year
2014
Enter first name
ak
Enter last name
pe
Enter graduation year
2013
Enter first name
ab
Enter last name
ab
Enter graduation year
2013
Enter first name
ab
Enter last name
aa
Enter graduation year
2013
************PRINTING THE ARRAY*********************
ak pe 2014
ak pe 2013
ab ab 2013
ab aa 2013
*********************DONE**************************
**************************BUBBLE SORT********************
After iteration 01
************PRINTING THE ARRAY*********************
ak pe 2013
ak pe 2014
ab ab 2013
ab aa 2013
*********************DONE**************************
After iteration 02
************PRINTING THE ARRAY*********************
ak pe 2013
ab ab 2013
ak pe 2014
ab aa 2013
*********************DONE**************************
After iteration 03
************PRINTING THE ARRAY*********************
ak pe 2013
ab ab 2013
ab aa 2013
ak pe 2014
*********************DONE**************************
After iteration 11
************PRINTING THE ARRAY*********************
ab ab 2013
ak pe 2013
ab aa 2013
ak pe 2014
*********************DONE**************************
After iteration 12
************PRINTING THE ARRAY*********************
ab ab 2013
ab aa 2013
ak pe 2013
ak pe 2014
*********************DONE**************************
After iteration 21
************PRINTING THE ARRAY*********************
ab aa 2013
ab ab 2013
ak pe 2013
ak pe 2014
*********************DONE**************************
************PRINTING THE ARRAY*********************
ab aa 2013
ab ab 2013
ak pe 2013
ak pe 2014
*********************DONE**************************
**************************SELECTION SORT********************
After iteration
************PRINTING THE ARRAY*********************
ab aa 2013
ak pe 2013
ab ab 2013
ak pe 2014
*********************DONE**************************
After iteration
************PRINTING THE ARRAY*********************
ab aa 2013
ab ab 2013
ak pe 2013
ak pe 2014
*********************DONE**************************
After iteration
************PRINTING THE ARRAY*********************
ab aa 2013
ab ab 2013
ak pe 2013
ak pe 2014
*********************DONE**************************
************PRINTING THE ARRAY*********************
ab aa 2013
ab ab 2013
ak pe 2013
ak pe 2014
*********************DONE**************************
**************************INSERTION SORT********************
After iteration
************PRINTING THE ARRAY*********************
ak pe 2013
ak pe 2014
ab ab 2013
ab aa 2013
*********************DONE**************************
After iteration
************PRINTING THE ARRAY*********************
ab ab 2013
ak pe 2013
ak pe 2014
ab aa 2013
*********************DONE**************************
After iteration
************PRINTING THE ARRAY*********************
ab aa 2013
ab ab 2013
ak pe 2013
ak pe 2014
*********************DONE**************************
************PRINTING THE ARRAY*********************
ab aa 2013
ab ab 2013
ak pe 2013
ak pe 2014
*********************DONE**************************
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.