Homework Help

Reply

Join Date: Dec 2004
Posts: 22
Reputation: blackbabydoll is an unknown quantity at this point 
Solved Threads: 0
blackbabydoll blackbabydoll is offline Offline
Newbie Poster

Homework Help

 
0
  #1
Dec 20th, 2005
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
  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
  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
  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?
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 22
Reputation: blackbabydoll is an unknown quantity at this point 
Solved Threads: 0
blackbabydoll blackbabydoll is offline Offline
Newbie Poster

Re: Homework Help

 
0
  #2
Dec 20th, 2005
Well I hope someone will help me........
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 22
Reputation: blackbabydoll is an unknown quantity at this point 
Solved Threads: 0
blackbabydoll blackbabydoll is offline Offline
Newbie Poster

Re: Homework Help

 
0
  #3
Dec 20th, 2005
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
Reply With Quote Quick reply to this message  
Join Date: Jun 2004
Posts: 2,108
Reputation: server_crash is on a distinguished road 
Solved Threads: 18
server_crash server_crash is offline Offline
Postaholic

Re: Homework Help

 
0
  #4
Dec 20th, 2005
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?
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 22
Reputation: blackbabydoll is an unknown quantity at this point 
Solved Threads: 0
blackbabydoll blackbabydoll is offline Offline
Newbie Poster

Re: Homework Help

 
0
  #5
Dec 21st, 2005
Originally Posted by server_crash
Shouldn't that be a 2D array?
No it isn't the names are in quotations.
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 6,145
Reputation: jwenting is just really nice jwenting is just really nice jwenting is just really nice jwenting is just really nice 
Solved Threads: 212
Team Colleague
jwenting's Avatar
jwenting jwenting is offline Offline
duckman

Re: Homework Help

 
0
  #6
Dec 21st, 2005
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.
As people are clearly allowed to attack me but I'm not allowed to defend myself, I no longer post to this site.
Reply With Quote Quick reply to this message  
Join Date: Jun 2004
Posts: 2,108
Reputation: server_crash is on a distinguished road 
Solved Threads: 18
server_crash server_crash is offline Offline
Postaholic

Re: Homework Help

 
0
  #7
Dec 21st, 2005
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.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Java Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC