I am doing a project where I have the find the smallest value and the smallest object.

In this project, you will write two methods. The first will take an array of int and will return the smallest value in the array. It will have a declaration as follows:
int findSmallest(int[] array);
This method should walk through the array, find the smallest element in the array, and return the value of that element. You should then create a main that creates and initializes an array of primitives as part of the declaration of the array, call the method, and print out the value of the smallest element in the array.
The second method should also find the smallest element, but should use an array of UMUC_Comparables and the compareTo method to find the smallest object. It will have a definition as follows:
UMUC_Comparable findSmallest(UMUC_Comparable[] array);
Create a main that creates an array of Student objects, as in section III of module 5's commentary. You do not have to rewrite the compareTo method; you can simply use the one defined in module 5. Create and initialize an array of the Student objects and find the smallest object using this method. You should then print out the Student object returned. Note that the return from the method is a UMUC_Comparables, not a Student object, so you will need to 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);

Well I keep getting the in my output null 0 for my comparables when it should be a name and a student's average.

Here is my code:

Smallest.java

public class Smallest {



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

	          int min = i;  
	          for (int j = i; j < (value.length); j++) 
	          {
	            if (value[j] < value[min])
	            {
	               min = j;
	            }
	          }
		}
	}
	
	public static void findSmallest(UMUC_Comparable[] values)
	{
		for (int i = 0; i < (values.length- 1); i++) 
		{

		      int min = i;  // array position of smallest element

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

	}


	

	public static void main(String[] args) 
	{
		// TODO Auto-generated method stub

		int[] value = {10, -3, 23, 4, 58, 7, -1, 90};
		int small = value[0];
		
		findSmallest(value);
		for(int k = 0; k < value.length-1; k++)
		{
		   if (value[k] < small)
		   {
			   small = value[k];
			   System.out.println("smallest value[" + k + "] = " + value[k]);
		   }   
		} 
	

		Student[] values = { new Student("Tom", 87),
                new Student("Cindy", 100),
                new Student("Pat", 75),
                new Student("Anne", 92),
                new Student("Matt", 82)};

		      
		findSmallest(values);

		for (int i = 0; i < values.length-1; i++) 
		{
		   System.out.println(UMUC_Comparable.name + " " +
		   UMUC_Comparable.average);       
		}
		
	}
	
}

UMUC_Comparable.java

public interface UMUC_Comparable {
   int average = 0;
   String name = null;
   int compareTo(UMUC_Comparable comparable);


}

Student.java

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

   public Student(String name, int average) {
      this.average = average;
      this.name = name;
   } // end method

   public String getName() {
      return name;
   } // end method

   public int getAverage() {
      return average;
   } // end method

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

   } // end method
   

}

Can anyone help?

Recommended Answers

All 6 Replies

Well I hope someone will help me........

Exception in thread "main" java.lang.Error: Unresolved compilation problem:
The operator < is undefined for the argument type(s) Student, Student

at Student.main(Student.java:63)


That is the error i got.........SOME PLEASE HELP I AM BEGGING

Student[] values = { new Student("Tom", 87),
new Student("Cindy", 100),
new Student("Pat", 75),
new Student("Anne", 92),
new Student("Matt", 82)};

Shouldn't that be a 2D array?

Shouldn't that be a 2D array?

No it isn't the names are in quotations.

1) the result of your comparison is thrown away as soon as you exit your findSmallest method, so you might as well not have run that method in the first place.
2) Java doesn't have operator overloading so you can't use mathematical operators on class instances. Your class is obviously longer than the bit you posted and the error in another part you didn't show.

No it isn't the names are in quotations.

I don't know what I was thinking. I swear I saw curly braces instead of parenthesis.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.