| | |
Homework Help
![]() |
•
•
Join Date: Dec 2004
Posts: 22
Reputation:
Solved Threads: 0
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
UMUC_Comparable.java
Student.java
Can anyone help?
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
Java Syntax (Toggle Plain Text)
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
Java Syntax (Toggle Plain Text)
public interface UMUC_Comparable { int average = 0; String name = null; int compareTo(UMUC_Comparable comparable); }
Student.java
Java Syntax (Toggle Plain Text)
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?
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.
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.
As people are clearly allowed to attack me but I'm not allowed to defend myself, I no longer post to this site.
![]() |
Similar Threads
- We only give homework help to those who show effort (Computer Science)
- Need help with Computer Science homework (Computer Science)
- Dynamic memory allocation homework (C++)
- Homework Help!! Priority Queue ?? (Computer Science)
Other Threads in the Java Forum
- Previous Thread: RMI CORBA hmm..which one , how ?
- Next Thread: I'm stuck
| Thread Tools | Search this Thread |
addball android applet application apps array automation awt bidirectional binary birt bluetooth businessintelligence busy_handler(null) button card class classes client code collision columns component constructor crashcourse css database designadrawingapplicationusingjavajslider draw eclipse ee error eventlistener exception expand fractal free game givemetehcodez graphics gui guidancer html ide image integration intellij j2me java javaarraylist javadoc javafx javamicroeditionuseofmotionsensor javaprojects jme jni jpanel jtree julia jvm linux loan loop method migrate mobile mobiledevelopmentcreatejar myaggfun netbeans newbie oracle phone physics plazmic print problem program programming project radio scanner server service set sharepoint smart sms smsspam software sql subclass support swing textfield threads tree trolltech unlimited utility windows






