Question

Create a Java program that will store 10 student objects in an ArrayList, ArrayList<Student>. A student object con...

Create a Java program that will store 10 student objects in an ArrayList, ArrayList<Student>. A student object consists of the following fields:

  • int rollno
  • String name
  • String address

Implement two comparator classes to sort student objects by name and by rollno (roll number). Implement your own selection sort method and place your code in a separate Java source file. Do not use a sort method from the Java collections library.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

CODE

import java.util.ArrayList;

import java.util.Comparator;

import java.util.Random;

class Student {

int rollno;

String name;

String address;

public Student(int rollno, String name, String address) {

super();

this.rollno = rollno;

this.name = name;

this.address = address;

}

@Override

public String toString() {

return "Student [rollno=" + rollno + ", name=" + name + ", address=" + address + "]";

}

}

class Sortbyroll implements Comparator<Student>

{

// Used for sorting in ascending order of

// roll number

public int compare(Student a, Student b)

{

return a.rollno - b.rollno;

}

}

class Sortbyname implements Comparator<Student>

{

// Used for sorting in ascending order of

// roll name

public int compare(Student a, Student b)

{

return a.name.compareTo(b.name);

}

}

public class Main {

public static void sort(ArrayList<Student> students)

{

Sortbyroll sortbyroll = new Sortbyroll();

int n = students.size();

// 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 (sortbyroll.compare(students.get(j), students.get(min_idx)) < 0)

min_idx = j;

// Swap the found minimum element with the first

// element

Student temp = students.get(min_idx);

students.set(min_idx, students.get(i));

students.set(i, temp);

}

}

public static void main(String[] args) {

Random rand = new Random();

ArrayList<Student> students = new ArrayList<>();

for (int i=0; i<10; i++) {

int roll = rand.nextInt(100);

String name = "Name" + i;

String address = "Address" + i;

students.add(new Student(roll, name, address));

}

sort(students);

System.out.println("The sorted students are:");

for (Student s : students) {

System.out.println(s);

}

}

}

​​​​​​​? Problems @ Javadoc Declaration Search Console X sterminated> StackListBased [Java Application] /Library/Java/JavaVirtualMa

Add a comment
Know the answer?
Add Answer to:
Create a Java program that will store 10 student objects in an ArrayList, ArrayList<Student>. A student object con...
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. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • 1. Do the following a. Write a class Student that has the following attributes: - name:...

    1. Do the following a. Write a class Student that has the following attributes: - name: String, the student's name ("Last, First" format) - enrollment date (a Date object) The Student class provides a constructor that saves the student's name and enrollment date. Student(String name, Date whenEnrolled) The Student class provides accessors for the name and enrollment date. Make sure the class is immutable. Be careful with that Date field -- remember what to do when sharing mutable instance variables...

  • Write a Java program which will store, manipulate, and print student registration information. As part of...

    Write a Java program which will store, manipulate, and print student registration information. As part of the solution, identify the following classes: Student Admissions. The class Student must have the following fields – Name, Address, Id number, Courses, and Date, where: Name is a user defined class comprising of at minimum first name and last name. Address is a user defined class comprising of fields - street, city, state, and zip code. Date is a predefined class in the java.util...

  • Java Task 2: Write a program where you create an ArrayList object that can store letters....

    Java Task 2: Write a program where you create an ArrayList object that can store letters. Show how can you add a letter to the ArrayList, remove a letter from theArrayList, replace a letter in the ArrayList, insert a letter in a specific location in the ArrayList, and display all the letters in the ArrayList. Task 3: Write a program where you create an ArrayList object that can store Circle object (you created circle class in your previous PAs). Create...

  • java Create the following classes: DatabaseType: an interface that contains one method 1. Comparator getComparatorByTrait(String trait)...

    java Create the following classes: DatabaseType: an interface that contains one method 1. Comparator getComparatorByTrait(String trait) where Comparator is an interface in java.util. Database: a class that limits the types it can store to DatabaseTypes. The database will store the data in nodes, just like a linked list. The database will also let the user create an index for the database. An index is a sorted array (or in our case, a sorted ArrayList) of the data so that searches...

  • ONLY NEED THE DRIVER CLASS PLEASE!!! Domain & Comparator Classes: 0.) Design a hierarchy of classes,...

    ONLY NEED THE DRIVER CLASS PLEASE!!! Domain & Comparator Classes: 0.) Design a hierarchy of classes, where the Media superclass has the artistName and the mediaName as the common attributes. Create a subclass called CDMedia that has the additional arrayList of String objects containing the songs per album. Create another subclass called DVDMedia that has the additional year the movie came out, and an arrayList of co-stars for the movie. 1.) The superclass Media will implement Comparable, and will define...

  • 3. Create a program named TestArrayList, and declare an ArrayList with an initial capacity of 10....

    3. Create a program named TestArrayList, and declare an ArrayList with an initial capacity of 10. Insert one copy of the string "Python", five copies of the string "Java" followed by four copies of the string "C++" so that your vector is now filled to сараcity. Add a class method named printArrayList to print out all the elements stored within an ArrayList on separate lines. Test your method to be sure it works a) Add a class method named delete...

  • Implement an abstract class named Person and two subclasses named Student and Staff in Java. A person has a name, address, phone number and e-mail address. A student has a credit hour status (freshman...

    Implement an abstract class named Person and two subclasses named Student and Staff in Java. A person has a name, address, phone number and e-mail address. A student has a credit hour status (freshman, sophomore, junior, or senior). Define the possible status values using an enum. Staff has an office, salaray, and date-hired. Implement the above classes in Java. Provide Constructors for classes to initialize private variables. Getters and setters should only be provided if needed. Override the toString() method...

  • ( Object array + input) Write a Java program to meet the following requirements: 1. Define...

    ( Object array + input) Write a Java program to meet the following requirements: 1. Define a class called Student which contains: 1.1 data fields: a. An integer data field contains student id b. Two String data fields named firstname and lastname c. A String data field contains student’s email address 1.2 methods: a. A no-arg constructor that will create a default student object. b. A constructor that creates a student with the specified student id, firstname, lastname and email_address...

  • Java Project In Brief... For this Java project, you will create a Java program for a...

    Java Project In Brief... For this Java project, you will create a Java program for a school. The purpose is to create a report containing one or more classrooms. For each classroom, the report will contain: I need a code that works, runs and the packages are working as well The room number of the classroom. The teacher and the subject assigned to the classroom. A list of students assigned to the classroom including their student id and final grade....

  • Java Framework Collection Assign. Create a class called ArrayListExample Create a ArrayList object called languageList which...

    Java Framework Collection Assign. Create a class called ArrayListExample Create a ArrayList object called languageList which accept type of String Add English, French, Italian and Arabic strings into languageList array object Print languageList using Iterator object Sort ArrayList object alphabetically Print languageList using Iterator object Solution will produce following: ArrayListExample class ArrayList object called languageList ArrayListExampleTest class with main method

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT