Interface and implements

Hello guys ,

I am new to java bassically I used to C++ and C#

I stuck with a little problem here :

I have to make a new interface class Comparable
to make a Student class that imlements comparable
and to make an alghorithm that will recieve an array of Comparale (s) (that will be a students ) and sort them

Comparable need to have only one function

int compareto

can someone please help me how do I make this interface
and student classes that the alghoritm will sort students ??

First of all: remember to add [code][/code] tags around source code. By doing that the code stays formatted and does not left allign.

Second, I think there are some design flaws in your code.

1. Your comparable interface should be usable for many, if not any, classes. Using a specific class in the compareTo method might not be the best thing to do.
2. Do you need 2 arguments in the compareTo method? Inside the implementation you compare two students but you do nothing with the student data of the object self.
3. Are you sure you want to ask for the data of a student in its constructor? Consider this: what happens if the student class is used in a GUI application? Does it still work then?

This is your first java homework? And they immediately start with interfaces... strange (IMHO).

Never the less,

1. Do you know the root of all Java classes? I suggest you use that class in the method of your interface.
2. I really cannot believe they want that! I should stick with student1.compareTo(student2).
3. Okay, so you are only using the console. But even then it is not a very good idea to put the interaction in the constructor. Why don't you create a constructor with two arguments: name and id. Create a new class StudentApp for example that contains the main entry, reads the data and creates the Student objects?

They want two arguments because that way you can't simply copy the existing Comparable interface. It's not much, but I suspect that's the idea. There's no point telling the OP to do something different to the requirements in a uni project. The best they can do is question it with the tutor.

Anyway, to the OP. What errors are you getting? You haven't actually told us.
I think I can see them, but I want you to actually tell us.

Normally the Comparable<T> interface is implemented by classes that need to compare objects with object of the same class while the Comparator<T> interface is implemented by a class that needs to compare classes (objects) of possibly another type.

There is nothing wrong with your interface and implementation thereof per se but note that your Comparable interface has nothing to do with the 'standard' Comparable interface. Your implementation class(es) will not considered to be implementations of the (standard) interface though.

I reciving an error here ... it tells that student1 && student2 doesn't have any getName etc

The compiler only knows that student1 and student2 are of type Comparable; it doesn't know that that particular interface is implemented by your Student class. Cast both parameters to type Student (because they actually are). You have to do it, the compiler is completely ignorant here:

You're welcome of course; don't think too much of the 'intelligence' of the compiler; it can't assume anything, all that it knows is that your Student class implements your Comparable interface so it implements the method 'comareTo' that takes to Comparables; it doesn't know that these Comparables are always Students (no matter the fact that this class implements that interface). Also, consider implementing the 'standard' Comparable interface (with a one argument compareTo( ... ) method), it avoids the silly use I showed in one of my previous replies.