| | |
Display how sort works? Please help
![]() |
•
•
•
•
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?
I have to show 2 sorts insertion, and selection sorts demonstration in bars.
What is this bars?
Check out my New Bike at my Public Profile at the "About Me" tab
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
>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
Check out my New Bike at my Public Profile at the "About Me" tab
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:
Also it would be a good idea to print the values in 1 line:
Java Syntax (Toggle Plain Text)
for (int scan = index+1; scan < numbers.length; scan++){ if (numbers[scan] < numbers[min]) min = scan; for(int i=0;i<numbers.length;i++) { System.out.print(numbers[i]+", "); } System.out.println(); }
Check out my New Bike at my Public Profile at the "About Me" tab
•
•
Join Date: Jan 2008
Posts: 25
Reputation:
Solved Threads: 0
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)
/** * @(#)SelectionBars.java * * SelectionBars Applet application * * @author * @version 1.00 2009/1/8 */ import java.awt.event.*; import java.util.Random; import javax.swing.*; import java.awt.*; import java.applet.*; public class SelectionBars extends Applet { //Variables and arrays Rectangle[] bar_array = new Rectangle[10]; int minIndex; public void init() { final int NUM_BARS = 10, WIDTH = 30, MAX_HEIGHT = 300, GAP = 9; int x = GAP, height; bar_array = new Rectangle[10]; Random generator = new Random(); height = generator.nextInt(MAX_HEIGHT) + 1; for (int i = 0; i < NUM_BARS; i++) { bar_array[i] = new Rectangle(x, MAX_HEIGHT-height, WIDTH, height); height = generator.nextInt(MAX_HEIGHT) + 1; x = x + WIDTH + GAP; } } void selectionSort() { for( int i = 0; i < bar_array.length - 1; i++ ) { int minIndex = i; for( int j = i + 1; j < bar_array.length; j++ ) { if( bar_array[j].height < bar_array[minIndex].height ) { minIndex = j; } } if(minIndex > i) { Rectangle temp = bar_array[i]; bar_array[i] = bar_array[minIndex]; bar_array[minIndex] = temp; int tempX = bar_array[i].x; bar_array[i].x = bar_array[minIndex].x; bar_array[minIndex].x = tempX; } } } public void paint (Graphics page) { final int GAP = 9; int x; setBackground (Color.black); page.setColor (Color.blue); x = GAP; for (int i = 0; i < bar_array.length; i++) { Dimension d = bar_array[i].getSize(); Point p = bar_array[i].getLocation(); page.fillRect(p.x, p.y, d.width, d.height); x = x + WIDTH + GAP; } } }
![]() |
Similar Threads
- printing div that was hidden by javascript (HTML and CSS)
- need help with this script please (Python)
- Some Pascal Help - Mostly DONE | Just A Little :D (Pascal and Delphi)
- Can anyone help me with adding 2 polynomials together? (C++)
- javascript works in IE but not working in firefox (JavaScript / DHTML / AJAX)
- can anyone make this tree table sort correctly? (JavaScript / DHTML / AJAX)
- 2 Envision Display difficulties - fuzzy and blank (Monitors, Displays and Video Cards)
- Sorting from a file (C)
- Bubble sort & File output jibrish errors??? (C)
- calling a SQL UDF? (PHP)
Other Threads in the Java Forum
- Previous Thread: Is Java the wrong language for a duplicate file scanner?
- Next Thread: Currency conversion
| Thread Tools | Search this Thread |
account android api applet application array arrays automation bidirectional binary birt bluetooth class classes client code columns component constructor database designadrawingapplicationusingjavajslider draw eclipse error errors exception expand fractal game givemetehcodez graphics gui guidancer homework html ide image inetaddress inheritance integer intellij j2me java javamicroeditionuseofmotionsensor javaprojects jlabel jme jni jpanel jtextfield jtree julia linux list loop map method methods midlethttpconnection mobile mobiledevelopmentcreatejar monitoring myaggfun netbeans newbie nullpointerexception open-source oracle plazmic print problem program project property recursion ria scanner search server set sharepoint smart sms smsspam sort sourcelabs splash sql sqlite static string subclass support swing testautomation threads tree unlimited webservices windows






