Page 1 of 2 12 LastLast
Results 1 to 20 of 28
  1. #1
    Bored2 is offline Member
    Join Date
    Nov 2010
    Posts
    21
    Rep Power
    0

    Question 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 ??

  2. #2
    m00nchile is offline Senior Member
    Join Date
    Feb 2010
    Location
    Ljubljana, Slovenia
    Posts
    470
    Rep Power
    5

    Default

    Comparable is an existing interface. Look it up.
    Ever seen a dog chase its tail? Now that's an infinite loop.

  3. #3
    Bored2 is offline Member
    Join Date
    Nov 2010
    Posts
    21
    Rep Power
    0

    Default

    Yes i know , But I was told to build it myself
    (I'am learning at the university, and this is what they want )
    I googled some hours to find its source but I found not :(

    actually the comparable I have to write takes 2 arguments ..

    pleasse can you make a little example how can i make it myself ?

  4. #4
    StormyWaters is offline Senior Member
    Join Date
    Feb 2009
    Posts
    307
    Rep Power
    6

    Default

    What Is an Interface? (The Java™ Tutorials > Learning the Java Language > Object-Oriented Programming Concepts)

    Sounds pretty straight forward, Make an interface that only has one method. An example would be something similar to this
    Java Code:
    public interface Shape {
    	
    	public void draw(Graphics g);
    
    }

  5. #5
    Bored2 is offline Member
    Join Date
    Nov 2010
    Posts
    21
    Rep Power
    0

    Default

    The one i tried to do is
    public interface Comparable
    {
    // int compareTo(Student student1 , Student student2);
    int comareTo(Comparable student1,Comparable student2);
    }
    public class Student implements Comparable , Printable {

    private int id;
    private String name;

    Student ()//constructor
    {
    int tempid;
    String tempname;

    Scanner scan = new Scanner(System.in);
    System.out.println("Please enter ID");
    tempid = scan.nextInt();
    System.out.println("Enter Name");
    tempname = scan.nextLine();
    setId(tempid);
    setName(tempname);
    }
    public void setId(int id) {
    this.id = id;
    }
    public int getId() {
    return id;
    }
    public void setName(String name) {
    this.name = name;
    }
    public String getName() {
    return name;
    }
    //@Override
    public int comareTo(Comparable student1, Comparable student2) {
    return (student1).getName().compareTo((student2).getName( )) ;
    }
    }




    but it doesn't work for me :(

    I guess i missing something (I don't get how do I connect student to be comparable)

  6. #6
    Bored2 is offline Member
    Join Date
    Nov 2010
    Posts
    21
    Rep Power
    0

    Default

    hey guys please I really need a little help here .. just point me to the problem

  7. #7
    venerik is offline Member
    Join Date
    Oct 2010
    Posts
    94
    Rep Power
    0

    Default

    Hi,

    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?

    Good luck,
    Erik
    I'm new to Java but I like to help where ever I can. :)

  8. #8
    Bored2 is offline Member
    Join Date
    Nov 2010
    Posts
    21
    Rep Power
    0

    Default

    Oh Ok sorry for code /code

    1- yes and i don't understand how to do that

    2) yes i need 2 ... i know it is ridicolous but that what they want
    Java Code:
    comparable1.compareTo(comparable1,comparable2);

    3- no we work in console only it is our first java homework so ... I know a much about it .

  9. #9
    venerik is offline Member
    Join Date
    Oct 2010
    Posts
    94
    Rep Power
    0

    Default

    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?

    Good luck,
    I'm off to bed so I won't be of any help today :(
    I'm new to Java but I like to help where ever I can. :)

  10. #10
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default

    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.

  11. #11
    Bored2 is offline Member
    Join Date
    Nov 2010
    Posts
    21
    Rep Power
    0

    Default

    at student class I have a problem with ;
    Java Code:
     
       public int comareTo(Comparable student1, Comparable student2) {
             return (student1).getName().compareTo((student2).getName( )) ;
    It says comparable doesn't have a function getname()


    The question is how do I make Student to be Comparable ?

  12. #12
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,785
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by Bored2 View Post
    at student class I have a problem with ;
    Java Code:
     
       public int comareTo(Comparable student1, Comparable student2) {
             return (student1).getName().compareTo((student2).getName( )) ;
    It says comparable doesn't have a function getname()


    The question is how do I make Student to be Comparable ?
    Aren't you confusing the Comparable<T> interface (one argument compareTo( ... ) method) with the Comparator<T> interface (two arguments compareTo( ... ) method)?

    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.

    kind regards,

    Jos

  13. #13
    Bored2 is offline Member
    Join Date
    Nov 2010
    Posts
    21
    Rep Power
    0

    Default

    No, I'm not confusing Comparable to Comperator .


    Normally the Comparable<T> interface is implemented by classes that need to compare objects with object of the same class => this is what I need

    I just can't make Comparable to be student .. how should I do that ?

    ?? Does my Comparable interface OK ? does it missing something ??

  14. #14
    Bored2 is offline Member
    Join Date
    Nov 2010
    Posts
    21
    Rep Power
    0

    Default

    Java Code:
    public interface Comparable 
    {
    	int comareTo(Comparable student1,Comparable student2);	
    }
    
    public class Student implements Comparable , Printable {
    
    	private int id;
    	private String name;
    	
    	Student ()//constructor
    	{
    	   int tempid;
    	   String tempname;
    	   
    	   Scanner scan = new Scanner(System.in);
    	   System.out.println("Please enter ID");
    	   tempid = scan.nextInt();   
    	   System.out.println("Enter Name");
    	   tempname =  scan.nextLine();
    	   
    	   setId(tempid);
    	   setName(tempname);
    	   
    	}
    	
    	public void setId(int id) {
    		this.id = id;
    	}
    	public int getId() {
    		return id;
    	}
    	public void setName(String name) {
    		this.name = name;
    	}
    	public String getName() {
    		return name;
    	}
    	//@Override
    	public int comareTo(Comparable student1, Comparable student2) {
    		return	(student1).getName().compareTo((student2).getName()) ;
    	} 
    
    }

  15. #15
    Bored2 is offline Member
    Join Date
    Nov 2010
    Posts
    21
    Rep Power
    0

    Default

    [QUOTE=venerik;155220]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.

    Object class ?

    you suggest
    Java Code:
         interface Comparable {
          
          int compareTo(Object O1, Object O2)
    actually i tried
    but then when I tried to sort them:
    Java Code:
          if(comparables[i].comparaeTo(comparables[i],comparables[i+1])
    it Gave me an error that compareTo doesn't get 2 comparables

    it says

  16. #16
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,785
    Blog Entries
    7
    Rep Power
    21

    Default

    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.

    Your interface demands code like this:

    Java Code:
    Student s1= new Student();
    Student s2= new Student();
    Student s3= new Student();
    int s1s2= s1.compareTo(s1, s2); // compares s1 and s2
    int s1s2s3= s3.compareTo(s1, s2); // also compares s1 and s2
    Note that last line: it looks funny but that's because any Student object can compare any other two Student objects.

    kind regards,

    Jos

  17. #17
    Bored2 is offline Member
    Join Date
    Nov 2010
    Posts
    21
    Rep Power
    0

    Default

    Jos


    student class
    Java Code:
    	//@Override
    	public int comareTo(Comparable student1, Comparable student2) {
    		return	(student1).getName().compareTo((student2).getName()) ;
    I reciving an error here ... it tells that student1 && student2 doesn't have any getName etc

  18. #18
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,785
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by Bored2 View Post
    Jos


    student class
    Java Code:
    	//@Override
    	public int comareTo(Comparable student1, Comparable student2) {
    		return	(student1).getName().compareTo((student2).getName()) ;
    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:

    Java Code:
    	public int comareTo(Comparable student1, Comparable student2) {
    		return	((Student)student1).getName().compareTo(((Student)student2).getName()) ;
    kind regards,

    Jos

  19. #19
    Bored2 is offline Member
    Join Date
    Nov 2010
    Posts
    21
    Rep Power
    0

    Default

    :-O and I breaking my head for 2 days what is wrong there ..

    WoW thanks you !!

  20. #20
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,785
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by Bored2 View Post
    :-O and I breaking my head for 2 days what is wrong there ..

    WoW thanks you !!
    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.

    kind regards,

    Jos

Page 1 of 2 12 LastLast

Similar Threads

  1. implements
    By danghieu in forum New To Java
    Replies: 1
    Last Post: 04-29-2010, 06:52 PM
  2. ButtonWatcher implements ActionListener
    By darkblue24 in forum New To Java
    Replies: 3
    Last Post: 03-05-2010, 07:18 PM
  3. Hashtable implements ActionListener ?
    By Zayaan in forum New To Java
    Replies: 5
    Last Post: 11-16-2009, 08:49 PM
  4. Adding 2 Implements to the class
    By jboy in forum New To Java
    Replies: 2
    Last Post: 10-23-2009, 06:19 AM
  5. How to implements cryptation in a java
    By paul in forum Advanced Java
    Replies: 1
    Last Post: 08-07-2007, 06:06 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •