• Post Reply Bookmark Topic Watch Topic
  • New Topic

Array Casting

Christy Wilson
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I need a lot of help! I need to know how to cast back from a comparable object to my regular array object. Then I need to know how to use the getName and getAverage methods to print out my smallest object information.

Here is what I have so far. I am very confused about the casting.

public class Student implements UMUC_Comparable {
private int average;
private String name;

public Student(String name, int average) {
this.average = average;
this.name = name;

public String getName() {
return name;

public int getAverage() {
return average;

public int compareTo(UMUC_Comparable student) {
Student s = (Student)student;
return(this.average = s.average);

public static void findSmallest(UMUC_Comparable[] array) {
for (int i = 0; i < (array.length- 1); i++) {

int min = i;

for (int j = i; j < (array.length); j++) {
if (array[j].compareTo(array[min]) < 0)
min = j;

UMUC_Comparable temp = array[min];
array[min] = array[i];
array[i] = temp;


public static void main(String[] args) {
Student[] array = { new Student("Tom", 95)
new Student("Mary", 98),
new Student("Anne", 87),
new Student("Mike", 89) };


Any guidance/help would be greatly appreciated.

Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Let's first look at your compareTo() method:

Do you know what the last line does? Remember that the = operator assigns a value to a variable. In this case, the average or one student is assigned to the average of another student. Is this what you want to do here? Somehow I don't think so. Also, the resulting value of the assignment is the value of the right-hand operand. This means that the compareTo() method returns the average for the student that was passed as a parameter. Again, is this what you want to do? What should the compareTo() method return? Think about this a little. Feel free to post your thoughts here. I'll be glad to give you some feedback.

Now let's look at findSmallest():

It seems to me that this should return either a UMUC_Comparable object or perhaps even a Student object, rather than returning void. If you did so, then you can easily use getAverage(), etc. to print out the information about the object.

Now that I look more closely at your code, it looks like this routine SORTS the array. In my opinion, getSmallest() is a misnomer. If you really intend to sort the array, then perhaps this method should be called sort() instead. On the other hand, if you want to find the smallest element, sorting is a SLOW way to do it. See if you can come up with a different algorithm that only uses one for loop, instead of two.

So depending on what you really mean to do with this findSmallest() method, I suggest you either change the name or change the algorithm and return type. If you simply return a UMUC_Comparable, then you can cast the returned value in main and call its methods. On the other hand, if you really mean to just sort the array, then you need to figure where the smallest element will end up.

Think about this a little bit and let us know what you come up with.


  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!