0

Inside Comparator_Interface.ObjectSortingOne package, I wrote exactly four classes, which are People.java, SortByName.java, SortByRoll.java and MainClass.java. Codes are given here.

package Comparator_Interface.ObjectSortingOne;

class People {      

    protected String name;
    protected int academicClass;
    protected int roll;

    People(String name, int academicClass, int roll){
        this.name = name;
        this.academicClass = academicClass;
        this.roll = roll;
    }

    @Override
    public String toString(){
        return ("Name: " + this.name + "; Class: " + this.academicClass + "; Roll: " + this.roll + "\n");
    }
    
}

package Comparator_Interface.ObjectSortingOne;

import java.util.Comparator;

class SortByName implements Comparator<People> {

    public int compare(People people1, People people2){
        return people1.name.compareTo(people2.name);
    }
}

package Comparator_Interface.ObjectSortingOne;

import java.util.Comparator;

class SortByRoll implements Comparator<People> {

    public int compare(People people1, People people2){
        return people1.roll - people2.roll;
    }
}

package Comparator_Interface.ObjectSortingOne;

import java.util.ArrayList;
import java.util.Collections;

class MainClass {

    public static void main(String[] args) {

        ArrayList<People> arrayList = new ArrayList<>();

        arrayList.add(new People("Arnab Das", 10, 03));
        arrayList.add(new People("Tarek Rahman", 10, 1));
        arrayList.add(new People("Jobayer Ahmed", 10, 1));
        arrayList.add(new People("Saidis Salehin", 10, 02));
        arrayList.add(new People("Rahik Wasif", 9, 03));

        System.out.println("Unsorted arraylist: ");
        for(Object obj: arrayList){
            obj.toString();
        }

        System.out.println();

        Collections.sort(arrayList, new SortByName());

        System.out.println("Sorted by name (in ascending): ");
        for(Object obj: arrayList){
            obj.toString();
        }

        System.out.println();

        Collections.sort(arrayList, new SortByRoll());

        System.out.println("Sorted by roll (in ascending): ");
        for(Object obj: arrayList){
            obj.toString();
        }

        System.out.println();

    }
    
}

Now when I am going for compiling and running the code, it's showing error.

enter image description here

I tried multiple times by changing access modifiers, but it won't work.

user207421
  • 305,947
  • 44
  • 307
  • 483

0 Answers0