Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

For this tutorial, you will only need two classes. First Class: • The first clas

ID: 3724659 • Letter: F

Question

For this tutorial, you will only need two classes.
First Class:
• The first class is called Student this class will implement Comparable (see the code
below).
• Class student will have three private fields String studentName, int studentID, int
studetnAge.
• Class student will also have a constructor that takes in all three of those parameters to
create an instance of student.
• For this class I am not requiring a default constructor, but you need getter and setter
methods for each field.
• If you look into the Comprarable interface (remember press control and hover over
Comparable then left click twice and you can view the interface code to know what you
need to implement) you only need to implement one method called compareTo().
• The compareTo method should sort the students by age
o This means you need to get the student age and compare it to another age. (see
code below)
• Lastly, you should override your toString method similar to assignment 2 so that you can
output the studentID, Name, and Age on one line.

The second class will be your Main class which will contain your main method (where all
the output comes out from).
• In this class you will initialize at least three students and pass the Name, Student ID, and
Age to each instance being initialized.
• After that you will create an ArrayList of type student, and insert the instances of Student
into your ArrayList (Hint: arrayListVariableName.add(student variable name).
• Now you can use the Collections.sort(arraylist) method to sort your students by
ascending age.
• Lastly utilize a for loop in order to iterate through the arraylist and print out each
element. (hint: this is just like GenericsDemo2 except instead of a generic array we are
utilizing an ArrayList of type student.

Code:
public class Student implements Comparable {
//place fields here
public Student(int studentID, String studentName, int studentAge) {
//complete constructor here
}
//getter methods and setter methods go here
//This Override method is required to implement Comparable sort the student by age in
ascending order
@Override
public int compareTo(Student compareStudent) {
//(Student) here is the casting, getStudentAge should be a getter method for the age field.
int compareAge = ((Student) compareStudent).getStudentAge();
//this is the return for ascending order, since you are new to sorting I gave you the methodology
//you no longer have to write any code for this method.
return this.studentage – compareAge;
}
@Override
public String toString(){
return //fill in code here and remember the return is the studentID, the student name, and the
studentAge on one line
}//end of the toString method bracket
}//end of the Student class bracket
public class Main {
public static void main(String args[]){
//Create ArrayList of type student
//Create 3 or more students (make sure the age is unique, so we can test the sorting algorithm).
You need to use the new keyword here.
//Use the Collections.sort(ArraList) method, remember for this to work you have to import
java.util.*;
//Use a for loop in order to output each element in the array list.
}//bracket that closes off your main method
}//bracket that closes off your Main class.

Explanation / Answer

public class Student implements Comparable {
String studentName;
int studentID;
int studetnAge;

public Student(int studentID, String studentName, int studentAge) {
this.studentID = studentID;
this.studentName = studentName;
this.studetnAge = studetnAge;
}

   public String getStudentName() {
       return studentName;
   }
   public void setStudentName(String studentName) {
       this.studentName = studentName;
   }
   public int getStudentID() {
       return studentID;
   }
   public void setStudentID(int studentID) {
       this.studentID = studentID;
   }
   public int getStudetnAge() {
       return studetnAge;
   }
   public void setStudetnAge(int studetnAge) {
       this.studetnAge = studetnAge;
   }
  
//This Override method is required to implement Comparable sort the student by age in
ascending order
@Override
public int compareTo(Student compareStudent) {
//(Student) here is the casting, getStudentAge should be a getter method for the age field.
int compareAge = ((Student) compareStudent).getStudentAge();
//this is the return for ascending order, since you are new to sorting I gave you the methodology
//you no longer have to write any code for this method.
return this.studentage – compareAge;
}

@Override
public String toString() {
   return "Student [studentID=" + studentID + ", studentName=" + studentName + ", studetnAge=" + studetnAge + "]";
}//end of the toString method bracket
}//end of the Student class bracket

import java.util.*;
public class Main {
public static void main(String args[]){
//Create ArrayList of type student
List<Student> students = new ArrayList<Student>();
//Create 3 or more students (make sure the age is unique, so we can test the sorting algorithm).
students.add(new Student(1323, "Chandan", 10));
students.add(new Student(100, "Manish", 8));
students.add(new Student(35, "Kumar", 12));
students.add(new Student(589, "Gaurav", 16));

//Use the Collections.sort(ArraList) method, remember for this to work you have to import java.util.*;
Collections.sort(students, new Student());
//Use a for loop in order to output each element in the array list.
for(Student student: students{
   System.out.println("Student Id: " + student.getStudentId());
   System.out.println("Student Name: " + student.getStudentName());
   System.out.println("Student Age: " + student.getStudentAge());
   System.out.println("-------------------------------------------------");
}
}//bracket that closes off your main method
}//bracket that closes off your Main class.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote