I have been given a second part to an assignement that wants me to create a program for comparing student obejects. The assignment description and code are below. I can' t see how the find to method interlocks with the findsmallest because find smallest is already finding the smallest value. I also don't see where the new diffinitions for UMUC_Comparable goes.
In the second part of this project, you will also find the smallest element in the array, but now the array will be of UMUC_Comparable objects. It will use the compareTo method to compare objects to find the smallest one. This method will also have the name findSmallest, but will take an array of UMUC_Comparable objects. This findSmallest method will have a definition as follows:
UMUC_Comparable findSmallest(UMUC_Comparable[] array);
The findSmallest method will use the compareTo method in the UMUC_Comparable interface. You will be using it with the Student objects from module V, section III, so you do not have to rewrite the compareTo method; you can simply use the one defined in the Student object in module V.
For the second part of this project, you should:
Create a main that creates an array of Student objects, as in section III of module V. You can use the same array as defined module V. You do not have to read these objects in from a file.
Call the findSmallest method to find the smallest Student object.
Use the getName and getAverage methods in the Student object to print out the smallest object.
Note that the return from the method is a UMUC_Comparable object, not a Student object, so you must cast the returned object to a Student object before printing it out. You can do so as follows:
Student[] students ....; // Fill in the declaration // of the student array.
Student s = (Student)findSmallest(UMUC_Comparable[] array);
/* File: Student.java
* Author: Darrell Clark
* Date: December 3, 2006
* Purpose: Shows how to find the smallest Int value in an array
*/
import java.util.*;
import java.io.*;
public class Student {
private int average;
private String name;
/* public constructor. Note that because no default
* constructor exists, Student objects will always
* be constructed with a name and an average.
*/
public Student(String name, int average) {
this.average = average;
this.name = name;
} // end method
/*
* return name
*/
public String getName() {
return name;
} // end method
/*
* return average
*/
public int getAverage() {
return average;
} // end method
/*
* compare to method for locating smallest value
*/
public static int findSmallest(int[] array) {
int min = Integer.MAX_VALUE;
for (int i = 1; i < (array.length); i++) {
if (array < min)
min = array;
}
return min;
}
/*
* compare student value
*/
public int compareTo(Student student) {
return (this.average - student.average);
} // end method
public static void main(String[] args) {
Student[] studentArray = { new Student("Tom", 87),
new Student("Cindy", 100),
new Student("Pat", 75),
new Student("Anne", 92),
new Student("Matt", 82)};
for (int i = 0; i < studentArray.length; i++) {
System.out.println(studentArray.name + " " +
studentArray.average);
} // end for
} // end method
} // end class