-2

I have to create class SorterDemo with following mechanisms.

sortDouble - sort array of double variables.

sortString - sort given String value in alphabetical manner.

sortStudent - sort array of Student objects based on marks.

I need to sort these functions into single main class SorterDemo Since I'm using comparator operator, at the time of using it in SorterDemo class, it's not allowing me to change.

macboyme95
  • 21
  • 1
  • 9
  • Please be more specific: post the exact error message or exception stack trace (and tell us to which line that matches in your source code). In other words: please visit the help center and learn how to ask good questions. – GhostCat Jul 04 '16 at 17:40
  • 1
    It's Arrays.sort(arr) not Array.sort(arr) – paisanco Jul 04 '16 at 17:40
  • And also instead of this: public String sortString(String str){ String s=str; char[] arr=s.toCharArray(); Array.sort(arr); String str=String.copyValueOf(arr); return str; } you can just say "char[] arr = str.toCharArray(); and then after sort str = String.copyValueOf(arr) return str; You are creating two uneccessary strings right there. – creativecreatorormaybenot Jul 04 '16 at 17:43
  • on `sort str = String.copyValueOf(arr);` its saying `Multiple markers at this line - Duplicate local variable str - sort cannot be resolved to a type` – macboyme95 Jul 04 '16 at 17:54
  • @Jägermeister At lline `Student[] stud=new Student[5];` and ` stud[i]=new Student(rollno,name,mk);` The error is `Student[] stud=new Student[5];`. it's asking to create the `class Student` but I have already created the class. – macboyme95 Jul 04 '16 at 17:55
  • are you talking about the code in the question or the code in your source file ? – niceman Jul 04 '16 at 22:52
  • @niceman I have updated the code. It works fine now. Just need to implement all those functions into single main class `SorterDemo`. Thanks. – macboyme95 Jul 04 '16 at 23:05
  • @niceman Can you please tell me an approach to do so? or simple example where `implement` opearator is used would suffice. – macboyme95 Jul 04 '16 at 23:27

1 Answers1

0

You want them to be all inside SorterDemo class, well you can put sortString class and sortStudent class inside SorterDemo class as nested classes(you put static before them).

A better approach though is to have something like this :

Class SorterDemo
{      
    static void sortString(Student[] stud)
    {
        Arrays.sort(stud,Student.namecomparator);
    }
    static void sortStudent(Student[] stud)
    {
        Arrays.sort(stud,Student.markscomparator);
    }
    public static void main(String[] args)
    {
        Student student[] = new Student[3];



        // initializing unsorted double array
        double dArr[] = { 3.2, 1.2, 9.7, 6.2, 4.5 };
        // let us print all the elements available in list
        for (double number : dArr) {
            System.out.println("Number = " + number);
        }

        // sorting array
        Arrays.sort(dArr);

        // let us print all the elements available in list
        System.out.println("The sorted double array is:");
        for (double number : dArr) {
            System.out.println("Number = " + number);
        }

        student[0] = new Student();
        student[0].setName("Nick");
        student[0].setmarks(19);

        student[1] = new Student();
        student[1].setName("Helen");
        student[1].setmarks(12);

        student[2] = new Student();
        student[2].setName("Ross");
        student[2].setmarks(16);

        System.out.println("Order of students before sorting is: ");

        for (int i = 0; i < student.length; i++) {
            System.out.println(student[i].getName() + "\t" + student[i].getmarks());
        }

        sortStudent(student);
        System.out.println("Order of students after sorting by student marks is");

        for (int i = 0; i < student.length; i++) {
            System.out.println(student[i].getName() + "\t" + student[i].getmarks());
        }

        sortString(student);
        System.out.println("Order of students after sorting by student name is");

        for (int i = 0; i < student.length; i++) {
            System.out.println(student[i].getName() + "\t" + student[i].getmarks());
        }
    }
}
class Student
{
    static Comparator<Student> namecomparator=(s1,s2)->s1.getName().compareTo(s2.getName());
    static Comparator<Student> markscomparator=(s1,s2)->Integer.compare(s1.getmarks(),s2.getmarks());
    //the rest of class 
}

As you can see We're defining two comparators as static fields of Student class, this talks like "we have two ways to compare Students : by name and by marks", we assign each comparator a lambda expression.

A lambda expression makes it easy to add implementations for interfaces that has only one method to implement(like Comparator) the syntax is like this : (par1,par2,...)->expr or (par1,par2,...)->{//block of code}, interface Runnable for example only has one method which is void run so we can define a Runnable variable like this : Runnable r=()->{//implementation for run method}.

Note that lambda expressions were introduced in java 8, in java 7 you use anonymous classes(classes without name) like this :

static Comparater<Student> namecomparator=new Comparator<Student>
{
    @Override
    public int compare(Student s1,Student s2)
    {
        return s1.getName().compareTo(s2.getName());
    }
};

lambda expressions or anonymous classes relieves us from defining classes just for purpose of implementing interfaces, you must use anonymous classes even in java 8 if the interface has more than one method to implement.

It is reasonable to define a named class that implements interface A only when that class has many things besides what A has, that's my opinion at least :).

Note

There is Comparable interface, you can make Student class implement that interface, more on the difference between Comparable and Comparator can be found here

Community
  • 1
  • 1
niceman
  • 2,653
  • 29
  • 57
  • Thanks. Source code updated. It's working fine. Just that I'm unable to call those static functions for printing. Tried `Arrays.sort(student, new sortStudent());` Similar for calling SortString(). – macboyme95 Jul 05 '16 at 13:47