944,004 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 3209
  • Java RSS
Dec 20th, 2005
0

Homework Help

Expand Post »
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
Java Syntax (Toggle Plain Text)
  1.  
  2.  
  3.  
  4.  
  5. public class Smallest {
  6.  
  7.  
  8.  
  9. public static void findSmallest(int[] value)
  10. {
  11. for (int i = 0; i < (value.length-1); i++)
  12. {
  13.  
  14. int min = i;
  15. for (int j = i; j < (value.length); j++)
  16. {
  17. if (value[j] < value[min])
  18. {
  19. min = j;
  20. }
  21. }
  22. }
  23. }
  24.  
  25. public static void findSmallest(UMUC_Comparable[] values)
  26. {
  27. for (int i = 0; i < (values.length- 1); i++)
  28. {
  29.  
  30. int min = i; // array position of smallest element
  31.  
  32. for (int j = i; j < (values.length); j++)
  33. {
  34. if (values[j].compareTo(values[min]) < 0)
  35. {
  36. min = j;
  37. }
  38. }
  39. }
  40.  
  41. }
  42.  
  43.  
  44.  
  45.  
  46. public static void main(String[] args)
  47. {
  48. // TODO Auto-generated method stub
  49.  
  50. int[] value = {10, -3, 23, 4, 58, 7, -1, 90};
  51. int small = value[0];
  52.  
  53. findSmallest(value);
  54. for(int k = 0; k < value.length-1; k++)
  55. {
  56. if (value[k] < small)
  57. {
  58. small = value[k];
  59. System.out.println("smallest value[" + k + "] = " + value[k]);
  60. }
  61. }
  62.  
  63.  
  64. Student[] values = { new Student("Tom", 87),
  65. new Student("Cindy", 100),
  66. new Student("Pat", 75),
  67. new Student("Anne", 92),
  68. new Student("Matt", 82)};
  69.  
  70.  
  71. findSmallest(values);
  72.  
  73. for (int i = 0; i < values.length-1; i++)
  74. {
  75. System.out.println(UMUC_Comparable.name + " " +
  76. UMUC_Comparable.average);
  77. }
  78.  
  79. }
  80.  
  81. }

UMUC_Comparable.java
Java Syntax (Toggle Plain Text)
  1.  
  2. public interface UMUC_Comparable {
  3. int average = 0;
  4. String name = null;
  5. int compareTo(UMUC_Comparable comparable);
  6.  
  7.  
  8. }


Student.java
Java Syntax (Toggle Plain Text)
  1. public class Student implements UMUC_Comparable {
  2. int average;
  3. private String name;
  4.  
  5. public Student(String name, int average) {
  6. this.average = average;
  7. this.name = name;
  8. } // end method
  9.  
  10. public String getName() {
  11. return name;
  12. } // end method
  13.  
  14. public int getAverage() {
  15. return average;
  16. } // end method
  17.  
  18. public int compareTo(UMUC_Comparable student) {
  19. Student s = (Student)student;
  20. return (this.average - s.average);
  21.  
  22. } // end method
  23.  
  24.  
  25. }

Can anyone help?
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
blackbabydoll is offline Offline
22 posts
since Dec 2004
Dec 20th, 2005
0

Re: Homework Help

Well I hope someone will help me........
Reputation Points: 10
Solved Threads: 0
Newbie Poster
blackbabydoll is offline Offline
22 posts
since Dec 2004
Dec 20th, 2005
0

Re: Homework Help

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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
blackbabydoll is offline Offline
22 posts
since Dec 2004
Dec 20th, 2005
0

Re: Homework Help

Quote ...
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?
Reputation Points: 113
Solved Threads: 19
Postaholic
server_crash is offline Offline
2,108 posts
since Jun 2004
Dec 21st, 2005
0

Re: Homework Help

Quote originally posted by server_crash ...
Shouldn't that be a 2D array?
No it isn't the names are in quotations.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
blackbabydoll is offline Offline
22 posts
since Dec 2004
Dec 21st, 2005
0

Re: Homework 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.
Team Colleague
Reputation Points: 1658
Solved Threads: 331
duckman
jwenting is offline Offline
7,719 posts
since Nov 2004
Dec 21st, 2005
0

Re: Homework Help

Quote originally posted by blackbabydoll ...
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.
Reputation Points: 113
Solved Threads: 19
Postaholic
server_crash is offline Offline
2,108 posts
since Jun 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: RMI CORBA hmm..which one , how ?
Next Thread in Java Forum Timeline: I'm stuck





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC