question

Reply

Join Date: Sep 2007
Posts: 30
Reputation: ronghel is an unknown quantity at this point 
Solved Threads: 0
ronghel ronghel is offline Offline
Light Poster

question

 
0
  #1
Jan 10th, 2008
can someone explain this to me... coz i cant understand.. im trying to this one but i cant do it.

Implement and test this method:
static double max(double[] x) {
// returns the maximum of the elements in the array x
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 2,355
Reputation: masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of 
Solved Threads: 252
Moderator
masijade's Avatar
masijade masijade is offline Offline
Nearly a Posting Maven

Re: question

 
0
  #2
Jan 10th, 2008
If you can use it, Arrays.sort and return the last element. Otherwise, save the first value and compare it to the others always saving the larger value and then return that.

What part of all that are you having problems with?
Java Programmer and Sun Systems Administrator

----------------------------------------------

Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 30
Reputation: ronghel is an unknown quantity at this point 
Solved Threads: 0
ronghel ronghel is offline Offline
Light Poster

Re: question

 
0
  #3
Jan 10th, 2008
sorry man.. i really dont understand that one.. ill just read again my notes.. my teacher didnt discuss that part..-_-

i also dont understand this one..

Static Boolean hasDuplicates (int[] a) {
// returns true if the array a[] has any duplicate elements
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 2,355
Reputation: masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of 
Solved Threads: 252
Moderator
masijade's Avatar
masijade masijade is offline Offline
Nearly a Posting Maven

Re: question

 
0
  #4
Jan 10th, 2008
Same concept, but just checking for equality.
Java Programmer and Sun Systems Administrator

----------------------------------------------

Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 791
Reputation: darkagn has a spectacular aura about darkagn has a spectacular aura about darkagn has a spectacular aura about 
Solved Threads: 109
darkagn's Avatar
darkagn darkagn is offline Offline
Master Poster

Re: question

 
0
  #5
Jan 10th, 2008
Hi ronghel,

If I gave you a list of numbers and asked you which one was the biggest, how would you do that? (Not in code, just in life...)

For your second one, if I gave you a list of numbers and asked you if there were any duplicates in the list, how would you know?

For both just think about the steps you would take to solve each problem. Then worry about coding and testing after and it will be much easier
There are no stupid questions, only those too stupid to ask for help.
echo is a web developer's best friend.
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 30
Reputation: ronghel is an unknown quantity at this point 
Solved Threads: 0
ronghel ronghel is offline Offline
Light Poster

Re: question

 
0
  #6
Jan 10th, 2008
  1. public class Problem8
  2. {
  3. public static void main(String[]args)
  4. {
  5. int a=0;
  6.  
  7. System.out.print(max(a));
  8. System.out.println();
  9.  
  10. }
  11.  
  12.  
  13. public static double max(double[]x)
  14. {
  15. int a,b,c,d=0;
  16. double high=0;
  17. double [] check = {1,2,3,4,5,6,7,8,9};
  18. for(a=0;a<check.length;a++)
  19. {
  20. if(check[a]>high)
  21. high=check[a];
  22. }
  23. return x;
  24. }
  25.  
  26.  
  27. }
this is the code but it doesnt work tsktsk.... can someone enlighten me.. hehe
Last edited by Narue; Jan 10th, 2008 at 10:21 am. Reason: Added code tags
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,614
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 713
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: question

 
1
  #7
Jan 10th, 2008
>public static double max(double[]x)
This signature suggests that you're supposed to pass an array of doubles to the function and the function returns the largest of them. Your code doesn't do that at all. Use this as your driver:
  1. public class Problem8
  2. {
  3. public static void main(String[] args)
  4. {
  5. double[] check = {1,2,3,4,5,6,7,8,9};
  6.  
  7. System.out.println(max(check));
  8. }
  9.  
  10. public static double max(double[] x)
  11. {
  12. // Find the largest value in x and return it
  13. }
  14. }
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 30
Reputation: ronghel is an unknown quantity at this point 
Solved Threads: 0
ronghel ronghel is offline Offline
Light Poster

Re: question

 
0
  #8
Jan 10th, 2008
  1. public class Problem7
  2.  
  3. {
  4.  
  5. public static void main(String[] args)
  6.  
  7. {
  8.  
  9. int [] check = {1,2,3,4,5,6,7,8,9,10};
  10.  
  11.  
  12.  
  13. System.out.println(max(check));
  14.  
  15.  
  16. }
  17.  
  18. public static Boolean max(int[] c)
  19. {
  20. // returns true if the array a[] has any duplicate elements
  21.  
  22.  
  23. int high=0, b=0;
  24. for(int a=0;a<c.length;a++)
  25. {
  26. if(c[a]==c.length)
  27. System.out.print(c[a]);
  28. }
  29. return true;
  30.  
  31.  
  32. }
  33. }
how about this one... i want to find the duplicated entry and return true.. but i think its not correct
Last edited by Narue; Jan 10th, 2008 at 1:31 pm. Reason: Added code tags
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4,438
Reputation: Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of 
Solved Threads: 510
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster

Re: question

 
0
  #9
Jan 10th, 2008
You aren't doing any element comparisons at all. Think about what operation would be needed to see if any element in the array was equal to another element.
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 30
Reputation: ronghel is an unknown quantity at this point 
Solved Threads: 0
ronghel ronghel is offline Offline
Light Poster

Re: question

 
0
  #10
Jan 11th, 2008
  1.  
  2. import java.util.*;
  3.  
  4. public class Problem7
  5. {
  6. public static void main(String[]args);
  7. private static Random random = new Random();
  8. {
  9. // *WILL I PUT DECLERATION HERE??**
  10. I DONT KNOW WHAT TO PUT HERE
  11.  
  12. }
  13.  
  14. public static boolean isSorted(int[] a)
  15. {
  16. // returns false unless a[0] <= a[1] <= a[2] <= a[2] <= . . .
  17. for (int i = 1; 1<a.length; i++)
  18. if (a[i] < a[i-1])
  19. return false;
  20. return true;
  21. }
  22.  
  23. public static void printed(int[] a)
  24. {
  25. // prints the elements of the specified array in sequence.
  26. if ( a == null | a.length == 0)
  27. return;
  28. System.out.print("{" + a[0]);
  29. for (int i=1; i<a.length; i++)
  30. System.out.print("," + a[i]);
  31. System.out.print("}");
  32.  
  33. }
  34.  
  35. public static int[] randomIntarray( int length, int range)
  36. {
  37. // returns a new int array of the specified length whose elements
  38. // are randomly distributed in the range 0 to range-1;
  39. int[] a = new int[length];
  40. for (int i=0; i<length; i++)
  41. a[i] = random.nextInt(range);
  42. return a;
  43. }
  44.  
  45. public static int[] resized(int[] a, int length )
  46. {
  47. // returns a new array with the specified length
  48. // containing the same elements as the specified array;
  49. int[] aa = new int[length];
  50. int n = Math.min(a.length, length);
  51. System.arraycopy(a, 0, aa, 0, n);
  52. return aa;
  53. }
  54.  
  55. public static void swap(int[] a, int i, int j)
  56. {
  57. // interchanges elements a[i] and a[j];
  58. int ai=a[i], aj=a[j];
  59. a[i] = aj;
  60. a[j] = ai;
  61. }
  62.  
  63. Static Boolean hasDuplicates (int[] a)
  64. {
  65. // returns true if the array a[] has any duplicate elements
  66. }
  67. }

can someone try to explain this one...? coz i got always error when i try to run it..
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