Win a copy of Head First Android this week in the Android forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

Why the below code is not (Sort) the students objects depend on their Score?

 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Output of the program
Student [StudentId=1001, StudentName=Geetha, StudentScore=80]
Student [StudentId=1002, StudentName=Deepa, StudentScore=70]
Student [StudentId=1003, StudentName=Shwetha, StudentScore=75]
Student [StudentId=1004, StudentName=Lakshmi, StudentScore=60]
Student [StudentId=1005, StudentName=Roopa, StudentScore=65]
Student [StudentId=1006, StudentName=Mamatha, StudentScore=77]


Student.java

StudentAdmin.java


Test.java

 
Saloon Keeper
Posts: 13385
295
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Two reasons:

  • You're comparing StudentId instead of StudentScore.
  • You're calling display() before you call sortStudents().

  • There are other problems with your code:

  • Make your classes final.
  • Don't use the public access modifier if you don't need it.
  • Field names should start with a lowercase letter.
  • Since you're comparing students to students, Student should implement Comparable<Student>, not Comparable<Object>.
  • Stop casting object references. If you REALLY need an ArrayList, let your createStudent() method take an ArrayList instead of a List. But:
  • Don't use more specific types than you need. The collection field can be of type Collection.
  • Use method names that describe what the method is doing. createStudent() doesn't create a student. It sets a collection of students, so it should be called setStudents(). But:
  • Don't use setters for mutable types without defensive copying. I recommend that you rename the createStudent() method to addStudents(), and you call collection.addAll() inside of it.
  •  
    Akash bhamri
    Ranch Hand
    Posts: 33
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Thanxx @Stephan van Hulst now I get the desire output but the another reason you told about createStudent() I dont get it can you please code it so that it would be great and I was make all my class final now..
     
    Akash bhamri
    Ranch Hand
    Posts: 33
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    and also without the toString() method in student.java
    I am getting this output
    Assisgnment3.Student@4361bd48
    Assisgnment3.Student@53bd815b
    Assisgnment3.Student@2401f4c3
    Assisgnment3.Student@7637f22
    Assisgnment3.Student@4926097b
    Assisgnment3.Student@762efe5d

    because it is a call display that is in student admin but Is there any method to call the values instead of there address directly?
     
    Marshal
    Posts: 8105
    571
    Mac OS X VI Editor BSD Linux
    • Likes 2
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    When you pass an object to a System.out.println() method, toString() method gets called of the passed object.

    Do you still have toString() method overridden/implemented in the Student class? If not, that's what you get.

    Akash bhamri wrote:Is there any method to call the values instead of there address directly?


    I think overriding toString() method is the right approach.
     
    Akash bhamri
    Ranch Hand
    Posts: 33
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Thanxx buddy got it.
     
    Don't get me started about those stupid light bulbs.
    reply
      Bookmark Topic Watch Topic
    • New Topic