943,724 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 1076
  • Java RSS
You are currently viewing page 1 of this multi-page discussion thread
Jan 4th, 2009
0

Display how sort works? Please help

Expand Post »
I have attached the file with the sorts. I have to show 2 sorts insertion, and selection sorts demonstration in bars. I should show the position of the bars after each run. How do i go on about doing that?
Similar Threads
Reputation Points: 7
Solved Threads: 0
Light Poster
Alishaikh is offline Offline
25 posts
since Jan 2008
Jan 4th, 2009
0

Re: Display how sort works? Please help

I didn't post the file sorry. It is attached below. Do I have to make changes to this program? or can I write a driver one?
Attached Files
File Type: java Sorts.java (2.8 KB, 11 views)
Reputation Points: 7
Solved Threads: 0
Light Poster
Alishaikh is offline Offline
25 posts
since Jan 2008
Jan 5th, 2009
0

Re: Display how sort works? Please help

Would you mind putting your code directly here in code tags ?
Reputation Points: 485
Solved Threads: 89
Posting Shark
verruckt24 is offline Offline
944 posts
since Nov 2008
Jan 5th, 2009
0

Re: Display how sort works? Please help

Click to Expand / Collapse  Quote originally posted by Alishaikh ...
I have attached the file with the sorts. I have to show 2 sorts insertion, and selection sorts demonstration in bars. I should show the position of the bars after each run. How do i go on about doing that?
What do you mean when you say:
I have to show 2 sorts insertion, and selection sorts demonstration in bars.

What is this bars?
Sponsor
Featured Poster
Reputation Points: 1014
Solved Threads: 446
Nearly a Senior Poster
javaAddict is offline Offline
3,258 posts
since Dec 2007
Jan 7th, 2009
0

Re: Display how sort works? Please help

hii
try to make your question more clear until we can help you with your problem.
good luck
Reputation Points: 10
Solved Threads: 1
Newbie Poster
hypocrisy is offline Offline
6 posts
since Jan 2009
Jan 7th, 2009
0

Re: Display how sort works? Please help

Well I have to show the sort go through the array. Like every time it goes through the array, in an applet
Reputation Points: 7
Solved Threads: 0
Light Poster
Alishaikh is offline Offline
25 posts
since Jan 2008
Jan 8th, 2009
0

Re: Display how sort works? Please help

You can create an array with some numbers inside.
>Then print its values,
>Call the sort method
>Print again the values.

Also if you want to see how the sort progresses you can change the methods given and add some :
>System.out.printlns after each loop is run.
Every time a loop of the method is executed print the values. (You will need another for-loop) to print the values.

As for applets have a textarea where you print the values of the array in each line. Check the API for the JTextArea class. Also you probably have some notes on how to create an applet.


What I can't understand is why you where given the code ready and asked to do it in applet. Usually applets are more "advanced" than a simple sort method. How can your teacher expect you to write an applet when you haven't even learned the simple sort algorithms since he is handing them to you ?

I believe that you should first learn how to write these sort algorithms before being asked to do them in applets
Sponsor
Featured Poster
Reputation Points: 1014
Solved Threads: 446
Nearly a Senior Poster
javaAddict is offline Offline
3,258 posts
since Dec 2007
Jan 8th, 2009
0

Re: Display how sort works? Please help

No no, I wrote the sorts in an earlier program. But I cant get it to print after every loop. This is what I have so far(I decided to forgo the applet, and do this first, and its due within the next hour. I'm in school right now)
BUT it wont work at all....PLease help
Attached Files
File Type: java Array.java (458 Bytes, 5 views)
File Type: java Sorts.java (3.1 KB, 10 views)
Reputation Points: 7
Solved Threads: 0
Light Poster
Alishaikh is offline Offline
25 posts
since Jan 2008
Jan 8th, 2009
0

Re: Display how sort works? Please help

You don't say what doesn't work and what errors you get.

Also it would be a good idea to print the values in 1 line:
Java Syntax (Toggle Plain Text)
  1. for (int scan = index+1; scan < numbers.length; scan++){
  2. if (numbers[scan] < numbers[min])
  3. min = scan;
  4. for(int i=0;i<numbers.length;i++) {
  5. System.out.print(numbers[i]+", ");
  6. }
  7. System.out.println();
  8. }
Sponsor
Featured Poster
Reputation Points: 1014
Solved Threads: 446
Nearly a Senior Poster
javaAddict is offline Offline
3,258 posts
since Dec 2007
Jan 8th, 2009
0

Re: Display how sort works? Please help

I made new code!!! This is an applet, but I dont know how to keep displaying the bars after each loop. PLease tell me how I can do that.
Java Syntax (Toggle Plain Text)
  1. /**
  2.  * @(#)SelectionBars.java
  3.  *
  4.  * SelectionBars Applet application
  5.  *
  6.  * @author
  7.  * @version 1.00 2009/1/8
  8.  */
  9.  
  10. import java.awt.event.*;
  11. import java.util.Random;
  12. import javax.swing.*;
  13. import java.awt.*;
  14. import java.applet.*;
  15.  
  16. public class SelectionBars extends Applet {
  17. //Variables and arrays
  18. Rectangle[] bar_array = new Rectangle[10];
  19. int minIndex;
  20. public void init() {
  21. final int NUM_BARS = 10, WIDTH = 30, MAX_HEIGHT = 300, GAP = 9;
  22. int x = GAP, height;
  23. bar_array = new Rectangle[10];
  24.  
  25. Random generator = new Random();
  26. height = generator.nextInt(MAX_HEIGHT) + 1;
  27.  
  28. for (int i = 0; i < NUM_BARS; i++)
  29. {
  30. bar_array[i] = new Rectangle(x, MAX_HEIGHT-height, WIDTH, height);
  31. height = generator.nextInt(MAX_HEIGHT) + 1;
  32. x = x + WIDTH + GAP;
  33. }
  34.  
  35. }
  36. void selectionSort()
  37. {
  38. for( int i = 0; i < bar_array.length - 1; i++ )
  39. {
  40. int minIndex = i;
  41. for( int j = i + 1; j < bar_array.length; j++ )
  42. {
  43. if( bar_array[j].height < bar_array[minIndex].height )
  44. {
  45. minIndex = j;
  46. }
  47. }
  48.  
  49. if(minIndex > i)
  50. {
  51. Rectangle temp = bar_array[i];
  52. bar_array[i] = bar_array[minIndex];
  53. bar_array[minIndex] = temp;
  54.  
  55. int tempX = bar_array[i].x;
  56. bar_array[i].x = bar_array[minIndex].x;
  57. bar_array[minIndex].x = tempX;
  58. }
  59. }
  60. }
  61.  
  62.  
  63.  
  64.  
  65.  
  66.  
  67.  
  68.  
  69. public void paint (Graphics page)
  70. {
  71. final int GAP = 9;
  72. int x;
  73. setBackground (Color.black);
  74.  
  75. page.setColor (Color.blue);
  76. x = GAP;
  77.  
  78. for (int i = 0; i < bar_array.length; i++)
  79. {
  80. Dimension d = bar_array[i].getSize();
  81. Point p = bar_array[i].getLocation();
  82. page.fillRect(p.x, p.y, d.width, d.height);
  83.  
  84. x = x + WIDTH + GAP;
  85. }
  86.  
  87. }
  88.  
  89. }
Reputation Points: 7
Solved Threads: 0
Light Poster
Alishaikh is offline Offline
25 posts
since Jan 2008

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: Is Java the wrong language for a duplicate file scanner?
Next Thread in Java Forum Timeline: Currency conversion





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


Follow us on Twitter


© 2011 DaniWeb® LLC