Question

Write a Java program that sorts an array of “Student” in an aescending order of their...

Write a Java program that sorts an array of “Student” in an aescending order of their “last names”. The program should be able to apply (bubble sort):

Student [] studs = new Student[8];

s[0] = new Student("Saoud", "Mohamed", 3.61);

s[1] = new Student("Abdelkader", "Farouk", 2.83);

s[2] = new Student("Beshr" , "Alsharqawy", 1.99);

s[3] = new Student("Nader", "Salah", 3.02);

s[4] = new Student("Basem", "Hawary", 2.65);

s[5] = new Student("Abdullah", "Babaker", 2.88);

s[6] = new Student("Abdelaal", "Khairy", 3.13);

s[7] = new Student("Mohamedain", "Marsily", 4.00);

Homework Answers

Answer #1

Code:

class Student//student class
{
   String firstname, lastname;double gpa;
   public Student(String firstname, String lastname, double gpa)//constructor
   {
       this.firstname = firstname;this.lastname = lastname;this.gpa = gpa;
   }
   public String getFirstName()//getter methods
   {
       return firstname;
   }
   public String getLastName()
   {
       return lastname;
   }
   public double getGPA()
   {
       return gpa;
   }
   public void setFirstName(String firstname)//setter methods
   {
       this.firstname = firstname;
   }
   public void setLastName(String lastname)
   {
       this.lastname = lastname;
   }
   public void setGPA(double gpa)
   {
       this.gpa = gpa;
   }
}
public class A
{
   public static void main(String args[])
   {
       Student []studs = new Student[8];int i, j, n=8;//n holds no of objects
       studs[0] = new Student("Saoud", "Mohamed", 3.61);//creating objects
       studs[1] = new Student("Abdelkader", "Farouk", 2.83);
       studs[2] = new Student("Beshr" , "Alsharqawy", 1.99);
       studs[3] = new Student("Nader", "Salah", 3.02);
       studs[4] = new Student("Basem", "Hawary", 2.65);
       studs[5] = new Student("Abdullah", "Babaker", 2.88);
       studs[6] = new Student("Abdelaal", "Khairy", 3.13);
       studs[7] = new Student("Mohamedain", "Marsily", 4.00);
       for(i=0;i<n-1;i++)//bubble sort
       {
           for(j=0;j<n-i-1;j++)
           {
               if((studs[j].getLastName()).compareTo(studs[j+1].getLastName()) > 0)
               {
                   String f = studs[j].getFirstName();//swap object studs[j]'s contents with that of object studs[j+1]
                   String l = studs[j].getLastName();
                   double g = studs[j].getGPA();
                   studs[j].setFirstName(studs[j+1].getFirstName());
                   studs[j].setLastName(studs[j+1].getLastName());
                   studs[j].setGPA(studs[j+1].getGPA());
                   studs[j+1].setFirstName(f);
                   studs[j+1].setLastName(l);
                   studs[j+1].setGPA(g);
               }
           }
       }
       System.out.println("After sorting:");
       for(i=0;i<n;i++)
       {
           System.out.println(studs[i].getFirstName() + " " + studs[i].getLastName() + " " + studs[i].getGPA());
       }
   }
}

Output:

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Write a Java program that sorts an array of “Student” in an aescending order of their...
Write a Java program that sorts an array of “Student” in an aescending order of their “last names”. The program should be able to apply (selection sort): Student [] studs = new Student[8]; s[0] = new Student("Saoud", "Mohamed", 3.61); s[1] = new Student("Abdelkader", "Farouk", 2.83); s[2] = new Student("Beshr" , "Alsharqawy", 1.99); s[3] = new Student("Nader", "Salah", 3.02); s[4] = new Student("Basem", "Hawary", 2.65); s[5] = new Student("Abdullah", "Babaker", 2.88); s[6] = new Student("Abdelaal", "Khairy", 3.13); s[7] = new Student("Mohamedain",...
Write a MIPS assembly program that sorts an array using bubble sort translating the C code...
Write a MIPS assembly program that sorts an array using bubble sort translating the C code int main(void) { int array[] = {10, 2, 7, 5, 15, 30, 8, 6}; // input array int arraySize = sizeof(array)/sizeof(array[0]); bool swapped = true; int j = 0; int tmp; while (swapped) { swapped = false; //Note : "j" , "arraySize - j" are optimizations to the bubble sort algorithm j++; // j= sorted elements int i=0; /* "arraySize - j" is used...
Assignment #4 – Student Ranking : In this assignment you are going to write a program...
Assignment #4 – Student Ranking : In this assignment you are going to write a program that ask user number of students in a class and their names. Number of students are limited to 100 maximum. Then, it will ask for 3 test scores of each student. The program will calculate the average of test scores for each student and display with their names. Then, it will sort the averages in descending order and display the sorted list with students’...
Java : Modify the selection sort algorithm to sort an array of integers in descending order....
Java : Modify the selection sort algorithm to sort an array of integers in descending order. describe how the skills you have gained could be applied in the field. Please don't use an already answered solution from chegg. I've unfortunately had that happen at many occasion ....... ........ sec01/SelectionSortDemo.java import java.util.Arrays; /** This program demonstrates the selection sort algorithm by sorting an array that is filled with random numbers. */ public class SelectionSortDemo { public static void main(String[] args) {...
Using JAVA For this assignment, you will analyze code that uses a file input stream and...
Using JAVA For this assignment, you will analyze code that uses a file input stream and a file output stream. Read through the linked Java™ code. In a Microsoft® Word document, answer the following questions: Could this program be run as is? If not, what is it lacking? Does this program modify the contents of an input stream? In what way? What are the results of running this code? ********************************************** CODE TO ANALYZE  ******************************************************** /********************************************************************** *   Program:   Datasort *   Purpose:   ...
Objective: Write a Java program that will use a JComboBox from which the user will select...
Objective: Write a Java program that will use a JComboBox from which the user will select to convert a temperature from either Celsius to Fahrenheit, or Fahrenheit to Celsius. The user will enter a temperature in a text field from which the conversion calculation will be made. The converted temperature will be displayed in an uneditable text field with an appropriate label. Specifications Structure your file name and class name on the following pattern: The first three letters of your...
write a program that automates the process of generating the final student report for DC faculty...
write a program that automates the process of generating the final student report for DC faculty considering the following restrictions. Consider declaring three arrays for processing the student data: studID studName studGrade The student ID is a random number generated by the 5-digit system in the range of (10000 - 99999). Create a function to assign the student ID generated in an array of numbers. Consider the following to generate the random number: Add the libraries: #include <stdlib.h> #include <ctime>...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT