Monday, September 28, 2020

#290 Assume you have class Student that

Assume you have class Student that - Computer Science

ChemistryExplain daily providing Q&A content “#290 Assume you have class Student that" in Computer science, Ba computer science, Berkeley computer science, Computer science associate degree jobs

ChemistryExplain “#290 Assume you have class Student that in Computer science, Ba computer science, Berkeley computer science, Computer science
Get the Free Online Chemistry Q&A Questions And Answers with explain. To crack any examinations and Interview tests these Chemistry Questions And Answers are very useful. Here we have uploaded the Free Online Chemistry Questions. Here we are also given the all chemistry topic.

 ChemistryExplain team has covered all Topics related to inorganic, organic, physical chemistry, and others So, Prepare these Chemistry Questions and Answers with Explanation Pdf.

For More Chegg Questions

Free Chegg Question

Assume you have class Student that has id (int), name(String), gpa(double), update the sorting algorithms above to receive an array of students, and sort them based on the gpa.

Please use java eclipse.

These are the sorting algorithms I must use:

public static void bubbleSort(int[] a) {
       int outer, inner;
       for (outer = a.length - 1; outer > 0; outer--) { // counting down
           boolean sorted=true;
           for (inner = 0; inner < outer; inner++) { // bubbling up
               if (a[inner] > a[inner + 1]) { // if out of order...
                   int temp = a[inner]; // ...then swap
                   a[inner] = a[inner + 1];
                   a[inner + 1] = temp;
                   sorted=false;
               }
           }
           if (sorted)
               break;
       }
   }

   public static void selectionSort(int[] a) {
       int outer, inner, min;
       for (outer = 0; outer < a.length - 1; outer++) { // outer counts down
           min = outer;
           for (inner = outer + 1; inner < a.length; inner++)
               if (a[inner] < a[min])
                   min = inner;
           int temp = a[outer];
           a[outer] = a[min];
           a[min] = temp;
       }
   }

   public static void insertionSort(int[] array) {
       int inner, outer;
       for (outer = 1; outer < array.length; outer++) {
           int temp = array[outer];
           inner = outer;
           while (inner > 0 && array[inner - 1] >= temp) {
               array[inner] = array[inner - 1];
               inner--;
           }
           array[inner] = temp;

       }
   }

For More Chemistry Notes and Helpful Content Subscribe Our YouTube Chanel - Chemistry Explain  

Free Chegg Answer

all_sorting.java

import java.util.Random;

public class all_sorting {

        public static void bubbleSort(Student[] arrStud) {
                int outer, inner;
                int len = arrStud.length;
                for (outer = len - 1; outer > 0; outer--) { // counting down
                        boolean sorted = true;
                        for (inner = 0; inner < outer; inner++) { // bubbling up
                          if (arrStud[inner].getGpa() > arrStud[inner + 1].getGpa()) { // if out of order...

                                        Student temp = arrStud[inner]; // ...then swap
                                        arrStud[inner] = arrStud[inner + 1];
                                        arrStud[inner + 1] = temp;
                                        sorted = false;
                                }
                        }
                        if (sorted)
                                break;
                }

                // print the value
                print(arrStud);
        }

        public static void selectionSort(Student[] arrStud) {
                int outer, inner, min;
                int len = arrStud.length;
                for (outer = 0; outer < len - 1; outer++) { // outer counts down
                        min = outer;
                        for (inner = outer + 1; inner < len; inner++)
                                if (arrStud[inner].getGpa() < arrStud[min].getGpa())
                                        min = inner;
                        Student temp = arrStud[outer];
                        arrStud[outer] = arrStud[min];
                        arrStud[min] = temp;
                }

                // print the value
                print(arrStud);
        }

        public static void insertionSort(Student[] arrStud) {
                int inner, outer;
                int len = arrStud.length;
                for (outer = 1; outer < len; outer++) {
                        Student temp = arrStud[outer];
                        inner = outer;
                        while (inner > 0 && arrStud[inner - 1].getGpa() >= temp.getGpa()) {
                                arrStud[inner] = arrStud[inner - 1];
                                inner--;
                        }
                        arrStud[inner] = temp;
                }

                // print the value
                print(arrStud);
        }

        public static void print(Student[] arrStud) {
                int len = arrStud.length;

                for (int i = 0; i < len; i++)
          System.out.println(arrStud[i].getGpa() + " " + arrStud[i].getId() + " " + arrStud[i].getName());
                System.out.println();
        }

        public static Student[] generateRandom_Gpa() {
                int n = 15;
                Student[] student = new Student[n];
                
                // generate random double number
                Random random = new Random();

                // generate number between 0 and 100
                int rangeMin = 0, rangeMax = 100; 

                for (int i = 0; i < n; i++) {
                        String name = "Rahul";

                        double gpa = rangeMin + (rangeMax - rangeMin) * random.nextDouble();
                        
                        // for 2 decimal value to easy to read
                        gpa = Math.round(gpa * 100.0) / 100.0;
                        student[i] = new Student(i, name, gpa);
                }
                return student;
        }

        public static void main(String[] args) {
        
       // we generate random number just for check code work properly or not
                System.out.println("Bubble Sort: ");
                all_sorting.bubbleSort(generateRandom_Gpa());

                System.out.println("Selection Sort: ");
                all_sorting.selectionSort(generateRandom_Gpa());

                System.out.println("Insertion Sort: ");
                all_sorting.insertionSort(generateRandom_Gpa());

        }
}

Student.java


public class Student {
        private int id;
        private String name;
        private double gpa;

        public Student(int id, String name, double gpa) {
                super();
                this.id = id;
                this.name = name;
                this.gpa = gpa;
        }

        public int getId() {
                return id;
        }

        public void setId(int id) {
                this.id = id;
        }

        public String getName() {
                return name;
        }

        public void setName(String name) {
                this.name = name;
        }

        public double getGpa() {
                return gpa;
        }

        public void setGpa(double gpa) {
                this.gpa = gpa;
        }
}

here we also generate random double value just to check all sorting algorithm work properly as it sorted according to

the gpa value.

add three more function

main() : To run all three sort function.

print() : to print Array of Student (Student[]) data

generateRandom_Gpa() : here we generate random value and store in Student array at index i, using

Student class constuctor.

Labels: , ,

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home